質問

i'm learning JSF and beans.

I've the following code:

public class Example {

    private List<ExampleObject> listExampleObject;

    //Get and set from list...
}

public class ExampleObject extends ExampleObjectExtend {

    private String exampleAttribute;

    //Get and set from attribute.

}

public class ExampleObjectExtend {

    private String extendedAttribute;

    //Get and set from extendedAttribute..

}

I need to display a selectList with itemLabel=exampleAttribute and itemValue=extendedAttribute.

I did the following:

<h:selectOneMenu id="listExample">
    <f:selectItems value="#{myBean.listExampleObject}" var="example" itemValue="#{example.extendedAttribute}" itemLabel="#{example.exampleAttribute}"/>
</h:selectOneMenu>

The point is that the itemLabel is being shown properly, but the value attribute from the option control is being shown as like this: com.package.example.web.Example@5a05a935 (i check this value by chrome debugger and firebug)

Why could this happen?

Thank you


UPDATE: I've implemented the provided solution by Jitesh and the system now works. Thank you so much!

役に立ちましたか?

解決

Your code should work. But I think you can achieve the same by following way,

xhtml:

<h:selectOneMenu id="listExample">
            <f:selectItems value="#{myBean.ddl_example_options}"/>
</h:selectOneMenu>

JSF Managed Bean:

public class MyBean implements Serializable{

List<SelectItem> ddl_example_options;
String ddl_example;

public List<SelectItem> getDdl_example_options() {
    return ddl_example_options;
}

public void setDdl_example_options(List<SelectItem> ddl_example_options) {
    this.ddl_example_options = ddl_example_options;
}

public String getDdl_example() {
    return ddl_example;
}

public void setDdl_example(String ddl_example) {
    this.ddl_example = ddl_example;
}

private void setDdl_example(){
    ddl_example_options=new ArrayList<>();
    ddl_example_options.add(new SelectItem("1","Option-1"));
    ddl_example_options.add(new SelectItem("2","Option-2"));
}

@PostConstruct
public void init(){
    setDdl_example();
}
public MyBean() {
}    

}

他のヒント

Create method with signature public List<SelectItem>, setup SelectItem per each ExampleObject, and use this method in value attribute of f:selectItems.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top