需要帮助才能使 Spring 表单正常工作
我正在编写一个 Spring Web 应用程序,一切进展顺利。我正在尝试编写一个表单,允许管理员更改一些基本的用户详细信息。但是,在提交表单时,我不断收到 HTTP 400 错误,指出客户端发送的请求“语法错误”。它并没有提供任何实际有用的信息,而且我确实检查了所有内容,并且不知道我可能做错了什么。这是 JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<div align="center">
<s:url value="/admin/access" var="access_url" />
<sf:form method="POST" modelAttribute="user" dojoType="dijit.form.Form">
<script type="dojo/method" event="onSubmit">
if (!this.validate()) {
return false;
}
return true;
</script>
<table>
<tr>
<td align="right">Username: </td>
<td>
<sf:input path="username" dojoType="dijit.form.ValidationTextBox" trim="true" required="true"/>
</td>
</tr>
<tr>
<td align="right">Password: </td>
<td>
<sf:input path="password" type="password" dojoType="dijit.form.ValidationTextBox" required="true"/>
</td>
</tr>
<tr>
<td align="right">Enabled: </td>
<td>
Yes<sf:radiobutton path="enabled" value="true" dojoType="dijit.form.RadioButton"/>
No<sf:radiobutton path="enabled" value="false" dojoType="dijit.form.RadioButton"/>
</td>
</tr>
<tr>
<td align="right">Admin: </td>
<td>
Yes<sf:radiobutton path="isAdmin" value="true" dojoType="dijit.form.RadioButton"/>
No<sf:radiobutton path="isAdmin" value="false" dojoType="dijit.form.RadioButton"/>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<button dojoType="dijit.form.Button" type="submit">Submit</button>
</td>
</tr>
</table>
</sf:form>
</div>
这是最终将在控制器中调用的方法,尽管还没有那么远:
@RequestMapping(value="/admin/editUser", method=RequestMethod.POST)
public String submitEditUser(@RequestParam("user") NFIUser nfiUser) {
这是来自同一个控制器的 GET 映射,尽管表单显示完美,所有值都正确映射到表格,所以我不知道这里有什么问题:
@RequestMapping(value="/admin/editUser", method=RequestMethod.GET)
public void showEditUser(Model model, @RequestParam("username") String username) {
....
model.addAttribute("user", user);
}
无论如何,如果有人有任何想法,请告诉我。我已经成功解决了我一整天遇到的所有错误,但我似乎看不出这个有任何问题。谢谢。
I'm in the middle of writing a Spring webapp and things were going well. I'm trying to write a form that allows an admin to change some basic user details. However, on submission of the form I keep getting an HTTP 400 error saying the request sent by the client was "Syntactically Incorrect". It doesn't offer any actual useful info though and I've really looked over everything and have no idea what I could be doing wrong. Here is the JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<div align="center">
<s:url value="/admin/access" var="access_url" />
<sf:form method="POST" modelAttribute="user" dojoType="dijit.form.Form">
<script type="dojo/method" event="onSubmit">
if (!this.validate()) {
return false;
}
return true;
</script>
<table>
<tr>
<td align="right">Username: </td>
<td>
<sf:input path="username" dojoType="dijit.form.ValidationTextBox" trim="true" required="true"/>
</td>
</tr>
<tr>
<td align="right">Password: </td>
<td>
<sf:input path="password" type="password" dojoType="dijit.form.ValidationTextBox" required="true"/>
</td>
</tr>
<tr>
<td align="right">Enabled: </td>
<td>
Yes<sf:radiobutton path="enabled" value="true" dojoType="dijit.form.RadioButton"/>
No<sf:radiobutton path="enabled" value="false" dojoType="dijit.form.RadioButton"/>
</td>
</tr>
<tr>
<td align="right">Admin: </td>
<td>
Yes<sf:radiobutton path="isAdmin" value="true" dojoType="dijit.form.RadioButton"/>
No<sf:radiobutton path="isAdmin" value="false" dojoType="dijit.form.RadioButton"/>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<button dojoType="dijit.form.Button" type="submit">Submit</button>
</td>
</tr>
</table>
</sf:form>
</div>
Here is the method that will end up being called in the Controller, though it's not getting that far:
@RequestMapping(value="/admin/editUser", method=RequestMethod.POST)
public String submitEditUser(@RequestParam("user") NFIUser nfiUser) {
And here is the GET mapping from the same controller, though the form displays perfectly with all values being mapped properly into the form so I don't know that there is any issue here:
@RequestMapping(value="/admin/editUser", method=RequestMethod.GET)
public void showEditUser(Model model, @RequestParam("username") String username) {
....
model.addAttribute("user", user);
}
Anyway, if anyone has any thoughts please let me know. I've been successfully solving errors all I'm encountering all day but I can't seem to see any issue with this one. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试删除submitEditUser方法中的@reqiestParam。
try to remove the @reqiestParam fotm the submitEditUser method.
事实证明,答案是每个人留下的东西的组合。通常,对于 Spring,您不需要表单上的操作方法,但在这种情况下,由于我加载表单的方式,最终需要一个操作方法来覆盖 Spring 正在加载的操作。
还删除了 @RequestParam("user") 位解决了 400 错误的问题。因为这次我使用了 spring 表单标签,所以一切都绑定得很好,所以我想我不需要 RequestParam 位。显然,我还不太清楚什么时候需要这些,什么时候不需要。
不管怎样,我标记了人们的答案,但从技术上讲,他们都不是完整的解决方案,所以我发布了这个。谢谢大家。
Turns out the answer was a combination of what everyone left here. Normally for Spring you don't need an action method on a form but in this case it turned out, due to how I was loading the form one ended up being needed in the end to overwrite the action that was being loaded by Spring.
Also removing the @RequestParam("user") bit solved the problem of the 400 error. Because I was using spring form tags this time so that everything was bound nicely I guess I didn't need the RequestParam bit. Clearly I don't yet have the best understanding of when those are and aren't needed.
Anyway, I marked up people's answers, but technically none of them was the full solution so I posted this. Thanks all.