前言
作为小白,最近在学ssh框架,当看到web.xml编写时脑袋就有点懵,花了点时间看了看别人的代码,对此做一个总结。
web.xml的作用
在一个web中,web.xml是非必须的。而web.xml是用来干什么的呢??它是用来配置欢迎页、servlet、filter、struts配置文件等的。当你的web工程用不上他们,就不需要配置web.xml文件。
第一步编写web-app
每一个web.xml文件的根元素中都得标明这个web.xml文件使用的是哪个模式文件。如:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
web.xml的模式(Schema)文件中定义了多少种标签元素,web.xml中就可以出现它的模式文件所定义的标签元素,它就能拥有定义出来的那些功能。
第二步配置需要加载的spring配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
其中beans.xml是spring的配置文件,如果没有contextConfigLocation 指定配置文件,则Spring 自动查找applicationContext.xml 配置文件。如果有contextConfigLocation,则利用该参数确定的配置文件。该参数指定的一个字符串,Spring 的ContextLoaderListener 负责将该字符串分解成多个配置文件,逗号","、空格" “及分号”;"都可作为字符串的分割符。如果既没有applicationContext.xml 文件,也没有使用contextConfigLocation参数确定配置文件,或者contextConfigLocation确定的配置文件不存在。都将导致Spring 无法加载配置文件或无法正常创建ApplicationContext 实例
第三步配置ContextLoaderListener监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
实现了ServletContextListener接口的作用就是当项目一经启动,就会激活实现了此接口的类方法,可以进行相关的初始化操作。
创建了ContextLoaderListener这个监听器,它继承了ContextLoader类、实现了ServletContextListener接口,监听器对Application的创建进行了监听 ;有ServletContext创建了,这个事件就会被监听器的contextInitlized(ServletContext event)方法监听到; 将application设置到contextLoader属性上;执行源码的this.contextLoader.initWebApplicationContext(event.getServletContext());取出web.xml中contextConfigLocation参数的值,也就是spring的配置信息;
根据这些配置信息生成Bean工厂;最后把这个bean工厂设置到application中去;
可以在后端处理器(Action)中通过application取出beanFactory,进而从beanFactory取出业务对象,进行业务操作。
第四布配置Log4j
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
log4j是Apache的一个开放源代码的项目,通过使用log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
而log4jConfigLocation的作用是配合org.springframework.web.util.Log4jConfigListener来设置spring环境下的Log4j
第五步struts过滤器
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
过滤器是在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的 action进行业务逻辑,比如过滤掉非法url(不是login.do的地址请求,如果用户没有登陆都过滤掉),或者在传入servlet或者 struts的action前统一设置字符集,或者去除掉一些非法字符
第六步设置编码过滤器
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
encodingFilter 是一个对编码进行统一处理的过滤,对请求和响应设置 你预先在xml配置的固定编码。
第七步配置webAppRootKey
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webShopping</param-value>
</context-param>
在tomcat下部署两个或多个项目时,web.xml文件中最好定义。
webAppRootKey参数,如果不定义,将会缺省为“webapp.root”
其用于获取应用路径。
第八步设置欢迎页
<welcome-file-list>
<welcome-file>init.jsp</welcome-file>
</welcome-file-list>
wrb.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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webShopping</param-value>
</context-param>
<welcome-file-list>
<welcome-file>init.jsp</welcome-file>
</welcome-file-list>
</web-app>
文章中内容参考文章链接:https://blog.csdn.net/ywl570717586/article/details/49494601
https://www.cnblogs.com/javahr/p/8408857.html
https://blog.csdn.net/isunnyvinson/article/details/52787486
https://www.cnblogs.com/godtrue/p/6442273.html