JSF< h:outputFormat&gt ;:使用数组值作为参数 [英] JSF <h:outputFormat>: use array values as parameters

查看:108
本文介绍了JSF< h:outputFormat&gt ;:使用数组值作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JSF2页面上,我正在使用国际化的错误消息.

On my JSF2 page, i'm using internationalized error messages.

在我的备用bean中,我将消息放入Flash Scope:

In my backing bean, i'm putting the messages into the flash Scope:

flash.put("error", exception.getType());

在页面上,此字符串的翻译方式如下:

On the page, this string gets translated this way:

<h:outputText value="#{bundle[flash.error]}"/>

工作正常.

现在,我还希望能够将(任意数量的)参数放入消息文本中,然后将其插入到message.properties的i18n属性中的占位符中.因此,我将参数作为String数组放入Flash Scope中,如下所示:

NOW i want to be also able to put (an arbitrary number of) parameters into the message text, that get inserted into the placeholders in the i18n-property in my message.properties. Therefore, i'm putting the parameters as a String array into the Flash Scope, like this:

//exception.getParameters returns String[]
flash.put("errorParams", exception.getParameters())

现在,我还希望能够将此String数组用作outputFormat元素的参数,以将其插入到Welcome, {0} {1}之类的属性中. 所以我试图通过使用 ui:repeat :

Now i also want to be able to use this String array as parameters for an outputFormat element, to insert them into a property like Welcome, {0} {1}. So i tried to achieve this by using ui:repeat:

<h:outputFormat value="#{bundle[flash.error]}" rendered="#{! empty flash.error}" class="invalid">
  <ui:repeat value="#{flash.errorParams}" var="_param">
    <f:param value="#{bundle[_param]}"/>
    <!-- also doesn't work: <f:param value="#{_param}"/>-->
  </ui:repeat>
</h:outputFormat>

不幸的是,参数值将被忽略,并且i18n属性的占位符不会被替换,因此渲染的输出为Welcome, {0} {1}.当使用常规"转发器时,将数组元素显示为输出文本即可.因此,outputFormat标记似乎不支持将重复作为子级使用. 该死的,太近了;)任何人都知道做我想要的事情的好方法,或者有没有支持这种功能的组件库?

Unfortunately, the param value is ignored and the placeholders of the i18n-property aren't replaced, so the rendered output is Welcome, {0} {1}. When using a "regular" repeater, displaying the array elements just as an outputtext, it works. So the outputFormat tag doesn't seem to support the use of a repeat as a child. Damn, so close ;) Anyone knows a good way to do what i want, or is there any component library supporting something like that?

推荐答案

此处的问题是ui:repeath:outputFormat的渲染时子元素,它实际上根本不支持.您希望在构建期间将多个f:param元素直接作为h:outputFormat的子元素放置.

The problem here is that ui:repeat is a render-time child of h:outputFormat which it indeed doesn't support at all. You'd like to put multiple f:param elements directly as children of h:outputFormat during build time.

c:forEach适用于此任务. JSTL核心标记(已经包含在Facelets中,因此您无需安装任何额外的JAR)在构建视图树期间(即在JSF转向 process之前)完成其工作. /render 视图树.

The c:forEach is suitable for this task. The JSTL core tags (which are already included in Facelets, so you don't need to install any extra JARs) do their job during building the view tree, right before it's JSF turn to process/render the view tree.

<html xmlns:c="http://java.sun.com/jsp/jstl/core">
...
<h:outputFormat value="#{bundle[flash.error]}" rendered="#{! empty flash.error}" class="invalid">
  <c:forEach items="#{flash.errorParams}" var="_param">
    <f:param value="#{bundle[_param]}"/>
  </c:forEach>
</h:outputFormat>

这篇关于JSF&lt; h:outputFormat&gt ;:使用数组值作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆