zoukankan      html  css  js  c++  java
  • Spring MVC 3.x 基本配置

    WEB-INF/web.xml

    例1

    <?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>

    例2

    <?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>

    说明:

    <load-on-startup>1</load-on-startup>:是启动顺序,让这个Servlet随Servletp容器一起启动。

    <servlet-name>SpringMVC</servlet-name>:servlet的名称。 DispatcherServlet的初始化后,它会在 Web应用程序的WEB - INF文件夹中查找一个文件名[servlet-name]-servlet.xml的配置,这里将会查找SpringMVC-servlet.xml,假如不想采用默认的方式查找,当然也可以自定义位置(见web.xml例2)

    <url-pattern>*.do</url-pattern>:映射URL模式*. do的DispatcherServlet,Servlet拦截匹配规则可以自已定义,拦截哪种URL合适?当映射为@RequestMapping("/user/add")时,为例:

    • 拦截*.do、*.htm, 例如:/user/add.do,这是最传统的方式,最简单也最实用。不会导致静态文件(jpg,js,css)被拦截。

    • 拦截/,例如:/user/add可以实现现在很流行的REST风格。很多互联网类型的应用很喜欢这种风格的URL。弊端:会导致静态文件(jpg,js,css)被拦截后不能正常显示。想实现REST风格,事情就是麻烦一些。后面有解决办法还算简单(见下面).

    • 拦截/*,这是一个错误的方式,请求可以走到Action中,但转到jsp时再次被拦截,不能访问到jsp。

    WEB-INF/SpringMVC-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: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.jsp

    访问静态文件(jpg,js,css)的方法:

    <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>
  • 相关阅读:
    许家骏
    平均得分 【杭州电-HDOJ-2023】 附加题+详细说明
    百度之星的第二个问题
    kendo ui 单击取消编辑数据grid减少的原因和治疗方法的数据
    2013年第36周准备考下半年的项目管理师
    2013年第36周三杂记
    2013第36周二小结
    2013第36周星期一
    2013年9月1日下午
    2013年8月最后一天晚上
  • 原文地址:https://www.cnblogs.com/xiepeixing/p/4235750.html
Copyright © 2011-2022 走看看