gpt4 book ai didi

java - JSF : commandLink as a parameter for outputFormat

转载 作者:搜寻专家 更新时间:2023-11-01 02:18:33 24 4
gpt4 key购买 nike

我正在对一些 JSF 文件进行国际化,因此我正在外部化字符串(以及使用占位符连接字符串)。我对 JSF 的经验很少(今天和昨天),所以如果我的问题有明显的答案,请原谅!

我一直在为简单的占位符成功使用 h:outputFormat 标签(和 f:param 标签),但现在我想用 commandLink 组件替换占位符。

<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”文本之前),有人知道我该怎么做吗?

提前致谢。

编辑/更新

感谢您的回复 McDowell - 非常有用,但仍然没有完全解决我的问题。我还需要对输入框 (h:inputText) 执行相同的操作,并且可能还会出现一个资源字符串中有多个占位符的情况。因此,我不能保证顺序,比如说,阿拉伯语,将是相同的。

如果我使用 Java 函数;你知道是否有一种方法可以将 JSF 标签作为字符串传递,例如<h:outputFormat value=.. .并使用 faces 上下文获取呈现的 HTML,然后我可以将其插入相应的占位符并作为纯 HTML 返回?或者沿着这些思路的任何其他想法?

干杯。

最佳答案

h:outputFormat标签将(据我所知)只使用 f:param child 作为参数。

如果消息是链接(而不是 h:commandLink ),您可以使用 HTML 文字:

<h:outputFormat escape="false" 
value="...seconds, please &lt;a href='etc..." />.

我不建议尝试通过这样做来模拟 h:commandLink。可能会在 third party library 中找到更丰富的 h:commandLink 版本这正是您想要的。

将字符串一分为二是可行的,但这会给翻译带来困难,而且翻译质量可能会受到影响(尤其是如果您经常这样做的话)。

我会考虑使用这样的自定义函数:

<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.

该函数由一个静态方法支持,该方法拆分字符串并返回请求的片段:

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
}

根据您的 View 技术(Facelets 或 JSP),函数声明略有不同:


编辑:基于更多信息

JSF 采用声明式方法来构建接口(interface)。您想要一个动态 UI,其中的子项根据它们在翻译后的字符串中出现的顺序排序。这两件事是矛盾的,但并非全无。

有几种方法可以解决这个问题。

  • 重新设计用户界面以在上方使用文本,在下方使用输入/控制表(流行的表单方法)。这是最简单的解决方案,但可能不会让您的 UI 设计师满意。
  • 使用binding attribute on a panel创建并以编程方式订购一切。这可能意味着您的支持 bean 中有很多重复代码。
  • 编写一个可重复使用的自定义控件,它可能如下所示:

.

<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 的渲染器以支持任意子项而不是创建完整的自定义控件可能(并且工作更少),但我反对它,因为它可能会导致长期困惑维护。

可能有人已经按照我的建议编写了一个自定义控件(这很难算作原创)。

关于java - JSF : commandLink as a parameter for outputFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1205386/

24 4 0