我可以为两个jsps使用一个请求映射吗?
我目前正在从一个控制器调用一个请求映射,但其中一个jsps似乎没有被控制器捕获。
两个jsps都具有相同的表单操作和相同的表单方法:
first.jsp看起来像这样:
<form:form method="POST" action="/ShowroomNav/requestQuote" id="requestQuoteForm">
<input type="hidden" value=${product.productCode } name="productCodes" />
<input type="hidden" id="requestQuoteEmailAddress" name="requestQuoteEmailAddress" />
</form:form>
second.jsp看起来像这样:
<form:form method="POST" action="/ShowroomNav/requestQuote" id="requestQuoteForm">
<input type="hidden" id="requestQuoteEmailAddress" name="requestEmailAddress" />
<c:forEach var="product" items="${products}">
<input type="hidden" value=${product.productCode } name="productCodes" />
<div class="box">
<img
src="public/productImages/${product.productCode}/${product.productCode}A.jpg"
style="max-width: 100%"
onclick="productProfile('${product.productCode}')" /><br /> <label
class="name">${product.productName}</label>
</div>
</c:forEach>
</form:form>
他们都通过javascript调用提交函数:
$("#requestQuoteSubmitButton").one("click",function(){
$("#requestQuoteEmailAddress").val($("#requestQuoteEmailAddressModal").val());
alert($("#requestQuoteEmailAddress").val());
$("#requestQuoteForm").submit();
});
这就是controller.java的样子:
@RequestMapping(value = "/requestQuote", method = RequestMethod.POST) // or GET
public String requestQuote(@RequestParam("requestQuoteEmailAddress") String requestQuoteEmailAddress, @RequestParam("productCodes") String[] productCodes) {
System.out.println(">>>> requesting quotes >>>>");
for(int i=0; i<productCodes.length; i++) {
System.out.println(" Product Codes : " + productCodes[i]);
}
System.out.println("requestQuoteEmailAddress : " + requestQuoteEmailAddress );
System.out.println("<<<<< requesting quotes <<<<");
return "productSearch";
}
所以我不知道为什么控制器无法捕获second.jsp,因为它在我尝试提交时始终显示此错误。
HTTP Status 400 - The request sent by the client was syntactically incorrect.
有人可以帮忙吗?
答案 0 :(得分:5)
你的second.jsp片段的第二行出现问题(错字?):
<input type="hidden" id="requestQuoteEmailAddress" name="requestEmailAddress" />
id
属性主要用于客户端引用,并且在提交表单时无关紧要(请参阅HTML input - name vs. id)。重要的是name
属性。因此,当POST请求发送到服务器时,请求正文如下所示:
requestEmailAddress=...&productCodes=...&productCodes=...
由于您将处理程序方法参数注释为@RequestParam("requestQuoteEmailAddress")
,因此Spring MVC会查找 requestQuoteEmailAddress 而不是 requestEmailAddress ,因此错误(@RequestParam
' s默认情况下是必需的。)