提供HTTP状态400的Spring MVC表单

时间:2017-03-04 06:22:43

标签: spring spring-mvc

我收到HTTP状态400.客户端发送的请求在语法上是不正确的。我正在做的是,只需将编辑表单保存到数据库中,但请求不要使用saveApplication方法。

以下是我的edit.jsp

<div align="center">
        <h1>New/Edit Contact</h1>
        <form:form action="saveApplication" method="post" modelAttribute="application">
        <table>
            <form:hidden path="applicationId"/>
            <tr>
                <td>Application Name:</td>
                <td><form:input path="applicationName" /></td>
            </tr>
            <tr>
                <td>Start Date:</td>
                <td><form:input path="startDate" id="startDate"/></td>
            </tr>
            <tr>
                <td>End Date:</td>
                <td><form:input path="endDate" id="endDate"/></td>
            </tr>
            <tr>
                <td>Projected StartDate:</td>
                <td><form:input path="projectedStartDate" id="projectedStartDate"/></td>
            </tr>
            <tr>
                <td>Projected EndDate:</td>
                <td><form:input path="projectedEndDate" id="projectedEndDate"/></td>
            </tr>
            <tr>
                <td>Current Action:</td>
                <td><form:input path="currentAction" /></td>
            </tr>

            <tr>
                <td>Comments:</td>
                <td><form:input path="comments" /></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Save"></td>
            </tr>
        </table>
        </form:form>
    </div>

以下是我的控制器类中的方法:

    @RequestMapping(value = "/editApplication", method = RequestMethod.GET)
    public ModelAndView editApplication(HttpServletRequest request) {

        ModelAndView model = new ModelAndView();
        int applicationId = Integer.parseInt(request.getParameter("id"));
        ApplicationTO to = applicationService.getApplication(applicationId);
        model.addObject("application", to);
        model.setViewName("edit");

        return model;
    }

    @RequestMapping(value = "/saveApplication", method = RequestMethod.POST)
    public ModelAndView saveContact(@ModelAttribute ApplicationTO application) {
        ModelAndView model = new ModelAndView();
        applicationService.saveApplication(application);
        model.setViewName("view");
        return model;
    }

2 个答案:

答案 0 :(得分:1)

我错过了表单中的变量danoneValidation,实际上这个变量存在于Model类中。

   <div align="center">
            <h1>New/Edit Contact</h1>
            <form:form action="saveApplication" method="post" modelAttribute="application">
            <table>
                <form:hidden path="applicationId"/>
                <tr>
                    <td>Application Name:</td>
                    <td><form:input path="applicationName" /></td>
                </tr>
                <tr>
                    <td>Start Date:</td>
                    <td><form:input path="startDate" id="startDate"/></td>
                </tr>
                <tr>
                    <td>End Date:</td>
                    <td><form:input path="endDate" id="endDate"/></td>
                </tr>
                <tr>
                    <td>Projected StartDate:</td>
                    <td><form:input path="projectedStartDate" id="projectedStartDate"/></td>
                </tr>
                <tr>
                    <td>Projected EndDate:</td>
                    <td><form:input path="projectedEndDate" id="projectedEndDate"/></td>
                </tr>
                <tr>
                    <td>Current Action:</td>
                    <td><form:input path="currentAction" /></td>
                </tr>

                <tr>
                    <td>Danone Validation:</td>
                    <td><form:input path="danoneValidation" /></td>
                </tr>

                <tr>
                    <td>Comments:</td>
                    <td><form:input path="comments" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Save"></td>
                </tr>
            </table>
            </form:form>
        </div>

答案 1 :(得分:-1)

尝试在操作名称之前添加斜杠,例如

<form:form action="/saveApplication" method="post" modelAttribute="application"> <table>

这可能是必需的。

同时检查提交表单时浏览器栏上打印的URL。仔细检查路径是否是您的控制器所期望的。