SpringMVC之web.xml,了解必要配置项

SpringMVC之web.xml,了解必要配置项

转载:https://blog.csdn.net/qing_gee/article/details/50965562javascript

现代Web应用程序普遍使用MVC(model、view、controller)模式,那么SpringMVC就刚好能够轻松帮咱们搭建一个Web开发环境。而要搭好开发环境,熟知SpringMVC的三个XML(web.xml、application-context.xml、context-dispatcher.xml)就显得必不可少。而我呢,虽然前先后后左左右右也大见过三次Web框架,但每次都纠结的要了老命,那么痛定思痛,我决定下功夫把三个XML给搞得有条理些。css

不过呢,做为一个软件开发的全栈工程师(自黑不是自诩,身为创业团队的负责人,打杂工的角色我就只能勉为其难),对于三个XML的看法只停留在认知的层面,整篇文章的叙述不免有不专业的地方,各位光临的朋友尽管指摘,我将虚心接受。html

本篇先来介绍web.xml,了解其中必要的配置项,为之后顺利开发打下坚实基础。java

①、xml内容

<?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>ymeng</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-context.xml</param-value> </context-param> <!-- set character encoding spring --> <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> <filter> <filter-name>sessionValidate</filter-name> <filter-class> com.honzh.common.filter.SessionValidateFilter </filter-class> <init-param> <param-name>uri</param-name> <param-value>/deal/</param-value> </init-param> <init-param> <param-name>loginUrl</param-name> <param-value>/login</param-value> </init-param> <init-param> <param-name>backToUrl</param-name> <param-value>/</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </filter> <!-- 把须要进行check登录的请求放到此处 --> <filter-mapping> <filter-name>sessionValidate</filter-name> <url-pattern>/deal/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>web-app</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:context-dispatcher.xml</param-value> </init-param> <!-- 使系统在启动时装在servlet而不是第一个servlet被访问 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>web-app</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置session过时时间120分钟 --> <session-config> <session-timeout>120</session-timeout> </session-config> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> </web-app>

②、重点内容介绍

一、contextConfigLocation

<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-context.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
  1. 指定web项目从项目的src路径下加载application-context.xml,这是springMVC所必不可少的配置项,classpath前缀指定xml的路径在src下,这是我所喜好的方式。
  2. 经过ContextLoaderListener自动装配ApplicationContext的配置信息,这里能够参照Spring中的ContextLoaderListener使用

二、characterEncodingFilter

关于字符集过滤器,就无需多言,从xml配置上就能够看得出来,其做用就是为了防止乱码,固然最开始在接触struts2的时候,被中文乱码困扰的内心都有了挫败感,但springMVC轻轻松松搞定了这个烦恼。web

配置的方式基本固定以下:spring

<!-- set character encoding spring --> <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字符集过滤器CharacterEncodingFiltermarkdown

三、sessionValidate

sessionValidate的过滤器对于个人项目来讲就很关键了,其主要做用就是为了在页面跳转时检查session有没有失效(包含超时、未登录),而后呢,若是验证失败,能够跳转到首页登录,登录完成呢,又能够回到原始的页面(以下图所示,点击个人资源,系统发现我没有登录,那么弹出登录窗口,当我登录完成后,显示个人资源页面)。session

这里写图片描述

<filter> <filter-name>sessionValidate</filter-name> <filter-class> com.honzh.common.filter.SessionValidateFilter </filter-class> <init-param> <param-name>uri</param-name> <param-value>/deal/</param-value> </init-param> <init-param> <param-name>loginUrl</param-name> <param-value>/login</param-value> </init-param> <init-param> <param-name>backToUrl</param-name> <param-value>/</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </filter> <!-- 把须要进行check登录的请求放到此处 --> <filter-mapping> <filter-name>sessionValidate</filter-name> <url-pattern>/deal/*</url-pattern> </filter-mapping>

这个配置我稍候会用一整篇的文章来介绍,因此这里只作一个引导。app

四、DispatcherServlet

DispatcherServlet是springMVC自带的一个开箱即用的调度员,这个调度员就和context-dispatcher.xml联系起来了。框架

<servlet> <servlet-name>web-app</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:context-dispatcher.xml</param-value> </init-param> <!-- 使系统在启动时装在servlet而不是第一个servlet被访问 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>web-app</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
  1. load-on-startup参数,按照《SpringMVC学习指南》所说的意思是:“若是它存在,则它将在应用程序启动时状态servlet并调用它的init方法,若不存在,则在该servlet的第一个请求时被加载”。这里我就很少作深究了,说白了,就是我还深究不到什么玩意。
  2. url-pattern参数,这里我使用了”/”。按照《Spring实战》所说,url-pattern的匹配模式一般有“*.html”、“/*”等等,而他们各有各的弊端,而“/”模式声明它会做为默认的servlet而且会处理全部的请求,包括静态资源(关于静态资源,在稍候讲context-dispatcher.xml会详细说明)
  1. *.html 只能响应html格式的请求。
  2. /*模式表示没有映射特定类型的响应,这会在处理图片或者样式css时带来没必要要的麻烦。

五、session-timeout

<!-- 配置session过时时间120分钟 --> <session-config> <session-timeout>120</session-timeout> </session-config>

session-timeout就不作详细说明了,很直白,多说无益。有兴趣的话能够继续了解Java Web开发Session超时设置

六、404

<error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page>

经过该配置,当服务端出现404错误时就会跳转到404.jsp页面。

那么首先,咱们来看一下普通的404页面写法。

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><%@ include file="/components/common/taglib.jsp"%> <!DOCTYPE HTML> <html> <head> <meta http-equiv='Refresh' content='3;URL=${ctx}/'> <title>404 错误</title> </head> <body> <p>此页面正在开发中...</p> <p>系统将在 <span style="color:red;">3</span> 秒后跳转到首页,或者直接点击 <a href="javascript:history.back()">返回</a></p> </body> </html>

当出现404时,首先提示用户页面访问不到,而后手动跳转到首页或者3秒后跳转到首页。