【问题标题】:Getting value from 1st drop down list jsp从第一个下拉列表 jsp 中获取价值
【发布时间】:2013-08-12 03:32:30
【问题描述】:

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

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

}

【问题讨论】:

  • 使用servlet并将参数存储在请求属性中,以便在下一个视图jsp中呈现。
  • 是否可以只使用单一形式?这些只是表单内的 div。

标签: java jsp struts


【解决方案1】:

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

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

请考虑重构您的实现,使其不会将 Java 代码直接嵌入到 JSP 页面中。您可以将与检查用户和查询表单列表相关的业务逻辑移动到 Servlet 实现(或 Struts 提供的等效类似 Servlet 的构造;我相信是 Action),然后使用&lt;c:forEach&gt; 标签将选项附加到您的 &lt;select&gt; 元素(即 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>

这将产生与您目前使用的程序化方法相当的结果。

【讨论】:

  • 是的,我看到了一些我应该这样做的东西,我只是希望也许有更简单的方法。感谢您让我知道我必须做什么。
  • 是的,这就是我想要的,但我的列表是基于我的数据库的,我不能以静态方式进行。
  • @RMsplace - 对,因此您可以使用 AJAX 调用在第一个框更改时获取数据,或者(如果您的数据库很小)您可以设置类似于 data 的内容通过使用您的 JSP 将您的信息输出为 JSON,在 JSFiddle 中创建数组。
  • 我想使用&lt;:forEach&gt; 标签。你能告诉我如何使用数据库来做到这一点吗?
  • @RMsplace - 我已经用一个如何重构代码以使用&lt;c:forEach&gt; 循环的示例更新了答案。
猜你喜欢
  • 2013-06-14
  • 1970-01-01
  • 2020-05-24
  • 2020-08-11
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多