如何创建允许我读取/写入属性的JSF复合组件? [英] How can I create a JSF composite component that allows me to read/write to the attributes?

查看:67
本文介绍了如何创建允许我读取/写入属性的JSF复合组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a composite component which allows a user to toggle between a selectOneMenu and selectManyListbox. I want the toggle to be bindable to a boolean value and the selectOneMenu/selectManyListbox bindable to a list of objects in the view-scoped backing bean of the page.

我能够创建一个复合组件,该组件可以足够容易地读取变量.我只是通过 getAttributes()在绑定的 @FacesComponent 对象中获取属性.

I'm able to create a composite component that can read the variables easily enough. I just get the attributes within the bound @FacesComponent object via getAttributes().

我如何使那些变量可写?

How do I go about making those variables writable though?

例如,说我有以下视图作用域的bean:

For example, say I have the following view scoped bean:

AssetSearch.java

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

    private boolean toggle;
    private List<Asset> selectedList;

}

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

And I want to manipulate those variables with a composite component:

index.xhtml

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

如何在复合组件支持bean中操纵这两个变量?

How can I manipulate these 2 variables within my composite component backing 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()来获取这些变量非常容易,但是我真的不确定如何操作它们.我确实读过:

As I said, its pretty easy to get these variables with getAttributes() but I'm really not sure how to manipulate them. I did read through:

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

我可能可以使用 getSubmitedValue/getConvertedValue 来管理 selectedList ,但是我还有很多其他变量需要处理.

I could probably use getSubmitedValue/getConvertedValue to manage the selectedList but I have a bunch of other variables I need to manipulate as well.

推荐答案

正如我所说,使用 getAttributes()来获取这些变量非常容易,但是我真的不确定如何操作它们.

As I said, its pretty easy to get these variables with getAttributes() but I'm really not sure how to manipulate them.

UIComponent#getAttributes() javadoc (重点是我):

From the UIComponent#getAttributes() javadoc (emphasis mine):

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

Return a mutable Map representing the attributes (and properties, see below) associated wth this UIComponent, keyed by attribute name (which must be a String).

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

It's thus mutable. You can use the usual Map#put() method on it. Provided that you want to toggle a java.lang.Boolean attribute named "toggle", here's an example:

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

这篇关于如何创建允许我读取/写入属性的JSF复合组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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