JSF 多个组合框问题

发布于 2024-11-30 12:16:17 字数 3584 浏览 0 评论 0原文

我对多个组合框有疑问,而且我也在使用 primefaces。我的代码如下;

xhtml页面:

<f:view xmlns="http://www.w3.org/19399/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:pm="http://primefaces.prime.com.tr/mobile"
contentType="text/html">

<pm:page title="Title">


    <pm:view id="organizationView" swatch="b">
        <pm:header title="Title">
            <f:facet name="right">
                <pm:button value="Logout" icon="minus" />
            </f:facet>
        </pm:header>

        <pm:content>
            <h:form id="organizationForm" prependId="false">
                <p:separator />

                <h:selectOneMenu id="company" value="#{myView.selected}" >
                    <f:selectItem value="#{null}" itemLabel="Select company..." noSelectionOption="true" />
                    <f:selectItems value="#{myView.companyList}" 
                        var="company" itemLabel="#{company.name}" itemValue="#{company.companyId}"/>
                    <p:ajax immediate="true" update="organization" listener="#{myView.loadOrganization}" />
                </h:selectOneMenu>

                <h:selectOneMenu id="organization" value="#{myView.organization.orgId}">
                    <f:selectItem itemLabel="Select Organization..." itemValue="" />
                    <f:selectItems value="#{myView.orgList}" 
                        var="organization" itemLabel="#{organization.name}" itemValue="#{organization.orgId}"/>
                </h:selectOneMenu>

                <p:separator />
            </h:form>
        </pm:content>
    </pm:view>

</pm:page>

和 backing-bean 是这样的:

@Component("myView")
@Scope("view")

public class MyView implements Serializable {

private Long selected;

private List<Company> companyList;
private Company company;
private Organization organization;
private List<Organization> orgList;

@Autowired
private MyViewService myViewService;

public MyView() {

}

@PostConstruct
public void init(){
    companyList = myViewService.findAllCompany();

}

public void loadOrganization() {
    if(selected != null && selected >0L)
        orgList = myViewService.findGroupsByCompanyId(selected);
}

public void setCompany(Company company) {
    this.company = company;
}

public Company getCompany() {
    return company;
}

public List<Company> getCompanyList() {
    return companyList;
}

public void setCompanyList(List<Company> companyList) {
    this.companyList = companyList;
}

public List<Organization> getOrgList() {
    return orgList;
}

public void setOrgList(List<Organization> orgList) {
    this.orgList = orgList;
}

public Organization getOrganization() {
    return organization;
}

public void setOrganization(Organization organization) {
    this.organization = organization;
}

public myViewService getMyViewService() {
    return myViewService;
}

public void setMyViewService(MyViewService myViewService) {
    this.myViewService = myViewService;
}

public Long getSelected() {
    return selected;
}

public void setSelected(Long selected) {
    this.selected = selected;
}

}

当我更改公司组合框时,它调用 loadOrganization() 方法,但 selected 值设置为 null。我如何从公司组合框中获取选定值? 谢谢大家的帮助。

I have a problem about multiple combobox, also i am using primefaces. My code is like below;

xhtml page:

<f:view xmlns="http://www.w3.org/19399/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:pm="http://primefaces.prime.com.tr/mobile"
contentType="text/html">

<pm:page title="Title">


    <pm:view id="organizationView" swatch="b">
        <pm:header title="Title">
            <f:facet name="right">
                <pm:button value="Logout" icon="minus" />
            </f:facet>
        </pm:header>

        <pm:content>
            <h:form id="organizationForm" prependId="false">
                <p:separator />

                <h:selectOneMenu id="company" value="#{myView.selected}" >
                    <f:selectItem value="#{null}" itemLabel="Select company..." noSelectionOption="true" />
                    <f:selectItems value="#{myView.companyList}" 
                        var="company" itemLabel="#{company.name}" itemValue="#{company.companyId}"/>
                    <p:ajax immediate="true" update="organization" listener="#{myView.loadOrganization}" />
                </h:selectOneMenu>

                <h:selectOneMenu id="organization" value="#{myView.organization.orgId}">
                    <f:selectItem itemLabel="Select Organization..." itemValue="" />
                    <f:selectItems value="#{myView.orgList}" 
                        var="organization" itemLabel="#{organization.name}" itemValue="#{organization.orgId}"/>
                </h:selectOneMenu>

                <p:separator />
            </h:form>
        </pm:content>
    </pm:view>

</pm:page>

and backing-bean is like this:

@Component("myView")
@Scope("view")

public class MyView implements Serializable {

private Long selected;

private List<Company> companyList;
private Company company;
private Organization organization;
private List<Organization> orgList;

@Autowired
private MyViewService myViewService;

public MyView() {

}

@PostConstruct
public void init(){
    companyList = myViewService.findAllCompany();

}

public void loadOrganization() {
    if(selected != null && selected >0L)
        orgList = myViewService.findGroupsByCompanyId(selected);
}

public void setCompany(Company company) {
    this.company = company;
}

public Company getCompany() {
    return company;
}

public List<Company> getCompanyList() {
    return companyList;
}

public void setCompanyList(List<Company> companyList) {
    this.companyList = companyList;
}

public List<Organization> getOrgList() {
    return orgList;
}

public void setOrgList(List<Organization> orgList) {
    this.orgList = orgList;
}

public Organization getOrganization() {
    return organization;
}

public void setOrganization(Organization organization) {
    this.organization = organization;
}

public myViewService getMyViewService() {
    return myViewService;
}

public void setMyViewService(MyViewService myViewService) {
    this.myViewService = myViewService;
}

public Long getSelected() {
    return selected;
}

public void setSelected(Long selected) {
    this.selected = selected;
}

}

when i change company combobox it calls loadOrganization() method, but selected value is set to null. How can i get selected value from company combobox?
Thx all for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪淡了忧伤 2024-12-07 12:16:17

如果您希望在使用提交的值更新模型值后调用侦听器,请从 中删除 immediate="true" 属性。

Remove the immediate="true" attribute from <p:ajax> if you want the listener to be invoked after that the model value is been updated with the submitted value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文