我正在使用Spring MVC。并得到以下错误:不支持请求方法“ POST”
Java
@RequestMapping(value = "/jdbcInsertGuest", method = RequestMethod.POST)
public void jdbcInsertGuest(@RequestParam(value = "guestName") String guestName, @RequestParam(value="comment") String comment) {
Guest guest = new Guest();
guest.setGuestName(guestName);
guest.setComment(comment);
jdbcExample.insertGuest(guest);
}
JSP
<form name="jdbcInsertGuest" method="POST">
<table>
<tr>
<td><b>Name: </b></td>
<td><input type='text' name='guestName'/></td>
</tr>
<tr>
<td><b>Comment: </b></td>
<td><input type='text' name="comment"/></td>
</tr>
</table>
<button>Send</button>
</form>
当我将方法更改为GET时,出现以下错误:不存在必需的字符串参数'guestName'。
该如何解决?
答案 0 :(得分:0)
我认为您应该在jsp中添加“ action =” / jdbcInsertGuest“”
<form name="jdbcInsertGuest" action="/jdbcInsertGuest" method="POST">
<table>
<tr>
<td><b>Name: </b></td>
<td><input type='text' name='guestName'/></td>
</tr>
<tr>
<td><b>Comment: </b></td>
<td><input type='text' name="comment"/></td>
</tr>
</table>
<button>Send</button>
答案 1 :(得分:0)
进行以下更改,
action="/URL"
属性添加到表单中,此属性使用Spring MVC控制器中的相关方法映射表单事件name
(jdbcInsertGuest)更改为guest,此数据模型传递给post方法@ModelAttribute
,此注释将表单数据与相关模型绑定JSP,
<form name="guest" action="/jdbcInsertGuest" method="POST">
<table>
<tr>
<td><b>Name: </b></td>
<td><input type='text' name='guestName'/></td>
</tr>
<tr>
<td><b>Comment: </b></td>
<td><input type='text' name="comment"/></td>
</tr>
</table>
<button>Send</button>
MVC控制器,
@RequestMapping(value = "/jdbcInsertGuest", method = RequestMethod.POST)
public void jdbcInsertGuest(@ModelAttribute("guest") Guest guest) {
jdbcExample.insertGuest(guest);
}
POJO(Guest.java),
public class Guest {
private String guestName;
private String comment;
public String getGuestName() {
return guestName;
}
public void setGuestName(String guestName) {
this.guestName = guestName;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
谢谢