级联 p:selectOneMenu 模型值未设置 [英] Cascading p:selectOneMenu model value not set

查看:19
本文介绍了级联 p:selectOneMenu 模型值未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have an application where i have a cascading dropdown, a simple dropdown and a submit commandButton. i wish to show report in datatable by selecting various criteria from the dropdowns. all works fine except when i wish to select the criteria from child dropdown. it doesnt bind with its value in the managed bean(found that by debugging). here's my code: 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 代码:

managed bean code:

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

豆方法:

@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 props?我该如何解决它?(我担心的是昨天相同的代码可以工作,但今天当我运行相同的代码时,它开始困扰我).

why does the value not bind to the bean props? how do i solve it?(the matter of my concern is that the same code worked yesterday but today when i ran the same code it started troubling me).

faces-config

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

推荐答案

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

That can happen if the managed bean is been placed in the request scope by @RequestScoped. Every single HTTP request, including Ajax requests, would create a completely brand new instance of the bean, with all properties set to default. This way the <f:selectItems> list becomes empty and hence there's no value to set in the model.

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

Placing the bean in the view scope by @ViewScoped should solve this problem.

更新:您应该从faces-config.xml删除那些定义.这是旧的 JSF 1.x 注册 bean 方式的遗留物.他们只会覆盖 @ManagedBean 配置的 JSF 2.x 方式.

Update: you should remove those <managed-bean> definitions from faces-config.xml. That was a leftover from old JSF 1.x way of registering beans. They would only override the JSF 2.x way of @ManagedBean configurations.

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

Not directly related to your concrete problem, but those <navigation-case> entries are superfluous in JSF 2.x. Perhaps you were incorrectly reading old JSF 1.x books/tutorials/resources instead of JSF 2.x ones?

与具体问题无关,您确实遇到了SQL 注入getTradeReport() 方法中的攻击漏洞.您应该永远字符串连接 SQL/JPQL 字符串中的用户控制变量.改用带有 setParameter() 的参数化查询.另请参见 JSF 中的 CSRF、XSS 和 SQL 注入攻击预防.

Unrelated to the concrete problem, you've there seriously a SQL injection attack hole in getTradeReport() method. You should never string-concatenate user-controlled variables in a SQL/JPQL string. Use parameterized queries with setParameter() instead. See also CSRF, XSS and SQL Injection attack prevention in JSF.

这篇关于级联 p:selectOneMenu 模型值未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆