JSF:commandLink作为outputFormat的参数 [英] JSFs: commandLink as a parameter for outputFormat
问题描述
我正在将一些JSF文件国际化,因此我正在外化字符串(以及使用占位符连接字符串)。我对JSF(今天和昨天)的经验很少,如果对我的问题有一个明显的答案,请原谅我!
I'm in the process of internationalising some JSF files so am externalising strings (and the concatenation of strings using placeholders). I have very little experience with JSF (today and yesterday) so excuse me if there is an obviuos answer to my question!
我一直在使用h:outputFormat标签(成功用于简单占位符的f:param标签,但现在我想用commandLink组件替换占位符。
I have been using the h:outputFormat tag (and the f:param tag) successfully for simple placeholders but now I'm looking to replace a placeholder with a commandLink component.
ie
<h:outputFormat value="#{adminMsgs.scheduleUpdateLink} ">
<h:commandLink value="#{adminMsgs.update}" action="refresh" />
</h:outputFormat>
属性文件:
scheduleUpdateLink = After waiting a few seconds, please {0} the page.
update = update
并按以下方式输出:
After waiting a few seconds, please <a href="#" class="..." onclick="...;return false;">update</a> the page.
此无法工作(更新链接出现在'scheduleUpdateLink之前'text),有谁知道我怎么做?
This doesn't work (the update link appears before the 'scheduleUpdateLink' text), does anyone know how I might do it?
提前致谢。
编辑/更新
感谢您的回复McDowell - 非常有用,但仍未完全解决我的问题。我还需要对输入框(h:inputText)执行相同的操作,并且可能还存在一个资源字符串中有多个占位符的情况。因此,我无法保证阿拉伯语中的顺序是相同的。
Thanks for your reply McDowell - very useful, but still haven't completely solved my problem. I will also be needing to do the same with input boxes (h:inputText), and there may also be instances where there are multiple placeholders in one resource string. Hence I cannot guarantee the order in, say, Arabic, will be the same.
如果我使用Java函数;你知道我是否有办法以字符串形式传递JSF标签,例如: < h:outputFormat value = ..
。并使用faces上下文来获取呈现的HTML,然后我可以将其插入到各自的占位符中并以纯HTML格式返回?或者沿着这些方向的任何其他想法?
If I went with a Java function; do you know if there is a way I can pass, as a string, the JSF tags, e.g. <h:outputFormat value=..
. and use the faces context to obtain the rendered HTML which I can then insert into respective placeholders and return as plain HTML? Or any other ideas along these lines?
干杯。
推荐答案
h:outputFormat 标记将(据我所知)只使用 f:param 子项作为参数。
The h:outputFormat tag will (as far as I know) only use f:param children as parameters.
如果消息是链接(而不是 h:commandLink ),您可以使用HTML文字:
If the message was a link (instead of a h:commandLink), you could use HTML literals:
<h:outputFormat escape="false"
value="...seconds, please <a href='etc..." />.
我不建议尝试通过这样做来模拟h:commandLink。有可能在第三方库中找到更丰富的h:commandLink版本希望。
I would not recommend trying to emulate h:commandLink by doing this. It might be possible to find a richer version of h:commandLink in a third party library that does exactly what you want.
将字符串分成两部分是可能的,但是翻译人员的生活很困难,翻译质量可能会受到影响(特别是如果你这么做的话) 。
Just splitting the string into two is possible, but makes life difficult for translators and the quality of translation may suffer (especially if you're doing this a lot).
我会考虑使用这样的自定义函数:
I'd look at using a custom function like this:
<f:loadBundle basename="i18n.I18n" var="bundle" />
<h:outputText value="#{i18n:fragment(bundle.scheduleUpdateLink, 0)}" />
<h:commandLink>
<h:outputText value="#{bundle.update}" />
</h:commandLink>
<h:outputText value="#{i18n:fragment(bundle.scheduleUpdateLink, 1)}" />
呈现的输出格式为:
After waiting a few seconds, please <script type="text/javascript"><!--
/*JavaScript elided*/
//-->
</script><a href="#"
onclick="return oamSubmitForm('someClientId');">update</a> the page.
该函数由一个静态方法支持,该方法拆分字符串并返回请求的片段:
The function is backed by a static method that splits the string and returns the requested fragment:
public static String fragment(String string, int index) {
// TODO: regex probably not complete; ask stackoverflow!
// see http://java.sun.com/javase/6/docs/api/java/text/MessageFormat.html
final String regex = "\\{\\d.*?\\}";
String[] fragments = string.split(regex, index + 2);
return fragments[index];
// TODO: error handling
}
取决于您的视图技术( Facelets或JSP),函数声明略有不同:
Depending on your view technology (Facelets or JSP), the function declaration is slightly different:
- Facelets tag library documentation
- Java EE 5 tutorial on JSP tag libraries
编辑:基于更多信息
JSF采用声明式构建接口的方法。您想要一个动态UI,其子项按照它们在翻译字符串中出现的顺序排序。这两件事情不一致,但并没有丢失。
JSF takes a declarative approach to building interfaces. You want a dynamic UI with children ordered according to the order they appear in a translated string. These two things are at odds, but all is not lost.
有几种方法可以解决这个问题。
There are a few ways to approach this.
- 重新设计UI以使用上面的文本,下面的输入/控制表(流行的表单方法)。这是最简单的解决方案,但可能无法让您的UI设计师满意。
- 使用面板上的绑定属性,用于创建和编程排序所有内容。这可能意味着你的支持bean中有很多重复的代码。
- 编写一个可重用的自定义控件,它可能如下所示:
- Redesign the UI to use text above, input/control table below (the popular form approach). This is the simplest solution, but might not make your UI designer happy.
- Use the binding attribute on a panel to create and programmatically order everything. This might mean a lot of repetitive code in your backing beans.
- Write a reusable custom control which might look like this:
。
<ed:formatPane string="click {0} after filling in forename {1} and surname {2}">
<h:commandLink value="#{bundle.linkMsg}" />
<h:inputText value="#{bean.prop1}" />
<h:inputText value="#{bean.prop2}" />
</ed:formatPane>
为h:outputFormat替换渲染器可能是(并且工作量少)可以支持任意孩子而不是创建一个完整的自定义控件,但我反对它,因为它可能会导致长期维护期间的混乱。
It is probably possible (and less work) to just replace the renderer for h:outputFormat to support arbitrary children instead of creating a full custom control, but I'd argue against it because it may lead to confusion during long-term maintenance.
有人可能已经编写了一个自定义控制做我的建议(它几乎不是一个原创的想法)。
Someone may have already written a custom control that does what I suggest (it can hardly be an original idea).
这篇关于JSF:commandLink作为outputFormat的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!