GVKun编程网logo

Spring MVC的web.xml配置详解(转)(spring mvc web.xml配置)

17

最近很多小伙伴都在问SpringMVC的web.xml配置详解(转)和springmvcweb.xml配置这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Spring3.2中发布的

最近很多小伙伴都在问Spring MVC的web.xml配置详解(转)spring mvc web.xml配置这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Spring 3.2中发布的新Spring MVC Test Framework是否测试web.xml配置?、spring mvc 中web.xml配置信息解释、Spring MVC 二 :基于xml配置、spring mvc 和spring security配置 web.xml设置等相关知识,下面开始了哦!

本文目录一览:

Spring MVC的web.xml配置详解(转)(spring mvc web.xml配置)

Spring MVC的web.xml配置详解(转)(spring mvc web.xml配置)

  • 出处:http://blog.csdn.net/u010796790
  • spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name)
  • 在web.xml配置监听器ContextLoaderListener(listener-class) ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
  • 部署applicationContext的xml文件:contextConfigLocationcontext-param下的param-name
  • DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。 DispatcherServletservlet-nameservlet-classinit-paramparam-name(contextConfigLocation)、param-value) 在DispatcherServlet的初始化过程中,框架会在web应用的WEB-INF文件夹下寻找名为[servlet-name]-servlet.xml的配置文件,生成文件中定义的bean
<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="3.0" 
         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_3_0.xsd">  

    <!-- 
    在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?
    下面我们来看看Spring框架给我们提供过滤器`CharacterEncodingFilter`  
    这个过滤器就是针对于每次浏览器请求进行过滤的,然后再其之上添加了父类没有的功能即处理字符编码。  
    其中`encoding`用来设置编码格式,`forceEncoding`用来设置是否理会`request.getCharacterEncoding()`方法,设置为`true`则强制覆盖之前的编码格式。
    -->  
    <filter>  
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>

    <!-- 项目中使用Spring时,`applicationContext.xml`配置文件中并没有`BeanFactory`,要想在业务层中的class文件中直接引用Spring容器管理的bean可通过以下方式:-->

    <!--1、在web.xml配置监听器ContextLoaderListener:
    `ContextLoaderListener`的作用就是启动Web容器时,自动装配`ApplicationContext`的配置信息。因为它实现了`ServletContextListener`这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法; 
    在`ContextLoaderListener`中关联了`ContextLoader`这个类,所以整个加载配置过程由`ContextLoader`来完成;
    它的API说明:
    第一段,`ContextLoader`可以由`ContextLoaderListener`和`ContextLoaderServlet`生成(如果查看`ContextLoaderServlet`的API,可以看到它也关联了`ContextLoader`这个类而且它实现了`HttpServlet`这个接口)。
    第二段,`ContextLoader`创建的是`XmlWebApplicationContext`这样一个类,它实现的接口是`WebApplicationContext`->`ConfigurableWebApplicationContext`->`ApplicationContext`-> `BeanFactory`这样一来spring中的所有bean都由这个类来创建: 
     `IUploaddatafileManager uploadmanager` = (`IUploaddatafileManager`)    `ContextLoaderListener`.`getCurrentWebApplicationContext()`.`getBean("uploadManager")`;
     -->
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  

    <!--2、部署applicationContext的xml文件:
    如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml;
    在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml;
    如果是要自定义文件名可以在web.xml里加入`contextConfigLocation`这个context参数:  
        在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并以“,”号分隔。  
        也可以这样applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。  
        在`ContextLoaderListener`中关联了`ContextLoader`这个类,所以整个加载配置过程由`ContextLoader`来完成。
    -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring/applicationContext.xml</param-value>  
    </context-param>  

    <!--如果你的`DispatcherServlet`拦截"/",为了实现`REST`风格,拦截了所有的请求,那么同时对*.js,*.jpg等静态文件的访问也就被拦截了。-->  
    <!--解决方案一:
    激活Tomcat的`defaultServlet`来处理静态文件;
        要写在`DispatcherServlet`的前面, 让`defaultServlet`先拦截请求,这样请求就不会进入Spring了,我想性能是最好的吧。
    -->  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.css</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.swf</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.gif</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.jpg</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.png</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.js</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.html</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.xml</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.json</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>*.map</url-pattern>  
    </servlet-mapping>

    <!--使用Spring MVC,配置`DispatcherServlet`是第一步。`DispatcherServlet`是一个Servlet,,所以可以配置多个`DispatcherServlet`;
    `DispatcherServlet`是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。
    -->  
    <servlet>  
        <servlet-name>DispatcherServlet</servlet-name>    <!--在`DispatcherServlet`的初始化过程中,框架会在web应用的 WEB-INF文件夹下寻找名为[servlet-name]-servlet.xml 的配置文件,生成文件中定义的bean。-->  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <!--指明了配置文件的文件名,不使用默认配置文件名,而使用dispatcher-servlet.xml配置文件。-->  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <!--
            其中<param-value>**.xml</param-value> 这里可以使用多种写法:
                1、不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml
                2、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>
                3、<param-value>classpath*:dispatcher-servlet.xml</param-value>
                4、多个值用逗号分隔
            -->  
            <param-value>classpath:spring/dispatcher-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup><!--是启动顺序,让这个Servlet随Servletp容器一起启动。-->  
    </servlet>  
    <servlet-mapping>  
        <!--
            这个Servlet的名字是`dispatche`,可以有多个`DispatcherServlet`,是通过名字来区分的。每一个`DispatcherServlet`有自己的`WebApplicationContext`上下文对象。同时保存的`ServletContext`中和`Request`对象中;
            `ApplicationContext`是Spring的核心,`Context`我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,`ApplicationContext`则是“应用的容器”了;Spring把Bean放在这个容器中,在需要的时候,用`getBean`方法取出
        -->  
        <servlet-name>DispatcherServlet</servlet-name>  
        <!--
        Servlet拦截匹配规则可以自已定义,当映射为`@RequestMapping("/user/add")`时,为例,拦截哪种URL合适?
            1、拦截*.do、*.htm, 例如:/user/add.do,这是最传统的方式,最简单也最实用。不会导致静态文件(jpg,js,css)被拦截。
            2、拦截/,例如:/user/add,可以实现现在很流行的REST风格。很多互联网类型的应用很喜欢这种风格的URL。弊端:会导致静态文件(jpg,js,css)被拦截后不能正常显示。
         -->  
        <url-pattern>/</url-pattern>    <!--会拦截URL中带“/”的请求。-->  
    </servlet-mapping>  

   <!--指定欢迎页面--> 
    <welcome-file-list> 
        <welcome-file>login.html</welcome-file>  
    </welcome-file-list>  

    <!--当系统出现404错误,跳转到页面nopage.html-->  
    <error-page>
        <error-code>404</error-code>  
        <location>/nopage.html</location>  
    </error-page>

    <!--当系统出现java.lang.NullPointerException,跳转到页面error.html-->  
    <error-page>
        <exception-type>java.lang.NullPointerException</exception-type>  
        <location>/error.html</location>  
    </error-page>

    <!--会话超时配置,单位分钟-->  
    <session-config>
        <session-timeout>360</session-timeout>  
    </session-config>  
</web-app>  

Spring 3.2中发布的新Spring MVC Test Framework是否测试web.xml配置?

Spring 3.2中发布的新Spring MVC Test Framework是否测试web.xml配置?

我已经多次阅读了文档(http://static.springsource.org/spring/docs/3.2.x/spring-framework-
reference/html/testing.html#spring-mvc-test-
framework),我可以WebApplicationContext无法确认使用@WebApplicationContext注解时注入的上下文是否实际上在查看web.xml。

换句话说,我想测试我的web.xml配置。过滤器和servlet路径专门。但是,当我配置测试时,它将忽略web.xml。(例如,我尝试get对这样的URL发出请求/myServletPath/foo,但失败并显示404。)

我的测试:

@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration({        "classpath*:WEB-INF/config/application-context.xml",        "classpath*:WEB-INF/oms-servlet.xml",        "classpath*:persistence-context.xml"})public class OrderSummaryControllerIntegrationTests {    @Autowired    private WebApplicationContext wac;    private MockMvc mockMvc;    @Before    public void setUp() throws Exception {        this.mockMvc = webAppContextSetup(this.wac).build();    }    @Test    public void testFindOrderSummariesExpectsSuccess() throws Exception {        mockMvc.perform(get("/oms/orders?user=1234&catalog=bcs"))                .andDo(print())                .andExpect(status().isOk())                .andExpect(content().contentType(MediaType.APPLICATION_JSON));    }}

还有我的web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app 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"         version="2.5">    <display-name>OMS REST Services</display-name>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.html</url-pattern>    </servlet-mapping>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.css</url-pattern>    </servlet-mapping>    <filter>        <filter-name>webappMetricsFilter</filter-name>        <filter-class>com.yammer.metrics.web.DefaultWebappMetricsFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>webappMetricsFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/config/application-context.xml, classpath*:persistence-context.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <servlet>        <servlet-name>oms</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>oms</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

答案1

小编典典

没错,Spring-mvc-test不会读取web.xml文件,但是您可以通过以下方式配置过滤器:

webAppContextSetup(this.wac).addFilter(new DefaultWebappMetricsFilter(), "/*").build()

spring mvc 中web.xml配置信息解释

spring mvc 中web.xml配置信息解释

   在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰。

        首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter 写在 listener 的前面而会先加载 filter。最终得出的结论是:listener -> filter -> servlet

        同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真正的加载顺序为:context-param -> listener -> filter -> servlet

        对于某类配置节而言,与它们出现的顺序是有关的。以 filter 为例,web.xml 中当然可以定义多个 filter,与 filter 相关的一个配置节是 filter-mapping,这里一定要注意,对于拥有相同 filter-name 的 filter 和 filter-mapping 配置节而言,filter-mapping 必须出现在 filter 之后,否则当解析到 filter-mapping 时,它所对应的 filter-name 还未定义。web 容器启动时初始化每个 filter 时,是按照 filter 配置节出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是按照 filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。

        servlet 同 filter 类似,此处不再赘述。

       由此,可以看出,web.xml 的加载顺序是:context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。

 

web.xml文件详解

Xml代码  收藏代码

  1. Web.xml常用元素   

  2. <web-app>   
    <display-name></display-name>定义了WEB应用的名字   
    <description></description> 声明WEB应用的描述信息   
      
    <context-param></context-param> context-param元素声明应用范围内的初始化参数。   
    <filter></filter> 过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联。   
    <filter-mapping></filter-mapping> 一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联。   
    <listener></listener>servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。   
                         Listener元素指出事件监听程序类。   
    <servlet></servlet> 在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。Servlet元素就是用来完成此项任务的。   
    <servlet-mapping></servlet-mapping> 服务器一般为servlet提供一个缺省的URL:http://host/webAppPrefix/servlet/ServletName。   
                  但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。在更改缺省URL时,使用servlet-mapping元素。   
      
    <session-config></session-config> 如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。   
              可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值。   
      
    <mime-mapping></mime-mapping>如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证。   
    <welcome-file-list></welcome-file-list> 指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。   
    <error-page></error-page> 在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。   
    <taglib></taglib> 对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,   
                      而不用编辑使用这些文件的JSP页面。   
    <resource-env-ref></resource-env-ref>声明与资源相关的一个管理对象。   
    <resource-ref></resource-ref> 声明一个资源工厂使用的外部资源。   
    <security-constraint></security-constraint> 制定应该保护的URL。它与login-config元素联合使用   
    <login-config></login-config> 指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-constraint元素联合使用。   
    <security-role></security-role>给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素   
                       的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。   
    <env-entry></env-entry>声明Web应用的环境项。   
    <ejb-ref></ejb-ref>声明一个EJB的主目录的引用。   
    < ejb-local-ref></ ejb-local-ref>声明一个EJB的本地主目录的应用。   
    </web-app>
  3.   

  4.   

  5. 相应元素配置   

  6.   

  7. 1、Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标   

  8. <icon>   
    <small-icon>/images/app_small.gif</small-icon>   
    <large-icon>/images/app_large.gif</large-icon>   
    </icon>
  9. 2、Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称   

  10. <display-name>Tomcat Example</display-name>   

  11. 3、Web 应用描述: 给出于此相关的说明性文本   

  12. <disciption>Tomcat Example servlets and JSP pages.</disciption>   

  13. 4、上下文参数:声明应用范围内的初始化参数。   

  14.   <context-param>   
        <param-name>ContextParameter</para-name>   
        <param-value>test</param-value>   
        <description>It is a test parameter.</description>   
      </context-param>
  15.   在servlet里面可以通过getServletContext().getInitParameter("context/param")得到   

  16.   

  17. 5、过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联。   

  18.   <filter>   
            <filter-name>setCharacterEncoding</filter-name>   
            <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>   
            <init-param>   
                <param-name>encoding</param-name>   
                <param-value>GB2312</param-value>   
            </init-param>   
      </filter>   
      <filter-mapping>   
            <filter-name>setCharacterEncoding</filter-name>   
            <url-pattern>/*</url-pattern>   
      </filter-mapping>   
    6、监听器配置   
      <listener>   
          <listerner-class>listener.SessionListener</listener-class>   
      </listener>   
    7、Servlet配置   
       基本配置   
       <servlet>   
          <servlet-name>snoop</servlet-name>   
          <servlet-class>SnoopServlet</servlet-class>   
       </servlet>   
       <servlet-mapping>   
          <servlet-name>snoop</servlet-name>   
          <url-pattern>/snoop</url-pattern>   
       </servlet-mapping>   
       高级配置   
       <servlet>   
          <servlet-name>snoop</servlet-name>   
          <servlet-class>SnoopServlet</servlet-class>   
          <init-param>   
             <param-name>foo</param-name>   
             <param-value>bar</param-value>   
          </init-param>   
          <run-as>   
             <description>Security role for anonymous access</description>   
             <role-name>tomcat</role-name>   
          </run-as>   
       </servlet>   
       <servlet-mapping>   
          <servlet-name>snoop</servlet-name>   
          <url-pattern>/snoop</url-pattern>   
       </servlet-mapping>   
       元素说明   
         <servlet></servlet> 用来声明一个servlet的数据,主要有以下子元素:   
         <servlet-name></servlet-name> 指定servlet的名称   
         <servlet-class></servlet-class> 指定servlet的类名称   
         <jsp-file></jsp-file> 指定web站台中的某个JSP网页的完整路径   
         <init-param></init-param> 用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数   
         <load-on-startup></load-on-startup>指定当Web应用启动时,装载Servlet的次序。   
                                     当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.   
                                     当值为负或未定义:Servlet容器将在Web客户首次访问这个servlet时加载它   
         <servlet-mapping></servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素   
           <servlet-name></servlet-name> 指定servlet的名称   
           <url-pattern></url-pattern> 指定servlet所对应的URL
  19. 8、会话超时配置(单位为分钟)   

  20.    <session-config>   
          <session-timeout>120</session-timeout>   
       </session-config>
  21. 9、MIME类型配置   

  22.    <mime-mapping>   
          <extension>htm</extension>   
          <mime-type>text/html</mime-type>   
       </mime-mapping>
  23. 10、指定欢迎文件页配置   

  24.    <welcome-file-list>   
          <welcome-file>index.jsp</welcome-file>   
          <welcome-file>index.html</welcome-file>   
          <welcome-file>index.htm</welcome-file>   
       </welcome-file-list>
  25. 11、配置错误页面   

  26.   一、 通过错误码来配置error-page   

  27.    <error-page>   
          <error-code>404</error-code>   
          <location>/NotFound.jsp</location>   
       </error-page>
  28.   上面配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。   

  29. 二、通过异常的类型配置error-page   

  30.    <error-page>   
           <exception-type>java.lang.NullException</exception-type>   
           <location>/error.jsp</location>   
       </error-page>
  31.   上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp   

  32. 12、TLD配置   

  33.    <taglib>   
           <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>   
           <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>   
       </taglib>
  34.    如果MyEclipse一直在报错,应该把<taglib> 放到 <jsp-config>中   

  35.    <jsp-config>   
          <taglib>   
              <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>   
              <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>   
          </taglib>   
       </jsp-config>
  36. 13、资源管理对象配置   

  37.  

  38.   <resource-env-ref>   
           <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>   
       </resource-env-ref>
  39. 14、资源工厂配置   

  40.    <resource-ref>   
           <res-ref-name>mail/Session</res-ref-name>   
           <res-type>javax.mail.Session</res-type>   
           <res-auth>Container</res-auth>   
       </resource-ref>
  41.    配置数据库连接池就可在此配置:   

  42.    <resource-ref>   
           <description>JNDI JDBC DataSource of shop</description>   
           <res-ref-name>jdbc/sample_db</res-ref-name>   
           <res-type>javax.sql.DataSource</res-type>   
           <res-auth>Container</res-auth>   
       </resource-ref>
  43. 15、安全限制配置   

  44.    

  45. <security-constraint>   
          <display-name>Example Security Constraint</display-name>   
          <web-resource-collection>   
             <web-resource-name>Protected Area</web-resource-name>   
             <url-pattern>/jsp/security/protected/*</url-pattern>   
             <http-method>DELETE</http-method>   
             <http-method>GET</http-method>   
             <http-method>POST</http-method>   
             <http-method>PUT</http-method>   
          </web-resource-collection>   
          <auth-constraint>   
            <role-name>tomcat</role-name>   
            <role-name>role1</role-name>   
          </auth-constraint>   
       </security-constraint>
  46. 16、登陆验证配置   

  47.    <login-config>   
         <auth-method>FORM</auth-method>   
         <realm-name>Example-Based Authentiation Area</realm-name>   
         <form-login-config>   
            <form-login-page>/jsp/security/protected/login.jsp</form-login-page>   
            <form-error-page>/jsp/security/protected/error.jsp</form-error-page>   
         </form-login-config>   
       </login-config>
  48. 17、安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。   

  49.     分别地声明角色可使高级IDE处理安全信息更为容易。   

  50.   <security-role>   
         <role-name>tomcat</role-name>   
      </security-role>
  51. 18、Web环境参数:env-entry元素声明Web应用的环境项   

  52.   <env-entry>   
         <env-entry-name>minExemptions</env-entry-name>   
         <env-entry-value>1</env-entry-value>   
         <env-entry-type>java.lang.Integer</env-entry-type>   
      </env-entry>
  53. 19、EJB 声明   

  54.   <ejb-ref>   
         <description>Example EJB reference</decription>   
         <ejb-ref-name>ejb/Account</ejb-ref-name>   
         <ejb-ref-type>Entity</ejb-ref-type>   
         <home>com.mycompany.mypackage.AccountHome</home>   
         <remote>com.mycompany.mypackage.Account</remote>   
      </ejb-ref>
  55. 20、本地EJB声明   

  56.   <ejb-local-ref>   
         <description>Example Loacal EJB reference</decription>   
         <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>   
         <ejb-ref-type>Session</ejb-ref-type>   
         <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>   
         <local>com.mycompany.mypackage.ProcessOrder</local>   
      </ejb-local-ref>
  57. 21、配置DWR   

  58.   

     

  59. <servlet>   
          <servlet-name>dwr-invoker</servlet-name>   
          <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>   
      </servlet>   
      <servlet-mapping>   
          <servlet-name>dwr-invoker</servlet-name>   
          <url-pattern>/dwr/*</url-pattern>   
      </servlet-mapping>
  60. 22、配置Struts   

  61.     <display-name>Struts Blank Application</display-name>   
        <servlet>   
            <servlet-name>action</servlet-name>   
            <servlet-class>   
                org.apache.struts.action.ActionServlet   
            </servlet-class>   
            <init-param>   
                <param-name>detail</param-name>   
                <param-value>2</param-value>   
            </init-param>   
            <init-param>   
                <param-name>debug</param-name>   
                <param-value>2</param-value>   
            </init-param>   
            <init-param>   
                <param-name>config</param-name>   
                <param-value>/WEB-INF/struts-config.xml</param-value>   
            </init-param>   
            <init-param>   
                <param-name>application</param-name>   
                <param-value>ApplicationResources</param-value>   
            </init-param>   
            <load-on-startup>2</load-on-startup>   
        </servlet>   
        <servlet-mapping>   
            <servlet-name>action</servlet-name>   
            <url-pattern>*.do</url-pattern>   
        </servlet-mapping>   
        <welcome-file-list>   
            <welcome-file>index.jsp</welcome-file>   
        </welcome-file-list>   
      
        <!-- Struts Tag Library Descriptors -->   
        <taglib>   
            <taglib-uri>struts-bean</taglib-uri>   
            <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>   
        </taglib>   
        <taglib>   
            <taglib-uri>struts-html</taglib-uri>   
            <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>   
        </taglib>   
        <taglib>   
        <taglib-uri>struts-nested</taglib-uri>   
        <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>   
        </taglib>   
        <taglib>   
            <taglib-uri>struts-logic</taglib-uri>   
            <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>   
        </taglib>   
        <taglib>   
            <taglib-uri>struts-tiles</taglib-uri>   
            <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>   
        </taglib>
  62. 23、配置Spring(基本上都是在Struts中配置的)   

  63.  

  64.  
       <!-- 指定spring配置文件位置 -->   
       <context-param>   
          <param-name>contextConfigLocation</param-name>   
          <param-value>   
           <!--加载多个spring配置文件 -->   
            /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml   
          </param-value>   
       </context-param>   
      
       <!-- 定义SPRING监听器,加载spring -->   
      
      <listener>   
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
      </listener>   
      
      <listener>   
         <listener-class>   
           org.springframework.web.context.request.RequestContextListener   
         </listener-class>   
      </listener>

 


Spring MVC 二 :基于xml配置

Spring MVC 二 :基于xml配置

创建一个基于xml配置的Spring MVC项目。

Idea创建新项目,pom文件引入依赖:

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.12.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.12.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.12.RELEASE</version>
    </dependency>

在项目的webapp/WEB-INF目录下创建web.xml文件,配置DispatcherServlet:

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

  <servlet>
    <description>dispatcherServlet</description>
    <display-name>DispatcherServlet</display-name>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

</web-app>

注意其中定义了contextConfigLocation:上下文配置文件位置,配置为:classpath:springmvc.xml,指定Spring MVC启动过程中会到指定位置读取该配置文件,所以我们需要配置好springmvc.xml文件。

在项目resource路径下配置springmvc.xml文件:

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

    <!-- 扫描包 -->
    <context:component-scan base-package="org.example.controller"/>
    <!-- 视图解析器 -->
    <bean id="viewResolver">
        <!-- 视图前缀 -->
        <property name="prefix" value="/" />
        <!-- 视图后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

主要是指定Spring MVC的controller的包扫描路径,并指定视图解析器、以及视图后缀为jsp。

最后,创建一个conrolller:

package org.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {
    @GetMapping("/hello")
    @ResponseBody
    public String hello(ModelAndView model){
        return "<h1>Hello World</h1>";
    }
}

我们的目的就是让快速让这个Spring MVC案例跑起来,就不创建jsp文件了,所以在controller中引入了@ResponseBody注解(不加这个注解的话,controller的@GetMapping方法返回String则视图解析器会将其处理为jsp页面)。

项目结构如下图:
image.png

项目启动配置:
image.png

启动后,测试:

image.png

所以,通过idea创建一个基于SpringMVC的项目非常简单,通过以上几步,一个Spring MVC项目就可以正常运行起来了。

接下来我们先简单研究一下以上案例得以正常运行的底层原理。

DispatcherServlet

我们知道Spring MVC是通过一个核心servlet:DispatcherServlet来处理请求的,所以还是从DispatcherServlet入手。

先来看一眼DispatcherServlet类的继承关系:
image.png

这个类层次结构中,Servlet->GenericServlet->HttpServlet是属于java SDK的内容,来自于javax.servlet.http包,之后的从HttpServletBean到DispatcherServlet是属于Spring Web MVC框架的内容,来自于org.springframework.web.servlet包。

如果不使用Spring Mvc、自己创建Servlet的话,我们的Servlet直接继承HttpServlet即可,其实实现起来也很简单:
1. 创建MyServlet

package org.example.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("<h1>OHhhhh,this is my own Servlet...</h1>");
    }
}

2.在web.xml中配置MySevlet

  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>org.example.servlet.MyServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/my</url-pattern>
  </servlet-mapping>

3.启动项目,访问
image.png

其实Spring MVC的工作原理和MyServlet的工作原理类似,MyServlet是通过配置文件找到当前请求URL对应的Servlet,然后再根据请求类型调用Servlet的doGet或者doPost等方法处理请求。Spring MVC处理请求的Servlet是DispatcherServlet,DispatcherServlet根据请求URL匹配HandlerMapping、找到对应的Controller,之后交给HandlerAdapters经过请求参数的匹配、转换之后,通过反射机制调用Controller的对应方法,完成请求的处理。

上述DispatcherServlet的底层处理逻辑,后面我们会逐步细化、深入,从代码层面进行分析。

上一篇 Spring MVC 一 :从MVC & Servlet开始
下一篇 Spring MVC 三 :基于注解配置

spring mvc 和spring security配置 web.xml设置

spring mvc 和spring security配置 web.xml设置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
 
    <welcome-file-list>
        <welcome-file>index.do</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
 
    <!-- 激活Tomcat的defaultServlet处理静态资源 -->
    <!--<servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>-->
 
    <!--设置filter编码过滤器,解决中文乱码问题-->
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <!--spring 和security  配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml,classpath:spring-security.xml</param-value>
    </context-param>
 
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>lingdong.webapp.root</param-value>
    </context-param>
    <!--spring security 过滤器-->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
 
 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
     
    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>
 
    <!--spring mvc 配置文件-->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
 
    <!--取消jsp语言产生的空行-->
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
        </jsp-property-group>
    </jsp-config>
 
 
</web-app>

 

今天的关于Spring MVC的web.xml配置详解(转)spring mvc web.xml配置的分享已经结束,谢谢您的关注,如果想了解更多关于Spring 3.2中发布的新Spring MVC Test Framework是否测试web.xml配置?、spring mvc 中web.xml配置信息解释、Spring MVC 二 :基于xml配置、spring mvc 和spring security配置 web.xml设置的相关知识,请在本站进行查询。

本文标签: