【问题标题】:Using h:outputFormat to message-format the f:selectItems of a h:selectOneRadio使用 h:outputFormat 对 h:selectOneRadio 的 f:selectItems 进行消息格式化
【发布时间】:2010-05-17 20:59:43
【问题描述】:

我在使用h:selectOneRadio 时遇到了一些问题。我有一个需要显示的正在返回的对象列表。我正在尝试这样的事情:

<h:selectOneRadio id="selectPlan" layout="pageDirection">
    <f:selectItems value="#{detailsHandler.planList}" />
</h:selectOneRadio>

planList 是一个计划列表。计划定义为:

public class Plan {
    protected String id;
    protected String partNumber;
    protected String shortName;
    protected String price;
    protected boolean isService;
    protected boolean isOption;  
    //With all getters/setters
}

每个单选按钮必须出现的文本实际上是在一个属性文件中,我需要在文本中插入参数来填充 bean 中的一些值。例如我的属性文件中的文本是:

plan_price=The price of this plan is {0}.

我希望做这样的事情:

<f:selectItems value="<h:outputFormat value="#{i18n.plan_price}">
    <f:param value="#{planHandler.price}">
</h:outputFormat>" />

通常,如果它不是h:selectOneRadio 组件,如果它只是文本,我使用h:outputFormatf:param 标记在我的.property 文件中显示消息,上面称为i18n,并插入一个参数在支持 bean 中。在这里这不起作用。有谁知道我该如何处理这个问题?

我收到了一份计划列表,每个计划都有自己的价格,要显示的文本保存在属性文件中。非常感谢任何帮助。

谢谢!


我现在能够按照以下建议解决上述问题。但现在我有另一个问题。 每个单选按钮项必须显示如下:

Click **here**  to see what is included. The cost is XX.

以上是每个单选按钮显示的内容。 “这里”需要是一个用户可以点击的超链接,并且应该弹出一个包含更多信息的对话框。我可以显示上面的句子但是如何使“这里”可以点击?

由于上面是显示的内容,因此返回的是 SelectItem(Object value, String label) 的标签。

非常感谢任何想法。

【问题讨论】:

    标签: jsf formatting


    【解决方案1】:

    传递给&lt;f:selectItems /&gt;的值必须javax.faces.model.SelectItem类型的列表或数组。

    您可以在 SelectItem 的构造函数中设置输出标签。我想您可以从支持 bean 访问您的属性文件。获取 SelectItems 的方法如下所示:

    public List<SelectItem> getPlanItems() {
      List<SelectItem> list = new ArrayList<SelectItem>();
      for (Plan plan : planList) {
        String label = buildPlanPriceLabel(plan.getPrice());
        list.add(new SelectItem(plan, label));
      }
      return list;
    }
    

    buildPlanPriceLabel留给读者作为练习。

    【讨论】:

    • 正确。但是,Map&lt;Object, String&gt; 也仅适用于 JSF 1.x。在 JSF 2.x 中,您甚至可以使用 OP 尝试使用的 javabean 列表或数组,但是您还必须设置 varitemLabelitemValue 属性,而这在此处缺少。另请参阅 this answer 以获取示例。
    • 感谢您的 cmets 我已按照这些步骤操作,并且能够在我的列表中显示信息。但是现在我面临另一个挑战,我在上面编辑了我的问题。如果您对此有任何想法,请告诉我。
    • 根据您的要求,h:selectOneRadio 是完全错误的组件!您应该使用 ui:repeat 或 a4j:repeat。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    相关资源
    最近更新 更多