0

我有一个名为 Lecturer 的托管 bean。Lecturer managed bean 包括部门之间的关系。我想显示存储在数据库中的所有部门。当我注册讲师时,我需要获得其部门。

我的讲师管理 bean。我有适当的 setter 和 getter 方法。我只是为了澄清而省略它。

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;
}

我的 Licturer.xhtml 文件:

<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>

我的面孔-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>

当我删除 selectOneMenu 标签时。我的页面工作正常。

关于解决方案的任何想法?

我必须在字符串和部门类之间添加一个转换器吗?

4

1 回答 1

3

value属性p:selectOneMenu应该保存对单个部门(这是选定的部门)的引用,而不是整个列表。您需要在 bean 中为此添加一个字段,例如:

private Department selectedDepartment
// getter and setter

然后在您的 facelet 中更改 和 的 valuep:selectOneMenu属性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>

此外,您将需要一个转换器将选定的字符串映射到对象。

于 2012-04-19T09:55:42.437 回答