z22
z22

Reputation: 10083

primefaces command button action not working on cascading dropdown

I have cascading dropdown on my xhtml page and its working fine, but when I click the command button after selecting a value from the child or 2nd dropdown of the cascading dropdown, I get the following error:

sourceId=frm:sym[severity=(ERROR 2), summary=(frm:sym: Validation Error: Value is not valid), detail=(frm:sym: Validation Error: Value is not valid)]

XHTML code:

<h:form id="frm">
    <p:selectOneMenu style="width: 150px" value="#{watchBean.exchange}">
            <f:selectItem itemLabel="NSE" itemValue="nse"/>
            <f:selectItem itemLabel="BSE" itemValue="bse"/> 
            <p:ajax update="sym" listener="#{watchBean.wow}" />
        </p:selectOneMenu> 
    <p:selectOneMenu style="width: 150px" id="sym" value="#{watchBean.scripSym}">
        <f:selectItem itemLabel="select scrip" />
        <f:selectItems var="scrip" value="#{watchBean.sl}"  itemLabel="#{scrip.scripSymbol}" itemValue="#{scrip.scripSymbol}"/>
    </p:selectOneMenu> 

    <p:commandButton label="add to watch" value="add to watch" id="btnAdd" action="#{watchBean.addUserTrack()}" ajax="false"/>              
    <p:dataTable id="dt" var="track" .....>

Bean code:

@javax.faces.bean.ManagedBean
@javax.faces.bean.RequestScoped
public class watchBean {
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/StatelessWebService/StatelessWebService.wsdl")
    private StatelessWebService_Service service;

    /** Creates a new instance of watchBean */
    public watchBean() {
    }
    String uname,scripSym,exchange;
    Integer scripID;
    List<UserTrack> ut;
List<MasterScrip> sl;
    public List<MasterScrip> getSl() {
        return sl;
    }

    public void setSl(List<MasterScrip> sl) {
        this.sl = sl;
    }

    public String getExchange() {

        return exchange;
    }

    public void setExchange(String exchange) {
        sl=getAllScripByExchange(exchange);
        setSl(sl);
        this.exchange = exchange;
    }

    public void wow() {
        //sl=getAllScripByExchange((String)e.getNewValue());
        // setSl(sl);
        //FacesContext.getCurrentInstance().renderResponse();
        sl=getAllScripByExchange(exchange);
    }

    public List<UserTrack> getUt() {
        HttpServletRequest req=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        HttpSession session=req.getSession();
        this.uname=(String)session.getAttribute("uname");
        ut=getAllUserTrack(uname);
        return ut;
    }

    //getter setter
    public void addUserTrack() {
        HttpServletRequest req=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        HttpSession session=req.getSession();
        this.uname=(String)session.getAttribute("uname");
        MasterScrip sm=getScripBySymbol(scripSym);
        scripID=sm.getScripID();
        addUserTrack(uname, scripID);
    }
}

What could be causing this problem? How do I solve it?

Upvotes: 1

Views: 1470

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Change your bean from RequestScoped to ViewScoped, in that way the changes on the view will be preserved while the view exists.

EDIT:

From your comment, you forgot to update the datatable after processing your list. Change this

<p:commandButton label="add to watch" value="add to watch" id="btnAdd" action="#{watchBean.addUserTrack()}" ajax="false"/>

to add the update sentence;

<p:commandButton label="add to watch" value="add to watch" id="btnAdd" action="#{watchBean.addUserTrack()}" ajax="false" update="dt"/>

Upvotes: 2

Related Questions