2012-01-07 183 views
5

我寫了一個非常簡單的Spring MVC應用程序。我很抱歉,我對Spring MVC比較陌生,所以請耐心等待。SpringMVC servlet映射

的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"> 

    <!-- 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>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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

</web-app> 

我的第一個問題是,我有一個JSP頁面下面的代碼登錄...

<form action="/login" method="post" > 
Username : <input name="username" type="text" /> 
Password : <input name="password" type="password" /> 
<input type="submit" /> 
</form> 

這給出了一個404,但在我的控制器,我已經將控制器映射到/用以下代碼登錄...

@Controller 
public class LoginController { 

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class); 

    /** 
    * Simply selects the home view to render by returning its name. 
    */ 
    @RequestMapping(value = "/login", method = RequestMethod.POST) 
    public String home(Locale locale, Model model, String username, String password) { 

     if(username.equalsIgnoreCase("david")) 
     { 
      logger.info("Welcome home! the client locale is "+ locale.toString()); 

      Date date = new Date(); 
      DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 

      String formattedDate = dateFormat.format(date); 

      model.addAttribute("serverTime", formattedDate); 

      return "home"; 
     } 
     else 
     { 
      return "void"; 
     } 

    } 

} 

我的理解是@requestm apping應該執行servlet映射而不是在web.xml中,這是正確的嗎?如果需要,/WEB-INF/spring/appServlet/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-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"> 

    <!-- 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="org.david.myapp" /> 



</beans:beans> 

所以我的第一個問題是:servlet映射在web.xml或在控制器類的@RequestMapping做了什麼?

第二個問題:構建這個頁面的最佳方法是什麼?我應該繼續添加到webxml嗎?我應該爲每個網址創建一個控制器嗎?我應該爲每個url創建一個servlet-context嗎?

感謝您的閱讀:)

+0

我沒有檢查你給了整個事情,但在第一個視圖控制器上您的請求方法是GET,你的表單使用POST方法。似乎是一個錯誤... – Omnaest 2012-01-07 11:01:40

+0

啊,謝謝你改變,但仍然是同樣的問題,編輯上面的代碼來反映這一點。 – david99world 2012-01-07 11:33:12

回答

5

您已經定義<url-pattern>/,這意味着你的appServlet只會收到請求到根網址。通過將其更改爲/*appServlet將獲得所有傳入請求。這將工作,但你也可以考慮創建一個特定的loginServlet,它可以映射到url /login/*

  1. 您可以在單個web.xml中定義多個servlet。通過添加更多<servlet-mapping>標籤來指定哪個請求將觸及每個servlet。
  2. 一個servlet可能有許多控制器。通常,一個控制器爲你的域的特定部分提供服務,例如PersonController,AddressController
  3. 每個控制器通常處理邏輯上分組在一起的多個URL,例如, /persons/{id}/persons/search/persons/add
+0

好吧,如果servlet映射在web.xml中完成,控制器類中@RequestMapping(value =「/ login」,method = RequestMethod.POST)的用途是什麼?謝謝:) – david99world 2012-01-07 12:28:34

+1

您可以在類級別和方法級別使用'@ RequestMapping'來實現對如何處理不同請求的細粒度控制。不同的處理可以基於URL(上面的c.f.項目3),HTTP方法和/或不同的請求參數。有關請求映射的更多信息可以在[Spring手冊]中找到(http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping ) – matsev 2012-01-07 12:46:48

+0

好吧,對不起,我可能會困惑自己,但如果我有/登錄到網絡。xml映射到一個servlet上下文,然後/登錄在@RequestMapping,這是否意味着達到這個控制器,我必須去/登錄/登錄?謝謝你的幫助。 – david99world 2012-01-07 13:09:27