0

i have a managed bean called Lecturer. Lecturer managed bean includes a relation between departments. I want to show all departments stored in the database. when i register a lecturer i need to get its department.

My Lecturer managed bean. i have proper setter and getter methods. i just omit it for clarification.

public class Lecturer 
{

    private String name;
    private String surname;

    private String email;
    private String username;
    private String password;


    private List<Department> departments;

    private LecturerService lecturerService;
    private DepartmentService departmentService;
}

my Licturer.xhtml file:

<h:form>
<f:view>
<p:panelGrid columns="2">  

<f:facet name="header">Lecturer Registration Form</f:facet> 

<h:outputLabel for="name" value="Name :" />  
<p:inputText id="name" value="#{Lecturer.name}" label="Name" required="true" />

<h:outputLabel for="surname" value="Surname :" />  
<p:inputText id="surname" value="#{Lecturer.surname}" label="Surname" required="true" />

<h:outputLabel for="department" value="Department :" /> 


<p:selectOneMenu value="#{Lecturer.departments}" effect="fade" editable="true" var="p" >
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{Lecturer.departments}" var="lec"
itemLabel="#{lec.name}" itemValue="#{lec.name}"/>
</p:selectOneMenu>


<h:outputLabel for="email" value="Email :" />  
<p:inputText id="email" value="#{Lecturer.email}" label="Email" required="true" />

<h:outputLabel for="username" value="User Name :" />  
<p:inputText id="username" value="#{Lecturer.username}" label="Email" required="true" />

<h:outputLabel for="password" value="Password :" />  
<p:inputText id="password" value="#{Lecturer.password}" label="Password" required="true" />

<f:facet name="footer">

 <p:commandButton type="submit"
                id="lecturer"
                action="#{Lecturer.registerLecturer}"
                value="Register" icon="ui-icon-disk">

                </p:commandButton>

</f:facet>

</p:panelGrid>
</f:view>
</h:form>

my faces-config.xml

<managed-bean>
        <managed-bean-name>Lecturer</managed-bean-name>
        <managed-bean-class>com.studinfo.controller.Lecturer</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>

        <managed-property>
            <property-name>departmentService</property-name>
            <property-class>com.studinfo.services.DepartmentService</property-class>
            <value>#{DepartmentService}</value>
        </managed-property>

        <managed-property>
            <property-name>lecturerService</property-name>
            <property-class>com.studinfo.services.LecturerService</property-class>
            <value>#{LecturerService}</value>
        </managed-property>
    </managed-bean>

when i remove the selectOneMenu tag. my page works properly.

any idea about the solution?

Do i must add a converter between string and Department class?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
erencan
  • 3,725
  • 5
  • 32
  • 50

1 Answers1

3

The value attribute of p:selectOneMenu should hold the reference to a single department (this is the selected department) and not the whole list. You need to add a field for this in your bean, e.g.:

private Department selectedDepartment
// getter and setter

Then in your facelet change the value attribute of p:selectOneMenu and the f:selectItems:

<p:selectOneMenu value="#{Lecturer.selectedDepartment}" effect="fade" 
                 editable="true" var="p" >
  <f:selectItem itemLabel="Select One" itemValue="" />
  <f:selectItems value="#{Lecturer.departments}" var="lec"
                 itemLabel="#{lec.name}" itemValue="#{lec}"/>
</p:selectOneMenu>

Furthermore you will need a converter to map selected strings to objects.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • @erencan: Have a close look at http://www.primefaces.org/showcase/ui/selectOneMenu.jsf (See the second example, it's `city` and `cities`) – MartinK Apr 19 '12 at 09:57
  • @MartinK yes i have looked at it. I think my problem is missing converter. i try to add a converter between my Department class and String. – erencan Apr 19 '12 at 10:01
  • i also want to show all departments stored in the database. user can select one of them for new lecturer registration. therefore, i must use list of departments on the page. – erencan Apr 19 '12 at 10:02
  • Yes, of course. You can show them all in the selectItems but the selection is only a single departement. – Matt Handy Apr 19 '12 at 10:05