1

最初のドロップダウン リスト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")を使用して値を呼び出しますが、null 値が返されます。

ListBIRFormNo.java ( 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;
}

}

4

1 に答える 1

1

リクエスト処理のライフサイクルを誤解している可能性があると思います。request.getParameter("bfnsCode")が null 以外の値を持つ唯一の方法は、呼び出されたパラメータが現在のリクエストbfnsCodeで送信された場合です。すべてのコードが単一のリクエストのコンテキスト内で実行されるため、(私が知る限り) ここではそうではありません。そうです、チェックした時点でnullになります。bfnsCode

同じページ内で 1 つの選択ボックスを別の選択ボックスに応答させたい場合は、通常、JavaScript を使用して実現するのが最適です。onchange最初のボックスにor onkeyup(または両方) のイベント ハンドラーが必要で、<select>最初のボックスから値を取得し、それを使用して 2 番目のボックスを更新するように実装する必要があります (AJAX を作成することによって) 。を呼び出して関連データをロードするか、ページで設定したキャッシュから正しいデータ セットをロードします)。これは実際よりも複雑に思えますが (特に jQuery などの JavaScript フレームワークを使用している場合)、現在のアプローチのように純粋にサーバー側のコードを使用して問題を解決することはできません。

Java コードを JSP ページ内に直接埋め込まないように、実装をリファクタリングすることを検討してください。ユーザーのチェックとフォームのリストのクエリに関連するビジネス ロジックをServlet実装 (または Struts によって提供される同等のサーブレットのような構造;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>

これにより、現在のプログラムによるアプローチに匹敵する結果が得られます。

于 2013-08-12T03:45:01.710 に答える