Spring MVC语法上不正确的请求

时间:2014-04-27 11:22:47

标签: java spring spring-mvc

我正在使用Spring MVC创建一个Web应用程序,但我的所有POST请求都会导致“客户端发送的请求在语法上不正确”。例如,这是一个搜索表单:

<form id="projectsForm" action="#" th:action="@{/projects}" th:object="${projectsForm}" method="post">
<input type="hidden" th:field="*{page}" />

<div id="search">
    <select id="expert" th:field="*{expert}">
        <option value="">(select expert)</option>
        <option th:each="expert : ${experts}"
                th:value="${expert.id}"
                th:text="${expert.firstName + ' ' + expert.lastName}"></option>
    </select>
    <select id="company" th:field="*{company}">
        <option value="">(select company)</option>
        <option th:each="company : ${companies}"
                th:value="${company.id}"
                th:text="${company.name}"></option>
    </select>
    <input type="text" id="query" th:field="*{query}" />
    <button class="search" onclick="firstPage()">Search</button>
    <button class="empty" onclick="empty()">Erase</button>
</div>

表单对象类如下所示:

public class ProjectsForm {

    private Expert expert;
    private Company company;
    private String query;
    private Integer page = 0;
    private Integer pages;

    public Expert getExpert() {
        return expert;
    }

    public void setExpert(Expert expert) {
        this.expert = expert;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPages() {
        return pages;
    }

    public void setPages(Integer pages) {
        if (pages > 0 && page >= pages) {
            page = pages - 1;
        }
        this.pages = pages;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }
}

这是控制器方法:

@RequestMapping(value="/projects", method=RequestMethod.POST)
public String projectsPost(@ModelAttribute("projectsForm") ProjectsForm projectsForm, ModelMap model) {
    sessionBean.setProjectsForm(projectsForm);
    Page<Project> projectPage = projectService.findAll(projectsForm.getPage(), ProjectController.PAGESIZE, projectsForm.getExpert(), projectsForm.getCompany(), projectsForm.getQuery());
    List<Project> projects = projectPage.getContent();
    model.addAttribute("projects", projects);
    projectsForm.setPage(projectPage.getNumber());
    projectsForm.setPages(projectPage.getTotalPages());
    model.addAttribute("projectsForm", projectsForm);
    return "projects";
}

Chrome告诉我表单数据如下所示:

page=0&expert=&company=&query=

是否有明显的错误,或者有什么办法可以诊断出这个问题?将log4j.logger.org.springframework.web = DEBUG添加到log4j.properties并未向我提供任何更多信息。令我困惑的是,完全相同的代码在Spring Boot jar应用程序中运行良好。

0 个答案:

没有答案