我有一个JSP说myPage.jsp
在jsp中我是两个div
每个div包含一个表单如下:
div A:
<div class="validation-box">
<form:form id="compnay-detail" method="post" action="companySave.do" commandName="company">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="col"><label>Address Type</label> <form:input type="text" path="addressType" class="textbox" name="addressType" id="addType" maxlength="20" />
</td>
<td class="col"><label>Street</label> <form:input path="street"
class="textbox" name="street" id="street" maxlength="50" />
</td>
</tr>
<tr>
<td colspan="3" class="no-padding">
<div class="button-pannel margin-top">
<span class="lhscrv"> <span class="rhscrv"> <input type="submit" class="bttn" id="saveAddress" value="Save" />
</span>
</span> <span class="lhscrv"> <span class="rhscrv"> <input
type="button" class="bttn" id="reset" value="Reset" />
</span>
</span> <span class="lhscrv"> <span class="rhscrv"> <input
type="button" class="bttn" id="cancel" value="Cancel" />
</span>
</span>
</td>
</tr>
</table>
</form:form>
</div>
div B:
<div class="validation-box">
<form:form id="compnayAddDetail" method="post" action="test.do" commandName="address">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="col"><label>Address Type</label> <form:input type="text" path="addressType" class="textbox" name="addressType" id="addType" maxlength="20" />
</td>
<td class="col"><label>Street</label> <form:input path="street"
class="textbox" name="street" id="street" maxlength="50" />
</td>
</tr>
<tr>
<td colspan="3" class="no-padding">
<div class="button-pannel margin-top">
<span class="lhscrv"> <span class="rhscrv"> <input type="submit" class="bttn" id="saveAddress" value="Save" />
</span>
</span> <span class="lhscrv"> <span class="rhscrv"> <input
type="button" class="bttn" id="reset" value="Reset" />
</span>
</span> <span class="lhscrv"> <span class="rhscrv"> <input
type="button" class="bttn" id="cancel" value="Cancel" />
</span>
</span>
</td>
</tr>
</table>
</form:form>
</div>
MY CONTROLLER CLASS:
@RequestMapping("/companySave.do")
public String saveCompany(Map<String, Object> map, @ModelAttribute("company")
Company company,@ModelAttribute("address")
CompanyAddress address, BindingResult result)
{
//...... code.......
return "redirect:/companyAddressPage/"+company.getId()+".do";
}
@RequestMapping("/companyAddressPage/{id}.do")
public String companyAddressPage(@PathVariable("id") long id,Map<String, Object> map, @ModelAttribute("company")
Company company,@ModelAttribute("address")
CompanyAddress address, BindingResult result)
{ //...... code.......
return "myPage";
}
@RequestMapping("/test.do")
public String saveAddressCompany(Map<String, Object> map, @ModelAttribute("company")
Company company,@ModelAttribute("address")
CompanyAddress address, BindingResult result)
{ //...... code.......
return "myPage";
}
目的:
提交div中存在的第一个表格后,我想启用包含其他表格的div B.
问题:
第一种形式是正确提交,但是当我点击第二种形式的提交按钮时,它会抛出异常(动作甚至没有被召唤)
例外:
无法将值测试从类型'java.lang.String'转换为'long'类型;嵌套异常是java.lang.NumberFormatException:对于带有根本原因的输入字符串:“test”]
任何想法.... ???
提前致谢... !!!
答案 0 :(得分:1)
编辑:也见评论。
您需要在第二个表单的操作属性前面添加正斜杠:
action="/test.do"
否则您将最终到/companyAddressPage/test.do,因为您刚刚被重定向到/companyAddressPage/1.do(其中1是示例id)。这是因为第二种形式的动作属性是相对的。
答案 1 :(得分:0)
无论你在@PathVariable中得到什么值,它总是只有String类型值。
所以不要使用
@PathVariable("id") long id
使用此
@PathVariable("id") String id
然后在使用函数之前手动将其转换为long。
希望这会对你有所帮助。
干杯。