无法从 servlet 访问 JSP 下拉值
我有一个带有两个下拉位置和部门的jsp。
部门下拉列表的值是通过 Ajax 根据位置值填充的。
但是,当我尝试保存页面时,如果选择了部门下拉列表中的第一项,则无法访问部门下拉列表的值。
但如果选择第二项或第三项,则可以从 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>
<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);
}
}
有什么想法吗?
I have a jsp with two drop down location and department.
The value of department dropdown is populated based on the value of location through Ajax.
But when I try to save the page I can't access the value of department dropdown if the first item from the department drop down is selected.
But if 2nd or 3rd item is selected it can be accessed from the 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>
<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);
}
}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,将代码添加为注释的一部分...我无法容纳整个代码,所以我必须添加注释..java 脚本中有一个错误。下面给出了正确的错误。感谢大家的建议。 ..
我从另一个 jsp 获取同一字符串中的 id 和名称,并将其拆分以获取不同部门 id 和名称的值。
sorry for adding code as a part off comment...i could not fit the whole code so i had to add the comment..there was a mistake in the java script.the correct one is given below.Thanks all for your suggestion ...
i am getting the id and name in the same string from another jsp and spliting it to get the value of the different department id and name.