web.xml基本配置详解 [转]

    xiaoxiao2023-11-12  153

    Web中,无非是请求和响应;在SpringMVC中,请求的第一站是DispatcherServlet,充当前端控制器角色;DispatcherServlet会查询一个或多个处理器映射(handler mapping)并根据请求所携带的URL信息进行决策,将请求发送给哪个SpringMVC控制器(controller);控制器做两件事:一是将数据打包,二是定义逻辑视图名,然后返回给DispatcherServlet;DispatcherServlet通过视图解析器(view resolver)来将逻辑视图名匹配为一个特定的视图实现,它可能是也可能不是JSP;交付数据模型,以视图形式响应给客户,整个请求流程完成。

    web.xml

    <welcome-file-list>[欢迎页面,可定义多个,会依次查找可用视图]<listener> <listener-class>基本配置包含Log4jConfigListener和ContextLoaderListener,且log4j监听器在前,目前已废除log4j监听器,原因还在努力追问<context-param>指定上下文配置文件路径,基本配置包含log4j和Spring配置文件 <param-name>指定上下文名称,一般为:名称+ConfigLocation后缀,如:contextConfigLocation,不可随意定义,否则指定的配置文件无法加载成功,实际上它是org.springframework.web.servlet.FrameworkServlet中的一个成员变量,而FrameworkServlet是DispatcherServlet的父类,log4jConfigLocation目前不得而知<param-value>指定上下文路径,如:classpath:applicationContext.xml<servlet> <servlet-name>Servlet名称,可以自定义,但是需要遵守规则:比如指定为Spring,那么最好在classpath路径中配置Spring-servlet.xml,否则需要在子元素<init-param>特别指出<servlet-class>因为要配置MVC,所以指定为:org.springframework.web.servlet.DispatcherServlet<init-param>[定义容器启动时初始化的配置文件,作用主要是指定自定义配置文件的路径,貌似可以指定多个] <param-name>[contextConfigLocation,不可更改,原因见3.1]<param-value>[可以自定义,如:classpath:spring-servlet.xml,如果不定义,那么默认为:classpath:${servlet-name}-servlet.xml,见4.1]<load-on-startup>[定义为1,表示启动等级,参考文章]<servlet-mapping> <servlet-name>与4.1保持一致<url-pattern>一般定义为“/”,表示所有请求都通过DispatcherServlet来处理<filter>[以字符集为例] <filter-name>[自行指定]<filter-class>[org.springframework.web.filter.CharacterEncodingFilter]<init-param> <param-name>[encoding,不可更改,它是CharacterEncodingFilter中定义的一个成员变量]<param-value>[UTF-8]<filter-mapping> <filter-name>[与6.1保持一致]<url-pattern>[/*表示所有请求都经过此过滤器过滤]
    <?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"> <display-name></display-name> <welcome-file-list> <welcome-file>/WEB-INF/views/home.jsp</welcome-file> </welcome-file-list> <!-- 加载指定位置的上下文配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!-- 定义LOG4J监听器 --> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>Spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 表示启动容器时初始化该servlet --> <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>Spring</servlet-name> <!-- 表示哪些请求需要交给Spring Web MVC处理,/是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 使用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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

    转载自:https://www.cnblogs.com/yw0219/p/6084128.html

    最新回复(0)