Spring Form:客户端发送的请求在语法上是不正确的

时间:2015-08-08 10:10:51

标签: java spring forms jsp

我有以下jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

      <form:form action="save_event" modelAttribute="eventForm" method="post">

      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Create Event</h4>
      </div>

      <div class="modal-body">

        <div class="row">
            <div class="col-xs-12 form-group">
                <label for="eventNameInput">Name:</label>
                <form:input id="eventNameInput" class="form-control" type="text" path="eventName" />
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12 form-group">
                <label for="eventStartInput">Start:</label>
                <div class='input-group date' id='datetimepicker1'>
                    <form:input id="eventStartInput" class="form-control" type="text" path="startDate" />
                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12 form-group">
                <label for="eventEndInput">End:</label>
                <div class='input-group date' id='datetimepicker2'>
                    <form:input id="eventEndInput" class="form-control" type="text" path="endDate" />
                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12 form-group">
                <label for="eventDescInput">Description:</label>
                <form:textarea id="eventDescInput" class="form-control" path="description"></form:textarea>
            </div>
        </div>

    </div>
    <div class="modal-footer">
        <input type="submit" value="Submit" class="btn btn-primary" />
        <input type="reset" class="btn btn-default" value="Reset" />
    </div>

    </form:form>

    <script type="text/javascript">
    $(function () {
        $('#datetimepicker1').datetimepicker({
            sideBySide: true,
            format: "DD.MM.YYYY HH:mm"
        });
        $('#datetimepicker2').datetimepicker({
            sideBySide: true,
            format: "DD.MM.YYYY HH:mm"
        });
    });
    </script>

以下控制器:

@Controller
public class EventCreateController {

    @RequestMapping(value="/create_event", method=RequestMethod.GET)
    public String createEvent(Model model) {
        model.addAttribute("eventForm", new EventForm());
        return "create_event";
    }

    @RequestMapping(value="/save_event", method=RequestMethod.POST)
    public String eventSubmit(@ModelAttribute("eventForm") EventForm form, Model model) {

        List<Event> eventList = new ArrayList<Event>();
        model.addAttribute("eventList", eventList);

        return "redirect:/welcome";
    }

}

但是每当提交表单时我都会收到错误: 客户端发送的请求在语法上是不正确的。

我通过邮寄方式发送表格并通过邮寄表格。我正在使用“/ save_event”,我希望结果为“/ save_event”。

一些想法?

编辑:这是application-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.afterguard.sailplanner.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

和web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns="http://java.sun.com/xml/ns/javaee" 
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
            version="3.0">
  <display-name>SailPlanner</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>


  <servlet>
    <servlet-name>sailplanner</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>sailplanner</servlet-name>
    <url-pattern>/welcome</url-pattern>
    <url-pattern>/users</url-pattern>
    <url-pattern>/create_event</url-pattern>
    <url-pattern>/save_event</url-pattern>
  </servlet-mapping>

</web-app>

2 个答案:

答案 0 :(得分:0)

该行动不应只是/save_event而不是${context}/save_event.jsp吗?

  1. 在使用form:form时,应自动处理上下文的前缀,即形成taglib。
  2. 为什么.jsp后缀?

答案 1 :(得分:0)

RequestMapping值应与表单的操作相同。使用表单标签代替表单:表单并且不要在表单操作或控制器中声明.jsp扩展名,只需在applicationContext.xml中定义 InternalResourceViewResolver

<强> CODE:

<强>的applicationContext.xml

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/pages/" />
    <property name="suffix" value=".jsp" />
</bean>

编辑:将 / pages / 替换为您的视图页面位置。

<强> JSP:

<form action="save_event" modelAttribute="eventForm" method="post">

  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title">Create Event</h4>
  </div>
</form>

<强>控制器:

@Controller
public class EventCreateController {

@RequestMapping(value="/save_event", method=RequestMethod.POST)
public String eventSubmit(@ModelAttribute("eventForm") EventForm form, Model model) {

    List<Event> eventList = new ArrayList<Event>();
    model.addAttribute("eventList", eventList);

    return "redirect:/welcome";
  }

}

我希望这对你有用。