无法从servlet访问JSP下拉列表值

时间:2011-12-16 11:51:18

标签: java ajax jsp servlets

我有一个带有两个下拉位置和部门的jsp。

部门下拉列表的值根据Ajax的位置值填充。

但是当我尝试保存页面时,如果选择了部门下拉列表中的第一项,则无法访问部门下拉列表的值。

但如果选择了第2或第3项,则可以从servlet访问它。

<form id="form1" name="crtdocfrm" action="CreateLocation" method="post">
  <fieldset width="50%">
    <legend>Division</legend>
    <table id="table1">
      <tr class="tr_stylebutton">
        <td>Location Name</td>
        <td><select name="locName" onChange="showDept(this.value)">
          <option value="-1">--select--</option>
          <%
            Iterator itrLocation = arlLocation.iterator();
            while (itrLocation.hasNext()) {
          %>
          <option value=<%=itrLocation.next()%>><%=itrLocation.next()%>
          </option>
          <%}%>
        </select></td>
      </tr>

      <tr class="tr_stylebutton">
        <td>Department Name</td>
        <td>
          <select name="ddDeptName" onChange="alert(this.value)"></select>
        </td>

      </tr>
      <tr class="tr_stylebutton">
        <td>Division ID</td>
        <%
          LocationPolulate lp = new LocationPolulate();
          int Div_Id = lp.DivisionId();
        %>
        <td><input type="text" name="divID" disabled="true" value=<%=Div_Id + 1%>/></td>
      </tr>

      <tr class="tr_stylebutton">
        <td>Division Name</td>
        <td><input type="text" name="divName" value=""/></td>
      </tr>
    </table>

    <table border="0" width="55%" align="left" id="table-button">
      <tr class="tr_stylebutton">
        <td align="right"><input type="submit" value="Save" class="button"/></td>
        <td align="left"><input type="submit" value="Cancel" class="button"/></td>
      </tr>
    </table>
  </fieldset>
  &nbsp;&nbsp;<br/>
</form>

Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);

    int intLocId = Integer.parseInt(request.getParameter("locName"));
    String strDivNm = (String) request.getParameter("divName");
    int intDeptId = Integer.parseInt(request.getParameter("ddDeptName"));

    LocationPolulate lp = new LocationPolulate();
    int Res = lp.InsertDivision(strDivNm, intLocId, intDeptId);

    if (Res == 0) {
        request.setAttribute("Errmsg", "Error Cannot Save Division");
        RequestDispatcher dis1 = getServletContext().getRequestDispatcher("/AddDivision");
        dis1.forward(request, response);
    } else {
        int Div = lp.DivisionId();
        request.setAttribute("succmsg", "Successfully saved Division with division id:" + Div);
        RequestDispatcher dis1 = getServletContext().getRequestDispatcher("AddDivision.jsp");
        dis1.forward(request, response);
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

抱歉添加代码作为评论的一部分...我无法适应整个代码,所以我不得不添加评论..在java脚本中有一个错误。正确的一个在下面给出。谢谢所有你的建议......

function stateChanged()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        var showdata = xmlHttp.responseText;
        var strar = showdata.split(":");

        if(strar.length==1)
        {
            document.getElementById("locName").focus();
            alert("Please Select location Id");
        }
        else if(strar.length>1)
        {
            option=new Option("---Select---", -1);

            document.crtdocfrm.ddDeptName.options[0]=option;
            for(var i=1;i<strar.length-1;i=i+2)
            {
                option=new Option(strar[i+1], strar[i]);
                document.crtdocfrm.ddDeptName.options[(i-1)/2+1]=option;
            }
        }
    }
}

我从另一个jsp获取相同字符串中的id和name,并将其拆分以获取不同部门ID和名称的值。