开发者

How to set default value to <h:selectOneMenu>

开发者 https://www.devze.com 2023-03-02 16:02 出处:网络
I am trying to set default value to h:selectOneMenu. But, It is not working. This is my code index.xhtml

I am trying to set default value to h:selectOneMenu. But, It is not working.

This is my code

index.xhtml

<h:body>
        <h:form id="test">
        <h:selectOneMenu value="#{selectMenuBean.selectedItem}"
                         title="select version"
                         onchange="submit()"
                         disabled="false" id="combo">

            <f:selectItems value="#{selectMenuBean.selectItems}" />
        </h:selectOneMenu>

        </h:form>
    </h:body>

BackingBean

private String selectedItem;
private List selectItems;
private int version=3;

public List getSelectItems() {

    List<Version> selectedItems = ExportDao.getVersionsList();
    System.out.println("List size: "+selectedItems.size());
    selectItems = new ArrayList();

    for (Version v1 : selectedItems) {
        String DATE_开发者_开发百科FORMAT = "yyyy-MM-dd HH:mm";
        //Create object of SimpleDateFormat and pass the desired date format.
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        selectItems.add(new SelectItem(v1.getVersion(), "V" + v1.getVersion() + "/" + sdf.format(v1.getDate())));
        if(version = v1.getVersion()) // I have to check the version and set the matching version as selected.
        selectedItem = "V" + v1.getVersion() + "/" + sdf.format(v1.getDate());

    }
    return selectItems;
}


You're setting the selectedItem with the item label instead of the item value.

Replace

selectedItem = "V" + v1.getVersion() + "/" + sdf.format(v1.getDate());

by

selectedItem = v1.getVersion();


A few possible solutions:

1) Set the type of selectItems to SelectItem[] instead of and untyped List.

or 2) Try setting the var, itemValue and itemLabel attributes of the selectItems like below, and put actual Version objects in your list.

or my favorite, 3) Make a VersionConverter that knows how to convert a Version object from and to a String. Example below if your Version object is persisted in a database and has an Id. After this is constructed, your selectedItem and List selectItems should have the typ Version (and List), not String. JSF will handle the conversion by itself.

@FacesConverter(forClass=Version.class)
public class VersionConverter implements Converter{

    public VersionConverter() {
    }

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        try {
            // Get an EJB that can fetch the Version from a DB. Alternativly, do whatever you need to get your object from a string.
            InitialContext ic = new InitialContext();

            MyDao myDao = (MyDao)ic.lookup(String.format("java:global/%s/MyBean", (String)ic.lookup("java:module/ModuleName")));
            return myDao.findEntity(Version.class, getKey(value));
        } catch (NamingException e) {
            return null;
        }
    }

    Long getKey(String value) {
        Long key;
        key = Long.valueOf(value);
        return key;
    }

    String getStringKey(Long value) {
        StringBuilder sb = new StringBuilder();
        sb.append(value);
        return sb.toString();
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Version) {
            Version e = (Version) object;
            return getStringKey(e.getId());
        }
        else
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Version.class.getName());
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号