级联p:未设置selectOneMenu模型值

时间:2012-06-01 17:53:50

标签: jsf primefaces selectonemenu updatemodel ajax-update

我有一个应用程序,我有一个级联下拉列表,一个简单的下拉列表和一个提交命令按钮。我希望通过从下拉列表中选择各种标准来显示数据表中的报告。一切正常,除非我想从儿童下拉菜单中选择标准。它没有与托管bean中的值绑定(通过调试发现)。这是我的代码: XHTML:

<h:outputText value="Exchange"/>
            <p:selectOneMenu style="width: 150px" value="#{reportBean.exchange}">
                <f:selectItem itemLabel="--select--" itemValue="--select--"/>
                <f:selectItem itemLabel="NSE" itemValue="nse"/>
                <f:selectItem itemLabel="BSE" itemValue="bse"/> 
                <p:ajax update="sym" listener="#{reportBean.wow}"/>
            </p:selectOneMenu>             
        <h:outputText value="Scrip Symbol :"/>
        <p:selectOneMenu value="#{reportBean.scripID}" id="sym" style="width: 100px">
            <f:selectItem itemLabel="All" itemValue="0"/>
            <f:selectItems var="scrip" value="#{reportBean.sl}" itemLabel="#{scrip.scripSymbol}" itemValue="#{scrip.scripID}"/>
        </p:selectOneMenu>

        <h:outputText value="Order Type :"/>
        <p:selectOneMenu style="width: 100px" value="#{reportBean.orderType}">
            <f:selectItem itemLabel="All" itemValue="All"/>
            <f:selectItem itemLabel="Buy" itemValue="B"/>
            <f:selectItem itemLabel="Sell" itemValue="S"/>
        </p:selectOneMenu>
            <p:commandButton ajax="true" update="dt" value="Submit" action="#{reportBean.getTradeByUserName()}" type="submit"/>
        </h:panelGrid>
        <p:dataTable id="dt" var="trade" widgetVar="scripTab" 
                     emptyMessage="No trade found with given criteria"  
                     value="#{reportBean.tradeList}"
                 paginator="true" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                 rowsPerPageTemplate="3,5,10" rows="3" >


        <p:column>....

托管bean代码:

    public void wow()
{    
        sl=getAllScripByExchange(exchange);
}

    public Integer getScripID() {
        return scripID;
    }

    public void setScripID(Integer scripID) {
        MasterScrip sm =getScripByID(scripID);
        if(sm!=null)
        this.scripSymbol=sm.getScripSymbol();
        this.scripID = scripID;
    }

    public List<TradeStock> getTradeList() {
        return tradeList;
    }

    public void setTradeList(List<TradeStock> tradeList) {
        this.tradeList = tradeList;
    }
     public List<service.TradeStock> getTradeByUserName()
    {
         HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        HttpSession session = request.getSession(true);
        String uname = session.getAttribute("uname").toString();   
        tradeList= getTradeReport(scripID, uname, exchange, orderType);
        return tradeList;
    }

bean方法:

@Override
public Collection<TradeStock> getTradeReport(Integer scripID, String uname, String exchange, String tradeType) {

    String strQuery = null;
    strQuery = "Select t from TradeStock t where t.userName.userName = :userName ";
        if(! exchange.equalsIgnoreCase("All"))
    {
        strQuery += " and t.scripID.exchange = '" + exchange + "'";
    }         
    if(scripID>0)
    {
        strQuery += " and t.scripID.scripID = " + scripID;
    }
     if(! tradeType.equalsIgnoreCase("All"))
    {
        strQuery += " and t.orderID.buySell = '" + tradeType + "'";
    }

    Collection<TradeStock> c = em.createQuery(strQuery).setParameter("userName",uname).getResultList();

    return c;
}

为什么值不绑定到bean道具?我该如何解决呢?(我关心的问题是昨天同样的代码工作但今天当我运行相同的代码时,它开始困扰我)。

编辑:面-配置

    <faces-config version="2.0"
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<managed-bean>
<managed-bean-name>userReportBean</managed-bean-name>    
<managed-bean-class>UserReportBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/adminpages/editScrip.xhtml</from-view-id>    
<navigation-case>
    <from-action>#{manageScrip.updateScrip}</from-action>
    <from-outcome>success</from-outcome>
    <to-view-id>/adminpages/manageScrip.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/adminpages/addScrip.xhtml</from-view-id>    
<navigation-case>
    <from-action>#{manageScrip.addScrip}</from-action>
    <from-outcome>success</from-outcome>
    <to-view-id>/adminpages/manageScrip.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/adminpages/manageScrip.xhtml</from-view-id>    
<navigation-case>
    <from-action>#{manageScrip.returnEdit}</from-action>
    <from-outcome>success</from-outcome>
    <to-view-id>/adminpages/editScrip.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>
    <managed-bean>
        <managed-bean-name>equityBean</managed-bean-name>
        <managed-bean-class>beans.equityBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    <managed-bean>
        <managed-bean-name>reportBean</managed-bean-name>
        <managed-bean-class>beans.reportBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>

1 个答案:

答案 0 :(得分:2)

如果托管bean被@RequestScoped放置在请求范围内,则会发生这种情况。每个HTTP请求(包括Ajax请求)都会创建一个全新的bean实例,所有属性都设置为default。这样,<f:selectItems>列表变为空,因此在模型中没有设置值。

通过@ViewScoped将bean放在视图范围内可以解决这个问题。

另见:


更新:您应该删除来自<managed-bean>的{​​{1}}定义。这是旧JSF 1.x注册bean的方式的遗留问题。它们只会覆盖faces-config.xml配置的JSF 2.x方式。

与您的具体问题没有直接关系,但JSF 2.x中的那些@ManagedBean条目是多余的。也许你错误地阅读了旧的JSF 1.x书籍/教程/资源而不是JSF 2.x的那些?


对具体问题

无关,您在<navigation-case>方法中认真对待SQL injection attack hole。您应该永远在SQL / JPQL字符串中串联连接用户控制的变量。使用参数化查询代替getTradeReport()。另请参阅CSRF, XSS and SQL Injection attack prevention in JSF