我不知道我在哪里弄错了。我已经试图解决这个问题好几个小时但是无法解决这个问题......
我在提交带有对象列表的表单时获得HTTP Status 400 The request sent by the client was syntactically incorrect.
,每个对象都有一些复选框。
下面是一些代码:
控制器:
@RequestMapping(value = "/admin/panel", method = RequestMethod.GET)
public String adminPanel(Locale locale, Model model, Form form,
HttpServletRequest request) {
FormWrapper wrapper = getFormWrapper();
model.addAttribute("listOfObjects", wrapper);
model.addAttribute("allCategories", dao.getCatsList());
return "WEB-INF/views/index/admin/home";
}
@RequestMapping(value = "/admin/saveAdmin", method = RequestMethod.POST)
public String save(Model model, @ModelAttribute(value="listOfObjects") FormWrapper listOfObjects) {
return "redirect:../index.html";
}
JSP:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<form:form modelAttribute="listOfObjects" method="POST" action="/admin/saveAdmin">
<c:forEach var="myObject" items="${listOfObjects.list}" varStatus="loop">
<form:checkboxes items="${allCategories}" path="list[${loop.index}].selectedCategories" itemLabel="name"/>
</c:forEach>
<input type="submit" value="saveTest"/>
</form:form>
FormWrapper:
public class FormWrapper {
private List<Form> list;
public List<Form> getList() {
return list;
}
public void setList(List<Form> list) {
this.list = list;
}
}
类别:
public class Category{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long categoryId;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getCategoryId() {
return categoryId;
}
public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Category){
return getCategoryId().equals(((Category)obj).getCategoryId());
} else {
return false;
}
}
}
感谢任何帮助。我尝试将模型属性adnotatnion更改为RequestParam
,但在这种情况下,我的对象始终为null
答案 0 :(得分:1)
是不是因为,FormWrapper类没有属性&#34; selectedCategories&#34;。 我尝试删除&#34; selectedCategories&#34; 然后表格成功提交。
<form:checkboxes items="${allCategories}" path="list[${loop.index}]" itemLabel="name"/>
答案 1 :(得分:0)
我不确定我是否正确理解了您的jsp,但是当jsp表单中的某些内容与您在控制器上处理的参数不匹配时会出现此问题。
你确定&#34;路径&#34;下面的变量可以吗?
<form:checkboxes items="${allCategories}" path="list[${loop.index}].selectedCategories" itemLabel="name"/>