【问题标题】:How can I create a JSF composite component that allows me to read/write to the attributes?如何创建允许我读取/写入属性的 JSF 复合组件?
【发布时间】:2015-08-08 09:17:38
【问题描述】:

我正在尝试创建一个复合组件,它允许用户在 selectOneMenu 和 selectManyListbox 之间切换。我希望切换可绑定到布尔值,并且 selectOneMenu/selectManyListbox 可绑定到页面的视图范围支持 bean 中的对象列表。

我能够创建一个可以轻松读取变量的复合组件。我只是通过 getAttributes() 获取绑定的 @FacesComponent 对象中的属性。

我该如何让这些变量可写呢?

例如,假设我有以下视图作用域 bean:

AssetSearch.java

@ManagedBean(name = "AssetSearch")
@ViewScoped
public class AssetSearch {

    private boolean toggle;
    private List<Asset> selectedList;

}

我想用一个复合组件来操作这些变量:

index.xhtml

<my:specialList toggle="#{AssetSearch.toggle}"
                selected="#{AssetSearch.selectedList}"/>

如何在我的复合组件支持 bean 中操作这两个变量?:

specialList.xhtml

<cc:interface componentType="specialList">
    <cc:attribute name="toggle" type="java.langBoolean" required="true"/>
    <cc:attribute name="selected" type="java.util.List" required="true"/>
</cc:interface/>
<cc:implementation>
    <h:selectBooleanCheckbox value=#{#cc.attrs.toggle}/>
    <h:selectOneMenu rendered="#{cc.attrs.toggle}" 
                     value="#{cc.attrs.selected}">
       ...
    <h:selectManyListbox rendered=#{! cc.attrs.toggle}"
                         value="#{cc.attrs.selected}">
       ...
</cc:implementation>

SpecialList.java

@FacesComponent(value = "specialList")
public class SpecialList extends UIInput {

    ...

}

正如我所说,使用 getAttributes() 获取这些变量非常容易,但我真的不确定如何操作它们。我确实通读了:

http://balusc.blogspot.com/2013/01/composite-component-with-multiple-input.html

我或许可以使用 getSubmitedValue/getConvertedValue 来管理 selectedList,但我还需要处理许多其他变量。

【问题讨论】:

  • 取决于问题中不清楚的具体功能要求。
  • @BalusC 对不起。我在帖子顶部添加了对我正在尝试做的事情的详细说明。
  • 除了切换(您应该将其存储在视图范围中,如上一个问题中所回答的那样),您不需要额外的逻辑来更新模型值。还是您面临问题?该组合也不一定是UIInput 的实例。
  • 好吧,例如,为了读取@FacesComponent 对象中的“toggle”属性,我使用:(Boolean) getAttributes().get(“toggle”)。我该怎么写呢?
  • 在ajax监听方法中?只需使用put()。这只是一个java.util.Map

标签: jsf composite-component


【解决方案1】:

正如我所说,使用 getAttributes() 获取这些变量非常容易,但我真的不知道如何操作它们。

来自UIComponent#getAttributes() javadoc(强调我的):

返回一个可变 Map 表示与此UIComponent 关联的属性(和属性,见下文),以属性名称(必须是字符串)为键。

因此它是可变的。您可以使用通常的Map#put() 方法。如果您想切换名为"toggle"java.lang.Boolean 属性,下面是一个示例:

getAttributes().put("toggle", getAttributes().get("toggle") != Boolean.TRUE);

【讨论】:

    猜你喜欢
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2011-01-04
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多