簡體   English   中英

從1st下拉列表中獲取價值

[英]Getting value from 1st drop down list 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.java (用於c: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")具有非空值的唯一方法是是否與當前請求一起發送了名為bfnsCode的參數。 (據我所知)這里不是這種情況,因為所有代碼都在單個請求的上下文中執行。 因此,是的, bfnsCode在您檢查時將為null。

如果您希望一個選擇框在同一頁面中對另一個做出響應,那么通常使用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>

這將為您目前使用的編程方法提供可比的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM