【问题标题】:Form POST submit "The request sent by the client was syntactically incorrect."表单 POST 提交“客户端发送的请求在语法上不正确。”
【发布时间】:2014-10-10 19:08:30
【问题描述】:

我不知道我在哪里犯了错误。我一直试图解决这个问题几个小时,但无法弄清楚...... 我在提交带有对象列表的表单时收到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

【问题讨论】:

  • 需要查看您的 FormWrapper 定义
  • dao.getCatsList() 给出什么类型?
  • List ,我已经用 Category 的实现更新了问题

标签: spring forms jsp post submit


【解决方案1】:

是不是因为 FormWrapper 类没有“selectedCategories”属性。 我尝试删除“selectedCategories” 然后表单提交成功。

<form:checkboxes items="${allCategories}" path="list[${loop.index}]" itemLabel="name"/>

【讨论】:

    【解决方案2】:

    我不确定我是否正确理解了您的 jsp,但是当 jsp 表单中的某些内容与您在控制器处处理的参数不匹配时,就会出现此问题。

    你确定下面的“路径”变量没问题吗?

    <form:checkboxes items="${allCategories}" path="list[${loop.index}].selectedCategories" itemLabel="name"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 2019-02-20
      • 2019-05-14
      相关资源
      最近更新 更多