从第一个下拉列表jsp中获取值



我有我的第一个下拉列表id="bfnsCode",我想将该值用于我的下一个下拉列表id="taxtCode",以便能够根据第一个下拉显示新的值集。这是我的代码:

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

如您所见,我在下拉税中使用request.getParameter("bfnsCode")调用该值,但它给了我一个空值。

ListBIRFormNo.javac:forEach的servlet)

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

}

我认为您可能误解了请求处理生命周期。request.getParameter("bfnsCode")具有非null值的唯一方法是,如果名为bfnsCode的参数与当前请求一起发送。这里的情况并非如此(据我所知),因为您的所有代码都在单个请求的上下文中执行。因此,是的,bfnsCode在您检查时将为空。

如果您希望一个选择框在同一页面中响应另一个,这通常/最好使用JavaScript来完成。您希望在第一个<select>框上有一个onchangeonkeyup(或两者都有)事件处理程序,并希望实现它,以便它从第一个框中获取值,然后使用它更新第二个框(通过进行AJAX调用加载相关数据,或通过从页面设置的某个缓存中加载正确的数据集)。这听起来比实际情况更复杂(尤其是如果您使用的是像jQuery这样的JavaScript框架),但您无法像当前方法那样使用纯服务器端代码来解决问题。

请考虑重构您的实现,这样它就不会将Java代码直接嵌入JSP页面中。您可以将与检查用户和查询表单列表相关的业务逻辑移动到Servlet实现(或Struts提供的类似Servlet的等效结构;我相信是Action),然后使用<c:forEach>标记将选项附加到<select>元素(即Sotirios在评论中所说的)。

例如,在您的Servlet/Action代码中,您可以按照当前状态设置birtypelist,然后执行:

if(user.getRcoCode() != null){
    birtypelist =birdao.getAllBirFormNumber();
}else{
    //no-op
}
//expose 'birtypelist' as an attribute
request.setAttribute("birtypelist", birtypelist);  

然后在您的JSP页面中,您可以使用:

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

这将产生与您目前采用的方案方法相当的结果。

最新更新