简体   繁体   中英

Getting value from 1st drop down list jsp

I have my 1st drop down list id="bfnsCode" , I want to use that value to my next drop down list id="taxtCode" to be able to show new set of values based on the 1st drop down. here is my code:

<div class="BIR" style="display: none;">
            <div>
            <label style="font-size: 17px;">BIR-Form Number</label><br>         
                <select name="bfnsCode" id="bfnsCode" class="sel" style="width: 245px; margin-left: 0;">
                    <option selected="selected" value=""></option>
                    <%
                        TblBIRFormNoDAO birdao = DAOFactory.getDaoManager(TblBIRFormNo.class);

                        HttpSession live = request.getSession(true);
                        TblUserInformation user = (TblUserInformation) live.getAttribute("user");

                        List<TblBIRFormNo> birtypelist =  null;                     

                        if(user.getRcoCode() != null){
                            birtypelist =birdao.getAllBirFormNumber();
                        }else{

                        }

                        String birtypeoptions = "";

                        if( birtypelist!=null) {
                            if( birtypelist.size()>0 ) {
                            for(int i=0; i<birtypelist.size();i++) {
                            TblBIRFormNo taxtype = (TblBIRFormNo) birtypelist.get(i);
                            birtypeoptions += "<option value='"+taxtype.getBfnsCode()+"'>"+taxtype.getBfnsCode()+"</option>";                                       
                            taxtype = null;
                                }
                            }
                        }

                        birdao = null;
                        birtypelist = null;
                        %>
                        <%=birtypeoptions%>

                </select>   
            <br><br>
            <label style="font-size: 17px;">Tax Type</label><br>            
                <select name="taxtCode" id="taxtCode" class="sel" style="margin-left: 0;">
                    <option selected="selected" value=""></option>
                    <%
                        TblTaxTypeDAO taxdao = DAOFactory.getDaoManager(TblTaxType.class);

                        List<TblTaxType> taxtypelist =  null;

                        String tax = request.getParameter("bfnsCode");
                        Debugger.print("test : "+tax);

                        if(tax != null){
                            taxtypelist = taxdao.findAlltaxtCode(tax);
                        }else{
                            taxtypelist = taxdao.getAllTaxTypes();
                        }

                        String taxtypeoptions = "";

                        if( taxtypelist!=null) {
                            if( taxtypelist.size()>0 ) {
                            for(int i=0; i<taxtypelist.size();i++) {
                            TblTaxType taxtype = (TblTaxType) taxtypelist.get(i);
                            taxtypeoptions += "<option value='"+taxtype.getTaxtCode()+"'>"+taxtype.getTaxtCode()+"</option>";                                       
                            taxtype = null;
                                }
                            }
                        }

                        taxdao = null;
                        taxtypelist = null;
                        %>
                        <%=taxtypeoptions%>         

                </select>   

As you can see, I call the value using request.getParameter("bfnsCode") in drop down tax but it gives me a null value.

ListBIRFormNo.java (servlet for c:forEach )

public class ListBIRFormNo extends HttpServlet {
private static final long serialVersionUID = 1L;

private List<TblBIRFormNo> birtypelist;

public List<TblBIRFormNo> getTblBIRFormNo() {
    return birtypelist;
}
public void setTblBIRFormNo(List<TblBIRFormNo> birtypelist) {
    this.birtypelist = birtypelist;
}
private HttpServletRequest request;

public void setServletRequest(HttpServletRequest request){
    this.request = request;
}

public String execute(){
    Debugger.border();
    try{
        TblBIRFormNoDAO birdao = DAOFactory.getDaoManager(TblBIRFormNo.class);

        HttpSession live = request.getSession(true);
        TblUserInformation user = (TblUserInformation) live.getAttribute("user");

        if(user.getRcoCode() != null){
            birtypelist =birdao.getAllBirFormNumber();          
        }else{
            //no-op
        }

        //expose 'birtypelist' as an attribute
        request.setAttribute("birtypelist", birtypelist); 

    }catch(Exception e){
        e.printStackTrace();
        Debugger.print(" EXCEPTION :"+e.getStackTrace());
        Debugger.endDebug(this.getClass().toString());
        Debugger.border();
    }
    Debugger.border();
    return null;
}

}

I think you may be misunderstanding the request processing lifecycle. The only way for request.getParameter("bfnsCode") to have a non-null value is if a parameter called bfnsCode was sent with the current request. That's not the case you have here (as far as I can tell), as all of your code executes within the context of a single request. So yes, bfnsCode will be null at the time you check for it.

If you want one select box to respond to another within the same page, that is something that is usually/best accomplished with JavaScript. You'd want an onchange or onkeyup (or both) event handler on the first <select> box, and you'd want to implement it so that it gets the value from the first box and then uses it to update the second box (either by making an AJAX call to load in the relevant data, or by loading in the correct set of data from some cache that you set up with the page). This sounds more complicated than it actually is (especially if you are using a JavaScript framework like jQuery), but you cannot solve the problem using purely server-side code as your current approach does.

Please consider refactoring your implementation so that it doesn't embed Java code directly inside of a JSP page. You can move the business logic related to checking for a user and querying for the list of forms to a Servlet implementation (or equivalent Servlet-like construct provided by Struts; an Action I believe it is), and then use a <c:forEach> tag to append options to your <select> element (ie what Sotirios said in his comment).

For example, in your Servlet / Action code you could set up birtypelist as you are currently, and then do:

if(user.getRcoCode() != null){
    birtypelist =birdao.getAllBirFormNumber();
}else{
    //no-op
}

//expose 'birtypelist' as an attribute
request.setAttribute("birtypelist", birtypelist);  

...and then in your JSP page, you can use:

<select name="bfnsCode" id="bfnsCode" class="sel" style="width: 245px; margin-left: 0;">
    <option selected="selected" value=""></option>
    <c:forEach var="taxtype" items="${birtypelist}">
        <option value="${taxType.bfnsCode}">${taxType.bfnsCode}</option>
    </c:forEach>
</select>

That will give comparable results to the programmatic approach that you currently have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM