2014-02-27 103 views
1

有人可以帮我吗?Spring MVC,表单提交不起作用

我的控制器类看起来是这样的,我已经创建的客户模型类..

/** 
* Handles requests for the application home page. 
*/ 
@Controller 
@RequestMapping("/customer") 
public class CustomerController { 

    @RequestMapping(method = RequestMethod.GET) 
    public ModelAndView student() { 
     return new ModelAndView("customer", "command", new Customer()); 
    } 

    @RequestMapping(value = "/addCustomer", method = RequestMethod.POST) 
     public String addStudent(@ModelAttribute Customer customer, 
     ModelMap model) { 

     model.addAttribute("customerName", customer.getCustomerName()); 
     model.addAttribute("emailId", customer.getEmailId()); 
     model.addAttribute("sex", customer.getSex()); 

     return "customerDetails"; 
    } 
} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <display-name>Customer Form Handling</display-name> 


    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>Customer</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/Customer/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>Customer</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

JSP页面

customer.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
    <title>Customer Form Handling</title> 
</head> 
<body> 

<h2>Customer Information</h2> 
<form:form method="POST" commandName = "command" action="/addCustomer"> 
    <table> 
    <tr> 
     <td><form:label path="customerName">customerName</form:label></td> 
     <td><form:input path="customerName" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="emailId">emailId</form:label></td> 
     <td><form:input path="emailId" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="sex">sex</form:label></td> 
     <td><form:input path="sex" /></td> 
    </tr> 
    <tr> 
     <td colspan="2"> 
      <input type="submit" value="Submit"/> 
     </td> 
    </tr> 
</table> 
</form:form> 
</body> 
</html> 

customerDetails.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
    <title>Customer Form Handling</title> 
</head> 
<body> 

<h2>Customer Detail Information</h2> 
    <table> 
    <tr> 
     <td>CustomerName</td> 
     <td>${customerName}</td> 
    </tr> 
    <tr> 
     <td>emailId</td> 
     <td>${emailId}</td> 
    </tr> 
    <tr> 
     <td>sex</td> 
     <td>${sex}</td> 
    </tr> 
</table> 
</body> 
</html> 

Servlet的context.xml中

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <!-- <resources mapping="/resources/**" location="/resources/" /> --> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

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

</beans:beans> 

但是当我运行在Tomcat服务器这个应用程序。第一URL指向 localhost:8080/controller/

如果我追加localhost:8080/controller/customer我得到的第一个表单页面..

但是,一旦我点击提交..它说找不到网页的错误。

+0

是您的customerDetails视图在/ WEB-INF/views目录中。还是像客户那样有一个目录? –

+0

其内部WEB-INF /视图 – user3358717

回答

2

这是一个相对路径问题。您的表单操作为/addCustomer(前缀为/),如果解决该问题,则为http://localhost:8080/addCustomer。你想要的可能是http://localhost:8080/appname/customer/addCustomer

在某些情况下,简单地将其更改为customer/addCustomer可能会解决它,但如果您的页面也可以通过http://localhost:8080/appname/customer/(注意尾部斜线)访问,那么这可能是一个问题。相对路径将转换为http://localhost:8080/appname/customer/customer/addCustomer

当然,现在您可以认为只是做了/appname/customer/addCustomer并解决了问题,但实际上您现在正在对上下文路径名称进行硬编码。如果有一天上下文路径发生变化,所有这些代码都会中断。

一种方法,我喜欢用,所以我的JSP可以计算出上下文路径是通过定义变量根

<c:set var="root" value="${pageContext.request.contextPath}"/> 
... 
<form:form action="${root}/customer/addCustomer"> 
+0

嗨,感谢您的回复。 – user3358717

+0

嗨,感谢您的回复。当我将行动更改为客户/ addcustomer时,它解决了问题。我无法理解这些...当我在tomcat服务器上运行应用程序。我的意思是我从日食运行应用程序。所以我右键单击该项目并在服务器上运行。初始页面被加载为http:// localhost:8080/controller /,并且它表示页面未加载。然后,如果我将Url更改为http:// localhost:8080/controller/customer,那么我打算工作的第一页,然后获取表单页面。如何纠正它。此外,还应该将该行为称为客户/添加客户。 – user3358717

+0

由于/ customer是控制器映射到的路径,/ addcustomer是处理器方法映射到的路径 – gerrytan

1

尝试为

<form:form method="POST" commandName = "command" action="addCustomer"> 

,而不是

<form:form method="POST" commandName = "command" action="/addCustomer">