2014-02-27 172 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">