<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

    Selecting from a list of entities

    See http://docs.jboss.org/seam/latest/reference/en/html/controls.html

    • Use <s:selectItems> to produce a list of labeled select items from a list of entities.
    • Use <s:convertEntity> to map back and forth between the select items and the actual entity values. This is what allows you to map the value of the <h:selectOneMenu> directly to the property of the referencing entity (e.g. a property that is a many-to-one).
    <h:selectOneMenu value="#{person.continent}" required="true">                        (1)
    <s:selectItems value="#{continents.resultList}" var="continent" (2)
    label="#{continent.name}" noSelectionLabel="Please Select..."/>
    <s:convertEntity /> (3)
    </h:selectOneMenu>
    1. person is an entity that has been outjected into the conversation. It has a 'continent' property which is many-to-one association with another entity.
    2. continents is a Seam application framework 'query' object. This 'query' object should probably use a Seam-managed EntityManager because we want have the Hibernate session-in-view behavior so we don't get lazy initialization exceptions when rendering the labels, etc.
    3. s:convertEntity will convert the Continent entities into values for the HTML select, and vice versa.

    Tips

    1. To avoid LazyInitializationExceptions and/or writing extra code in your EJB/Controller bean to initialize objects, use session in view.
    2. For required fields, put required="true" on the selectOneMenu and override javax.faces.component.UIInput.REQUIRED in messages.properties (see Standard Faces Error Messages).

    Select from an enum

    This works just like selecting an entity, but <s:convertEnum/> is used instead.


    XHTML:

    <h:selectOneMenu id="marketStatus" value="#{person.status}"         (1)
    required="true">
    <s:selectItems value="#{enumLists.statusArray}" var="status" (2)
    label="#{status}"
    noSelectionLabel="Select a status..."/>
    <s:convertEnum/>
    </h:selectOneMenu>

    EnumLists.java:

    @Name("enumLists")
    @Scope(ScopeType.STATELESS)
    public class EnumLists
    {
    public Status[] getStatusArray()
    {
    return Status.values();
    }
    }
    1. person is an entity that has been outjected into the conversation. It has a 'status' property which is an enum.
    2. We need to expose the values of the enum as a list or an array, so we make a stateless POJO component with getters that returns arrays for various enums called enumLists.

    Multi-select from an enum

    Here we use a selectManyCheckbox.


    <h:selectManyCheckbox id="roles" 
    layout="pageDirection" value="#{person.roles}"
    required="true">
    <s:selectItems value="#{enumLists.roleArray}" var="role"
    label="#{role}"/>
    <s:convertEnum/>
    </h:selectManyCheckbox>

    Unfortunately, Seam's convertEnum can't handle multi selects yet. This example will yeild a strange exception:

    java.lang.IllegalArgumentException: java.util.List is not an enum type

    Luckily, it's very easy to create custom converter tags with Facelets. Here is the converter class that handles both ordinary enums and multi-selects:

    package eg;

    import javax.faces.component.*;
    import javax.faces.context.*;
    import javax.faces.convert.*;
    import javax.faces.el.ValueBinding;
    import java.util.List;
    import java.util.Collection;

    /**
    * Converter for enum multi-selects.
    * <br>User: Joshua Davis
    * Date: May 16, 2007
    * Time: 7:25:58 AM
    */
    public class EnumListConverter implements Converter
    {
    @SuppressWarnings({"unchecked"})
    public Object getAsObject(FacesContext context,
    UIComponent comp,
    String value)
    throws ConverterException
    {
    ValueBinding binding = comp.getValueBinding("value");
    Class enumType = binding.getType(context);
    if (enumType.isEnum()) // Single enum?
    return Enum.valueOf(enumType, value);
    else // List of enums.
    {
    // Find the s:selectItems so we can get the enum.
    List children = comp.getChildren();
    for (Object child : children)
    {
    if (child instanceof UIComponent)
    {
    UIComponent c = (UIComponent) child;
    ValueBinding b = c.getValueBinding("value");
    Class t = b.getType(context);
    // Array of enums: use the component type.
    if (t.isArray() && t.getComponentType().isEnum())
    {
    t = t.getComponentType();
    return Enum.valueOf(t,value);
    }
    else
    {
    Object v = b.getValue(context);
    // Collection of enum values, get the type of the first element.
    if (v instanceof Collection)
    {
    t = ((Collection) v).iterator().next().getClass();
    return Enum.valueOf(t,value);
    }
    }
    }
    }
    throw new ConverterException("Unable to find selectItems with enum values!");
    }
    }

    public String getAsString(FacesContext context,
    UIComponent component,
    Object object)
    throws ConverterException
    {
    if (object == null) {
    return null;
    }

    return ((Enum) object).name();
    }

    }
    Retrieved from "
    主站蜘蛛池模板: 99免费在线视频| 精品一区二区三区无码免费视频 | 亚洲成年人免费网站| 99视频全部免费精品全部四虎| 亚洲香蕉网久久综合影视| 黄色免费在线网址| 黄页网址大全免费观看12网站| 免费av欧美国产在钱| 亚洲电影免费在线观看| 一区二区三区在线免费 | 免费一级毛片在线播放放视频| 国产做床爱无遮挡免费视频| 69天堂人成无码麻豆免费视频| 亚洲熟妇av午夜无码不卡 | 亚洲视频中文字幕在线| 91精品成人免费国产片| 青柠影视在线观看免费高清| 成年网站免费入口在线观看| 亚洲av色福利天堂| 亚洲欧洲日韩不卡| 亚洲爆乳无码专区www| 妞干网免费视频观看| 国产日韩AV免费无码一区二区| 最近在线2018视频免费观看| 免费观看四虎精品成人| 91精品导航在线网址免费| 亚洲砖码砖专无区2023| 亚洲欧洲日韩国产一区二区三区| 91免费人成网站在线观看18| 日本在线看片免费人成视频1000| 狠狠色香婷婷久久亚洲精品| 又黄又大又爽免费视频| 亚洲愉拍99热成人精品热久久 | 国产成人免费手机在线观看视频 | 亚洲一区二区三区免费观看| 亚洲欧洲自拍拍偷综合| 亚洲国产成人精品久久久国产成人一区二区三区综 | 一区在线免费观看| 国产一区二区免费| 国产性生交xxxxx免费| 国产成人精品一区二区三区免费|