java在提交表单时收到错误消息:“客户端发送的请求在语法上不正确”
我正在开发一个使用Spring框架3.1.0版开发的Web应用程序
我有一个JSP页面,其中包含一个表单,允许用户从选择下拉列表框中选择一个国家。提交表单应该运行一个数据库查询并返回相同的JSP页面,其中包括表单以及所选国家的作者和研究所数据的HTML表
以下是JSP页面的部分代码:
<c:url var="submitUrl" value="/authorsInstitutes.htm"/>
<form:form action="${submitUrl}" method="POST">
<select id="countryCode">
<option value="-1">-- Please Select --</option>
<c:forEach var="country" items="${countryList}">
<option value="${country.countryCode}">${country.countryName}</option>
</c:forEach>
</select>
<input type="submit"/>
</form:form>
下面是控制器类的代码,用于处理“GET”和“POST”请求的方法:
@RequestMapping(value = "/authorsInstitutes", method = RequestMethod.GET)
public ModelAndView displayAuthorsInstitutesForm() {
ModelAndView mav = new ModelAndView("authorsInstitutes");
List<Country> countryList = countrySrv.findAll();
mav.addObject("countryList", countryList);
return mav;
}
@RequestMapping(value = "/authorsInstitutes", method = RequestMethod.POST)
public ModelAndView displayAuthorsInstitutes(
@RequestParam(value = "countryCode") String country) {
ModelAndView mav = new ModelAndView("authorsInstitutes");
List<Country> countryList = countrySrv.findAll();
mav.addObject("countryList", countryList);
if (country != null && !country.trim().equals("")) {
List<Affiliation> affiliationList =
affiliationSrv.getAffiliationsByCountryCode(country);
mav.addObject("affiliationList", affiliationList);
}
return mav;
}
“GET”请求工作正常,但当我选择一个国家并提交表单时,会收到404错误消息:
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect ().
我相信这很简单,但就我个人而言,我只是不知道我做错了什么。我非常感谢你能帮我解决这个问题,谢谢
# 1 楼答案
因为我们在POST方法映射中有一个请求参数
countryCode
。控制器希望在请求中发送参数。这是在下面的代码中更改jsp中的select标记以包含如下所示的名称属性
这将为所选选项分配contryCode。 试试这个,我希望它能起作用
# 2 楼答案
您需要在url标记中删除url中的斜杠,如下所示:
<c:url var="submitUrl" value="authorsInstitutes.htm"/>
若在jsp中使用带斜杠的绝对路径,表单将不会发送数据
# 3 楼答案
使用Firebug或任何类似工具,检查是否将
countryCode
参数传递给服务器。 我还看到请求映射是@RequestMapping(value = "/authorsInstitutes", method = RequestMethod.POST)
其中as请求被发送到
/authorsInstitutes.htm