SpringMVC环境配置之web.xml

1.web.xml

  1.1 web.xml配置中的<mime-mapping>的作用?
用途:指定特定后缀名的mime类型,好让该后缀名的文件被安全传输到客户端.
保证它们在发送到客户端时分配为某种MIME类型;

  1.2 <welcome-file-list>的作用?
指定应用的首页
里面可以指定多个文件,应用服务器会按从上到下的顺序搜索,如果找到就了
进入该页面,如果都遍历完了还没找到就会返回404错误代码给浏览器。
(若服务器根目录下无此文件##不要放在子目录下##,会进入Controller中/对应的请求);

welcome-file-list是一个配置在web.xml中的一个欢迎页,用于当用户在url中
输入工程名称或者输入web容器url(如http://localhost:8080/)时直接跳转的页面.

在Spring MVC配置文件dispatcher-servlet.xml中添加:<mvc:default-servlet-handler/>
 

 1.3 session有效期?

     <session-config>

        <session-timeout>30</session-timeout>
     </session-config>
 
session-timeout元素用来指定默认的会话超时时间间隔,以分钟为单位。
该元素值必须为整数。如果session-timeout元素的值为零或负数,则表示
会话将永远不会超时。

  1.4 SpringMVC设置字符编码
  <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>

其中encoding用来设置编码格式,
forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,
 设置为true则强制覆盖之前的编码格式。
 
Spring里的字符过滤器CharacterEncodingFilter是针对请求的,
forceEncoding=true是意思是指无论客户端请求是否包含了编码,都用过滤器里的编码来解析请求;
 
当Servlet容器启动的时候,会读取web.xml中对于过滤器的配置信息, 读取到<init-param>中的子标签
<param-name>encoding和forceEncoding所对应的<param-value>的值,再通过调用该类setEncoding(String encoding)
和setForceEncoding(boolean forceEncoding) 将值分别注入到encoding和forceEncoding 中。
 
forceEncoding:字面意思是强制字符集,但你大可不必按字面意思理解,因为这个参数的值只不过是指定response的
字符集是否也设置成encoding所指定的字符集,所以你可以选择设置为true或false,默认值为false。
当值为true时,相当于
request.setCharacterEncoding(this.encoding);  
response.setCharacterEncoding(this.encoding);  
当值为false时,相当于:
request.setCharacterEncoding(this.encoding);  
 
故如果在web.xml中设置了此编码格式就可以不用再代码中再写这两句设置编码了

1.5 <!-- Shiro filter-->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

先filter中加入DelegatingFilterProxy类,"targetFilterLifecycle"指明作用于filter的所有生命周期。
原理是,DelegatingFilterProxy类是一个代理类,所有的请求都会首先发到这个filter代理,然后再
按照"filter-name"委派到spring中的这个bean。
在Spring中配置的bean的name要和web.xml中的<filter-name>一样.

此外,spring bean实现了Filter接口,但默认情况下,是由spring容器来管理其生命周期的(不是由tomcat这种服务器容器来管理)。
如果设置"targetFilterLifecycle"为true,则由spring来管理Filter.init()和Filter.destroy();若为false,则这两个方法失效!!

1.6 把Spring容器集成到Web应用里面
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
ContextLoaderListener的作用就是启动Web容器时,自动装配applicationContext.xml的配置信息。
因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

使用spring除了添加必要的jar包,另外在web.xml一定要加上启动spring的监听器,这样配置在xml文件中的bean才会初始化;
它会默认查找位于:WEB-INF/下的是否有一个文件名称为:applicationContext.xml 

ApplicationContext.xml这个配置文件部一般默认放置在applicationContext的默认的路径是”/WEB-INF/applicationContext.xml。
也可以在web.xml中配置该文件的其他位置,在web.xml中在配置另外一个节点名称,配置如下:
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
            classpath:applicationContext-security.xml;
        </param-value>
</context-param>

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring.xml</param-value>
    </context-param>

1.7 classpath 和 classpath* 区别:

首先 classpath是指 WEB-INF文件夹下的classes目录 

解释classes含义: 
1.存放各种资源配置文件 eg.init.properties log4j.properties struts.xml 
2.存放模板文件 eg.actionerror.ftl 
3.存放class文件 对应的是项目开发时的src目录编译文件 
总结:这是一个定位资源的入口 

classpath:只会到你指定的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.

举个简单的例子,在我的web.xml中是这么定义的:classpath*:META-INF/spring/application-context.xml
那么在META-INF/spring这个文件夹底下的所有application-context.xml都会被加载到上下文中,
这些包括META-INF/spring文件夹底下的 application-context.xml,META-INF/spring的子文件夹的application-context.xml
以及jar中的application-context.xml。

如果我在web.xml中定义的是:classpath:META-INF/spring/application-context.xml
那么只有META-INF/spring底下的application-context.xml会被加载到上下文中。

    1.8 <!-- 防止内存泄漏 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
参考:http://blog.csdn.net/lizhi_java/article/details/46849455

1.9 路径匹配
<url-pattern>/</url-pattern>  会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url
<url-pattern>/*</url-pattern> 会匹配所有url:路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等)
<url-pattern>/</url-pattern>  不会匹配到*.jsp,不会进入spring的DispatcherServlet类
<url-pattern>/*</url-pattern> 会匹配*.jsp,导致进入spring的DispatcherServlet类,然后去寻找controller,接着找不到对应的controller所以报错。

所以要在spring-servlet.xml文件中配置如下:
<!--让spring前端控制器跳过静态的资源请求 location路径, /resources/**这个文件夹下所有资源全部跳过 -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

路径后缀匹配方式:【注意后缀匹配开始路径不需要编写“/”】       
<url-pattern>*.do</url-pattern>                后缀匹配
<url-pattern>*.action</url-pattern>            后缀匹配

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值