0

I have the following addService.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">

<h:head><title>Add Service</title></h:head>
<h:body>
    <h:form>
        <p:outputLabel for="name" value="Service Name" />
        <p:inputText id="name" value="#{serviceMB.name}"></p:inputText>

        <p:outputLabel for="categoryCompId" value="Service Category" />
        <p:selectOneMenu id="categoryCompId" value="#{serviceMB.selectedCategory}" >
            <f:selectItem itemLabel="Select Category" itemValue="" noSelectionOption="true" />
            <f:selectItems value="#{serviceMB.categories}" var="category" itemLabel="#{category.name}" itemValue="#{category}"/>
        </p:selectOneMenu>
        <p:commandButton id="saveService" value="Save" action="#{serviceMB.saveServiceAndExit}"/>
    </h:form>
</h:body>

And the following managed bean

@Component
@Controller
@Scope(value = "request")
@ManagedBean(name="serviceMB")
@RequestScoped
public class ServicesManagedBean implements Serializable {
    private Category selectedCategory;
    private String name;
    public Category getSelectedCategory() {
        return selectedCategory;
    }
    public void setSelectedCategory(Category selectedCategory) {
        this.selectedCategory = selectedCategory;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Category> getCategories() {
        return categoryService.getCategories();
    }

}

The setSelectedCategory() method doesn't be called and selectedCategory field is always null, although I'm using it in the <p:selectOneMenu. Is there anything wrong in my code?

I've added a converter, but when I use Object selectedObject = ((HtmlSelectOneMenu) arg1).getValue(); it returns null also.

user3377708
  • 151
  • 6
  • 17
  • You have redundant annotations on the managed bean class. Why are they there? – Tiny Nov 08 '14 at 08:14
  • I need to inject a Spring service bean into the managed bean. so, I need this managed bean to be visible on Spring container as well. – user3377708 Nov 08 '14 at 08:58

1 Answers1

1

You can pass only Strings and basic types in JSF inputs. For complex Objects you need a converter:

<p:selectOneMenu id="categoryCompId" value="#{serviceMB.selectedCategory}" 
    converter="categoryConverter">
    <f:selectItem itemLabel="Select Category" itemValue="#{null}" noSelectionOption="true" />
    <f:selectItems value="#{serviceMB.categories}" var="category" itemLabel="#{category.name}" itemValue="#{category}"/>
</p:selectOneMenu>
Michele Mariotti
  • 7,372
  • 5
  • 41
  • 73
  • I've added a converter, but when I use Object selectedObject = ((HtmlSelectOneMenu) arg1).getValue(); inside the "getAsObject" method, it returns null also. – user3377708 Nov 08 '14 at 08:10
  • That's not how a converter should be implemented. Look here: [http://stackoverflow.com/questions/8001079/how-create-a-custom-coverter-in-jsf-2](http://stackoverflow.com/questions/8001079/how-create-a-custom-coverter-in-jsf-2) – Michele Mariotti Nov 09 '14 at 10:23