Spring MVC 3.x 基本配置

WEB-INF/web.xmlcss

例1html

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringSvcDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

例2java

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringSvcDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- 自定義位置 -->
            <param-value>/WEB-INF/SpringMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 

 

說明:web

<load-on-startup>1</load-on-startup>:是啓動順序,讓這個Servlet隨Servletp容器一塊兒啓動。spring

<servlet-name>SpringMVC</servlet-name>:servlet的名稱。 DispatcherServlet的初始化後,它會在 Web應用程序的WEB - INF文件夾中查找一個文件名[servlet-name]-servlet.xml的配置,這裏將會查找SpringMVC-servlet.xml,假如不想採用默認的方式查找,固然也能夠自定義位置(見web.xml例2)spring-mvc

<url-pattern>*.do</url-pattern>:映射URL模式*. do的DispatcherServlet,Servlet攔截匹配規則能夠自已定義,攔截哪一種URL合適?當映射爲@RequestMapping("/user/add")時,爲例:mvc

  • 攔截*.do、*.htm, 例如:/user/add.do,這是最傳統的方式,最簡單也最實用。不會致使靜態文件(jpg,js,css)被攔截。

  • 攔截/,例如:/user/add能夠實現如今很流行的REST風格。不少互聯網類型的應用很喜歡這種風格的URL。弊端:會致使靜態文件(jpg,js,css)被攔截後不能正常顯示。想實現REST風格,事情就是麻煩一些。後面有解決辦法還算簡單(見下面).

  • 攔截/*,這是一個錯誤的方式,請求能夠走到Action中,但轉到jsp時再次被攔截,不能訪問到jsp。

 

 

 

 

WEB-INF/SpringMVC-servlet.xmlapp

 

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        
    <!-- 自動掃描的包名 -->
    <context:component-scan base-package="com.test" /> 
    <!-- 默認的註解映射的支持 -->
    <mvc:annotation-driven />
    <!-- 視圖解釋類 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

 

在上面的XML配置文件,咱們已經定義了一個標記<context:component-scan>。 這將容許Spring從com.test包中載入全部組件和它的全部子包。 這將載入咱們Controller類。 另外,咱們定義一個bean viewResolver視圖解釋,並添加前綴字符串/WEB-INF/view/以及後綴.JSP。 因此一個爲hello 的@RequestMapping("/hello") 請求映射對應的視圖爲/WEB-INF/jsp/hello.jspjsp

 

訪問靜態文件(jpg,js,css)的方法:ui

<url-pattern>/</url-pattern>的攔截方法雖然能夠實現REST URL,會致使靜態文件也被攔截,能夠經過配置來設置訪問.

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        
    <!-- 自動掃描的包名 -->
    <context:component-scan base-package="com.test" /> 
    <!-- 默認的註解映射的支持 -->
    <mvc:annotation-driven />
    
    <mvc:resources location="/WEB-INF/view/image/" mapping="/image/**"/>  
    <mvc:resources location="/WEB-INF/view/js/" mapping="/js/**"/>  
    <mvc:resources location="/WEB-INF/view/css/" mapping="/css/**"/>

    <!-- 視圖解釋類 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

/WEB-INF/view/image/dogs.jpg在視圖hello.jsp中就能夠正常訪問了

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring 3.x MVC Series: Hello World</title>
</head>
<body>
    <h1>${message}</h1>
    <img alt="dogs" src="image/dogs.jpg">
</body>
</html>
相關文章
相關標籤/搜索