Spring MVC的web.xml配置详解
作者:Liuqz2009 时间:2023-08-10 04:45:01
spring是目前最流行的框架。创建java web项目时,我们首先会遇到的配置文件就是web.xml,这是javaweb为我们封装的逻辑,不在今天的研究中。下面我们将简单讲讲web.xml中的配置。
一、一个空的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID">
</web-app>
二、标签介绍
web.xml中比较常见的标签以及其加载顺序为:
context-param > listener > filter > servlet
1、 <display-name>Archetype Created Web Application</display-name>
display-name 是标识项目的名称,这个不是很常用,可有可无的,或者说不需要我们去在意的东西。
2、 <context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>60000</param-value>
</context-param>
context-param 是web.xml首先加载的标签,其下子标签有param-name和param-value.
此所设定的参数,在JSP网页中可以使用下列方法来取得:
${initParam.webAppRootKey}
若在Servlet可以使用下列方法来获得:
String param_name=getServletContext().getInitParamter(“webAppRootKey”);
3、listener
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
listenter在项目开始的时候就注入进来,尽在context-param之后,所以正常我们将spring配置在listener 中,这样方法spring 初始化相关的bean。
4、filter
<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起到一个过滤的作用,在servlet执行前后,像上面的配置就是在过滤servlet前将编码转换UTF-8,filter-mapping 则是将filter和url路径进行映射。其中init-param则是将初始化需要的参数传入到filter-class中从而进行初始化。filter和filter-mapping中的name必须是相同的,才能起到映射的作用,而filter-mapping 中的url-pattern则是匹配请求路径的。上面‘/*'表示过滤所有请求的servlet,如果写成‘/zxh',则过滤http://localhost:8080/项目名/zxh这个请求。
5、servlet
<servlet>
<!-- 配置DispatcherServlet -->
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定spring mvc配置文件位置 不指定使用默认情况 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<!-- 设置启动顺序 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- ServLet 匹配映射 -->
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.zxh</url-pattern>
</servlet-mapping>
servlet和filter类似,需要先指定servlet对应的class类,然后将这个类和utl路径请求地址进行映射。这里不多说了。
来源:http://blog.csdn.net/liuqz2009/article/details/72833678
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java8 统计字符串字母个数的几种方法总结(推荐)
java实现省市区转换成树形结构
![](https://img.aspxhome.com/file/2023/4/84264_0s.jpg)
Bitmap海量数据快速查找去重代码示例
Spring Boot Security配置教程
Android自定义TextView实现文字图片居中显示的方法
![](https://img.aspxhome.com/file/2023/0/137690_0s.gif)
一篇文章带你了解Java容器,面板及四大布局管理器应用
![](https://img.aspxhome.com/file/2023/2/128832_0s.png)
Java回调方法详解
使用Netty实现类似Dubbo的远程接口调用的实现方法
Android WaveView实现水流波动效果
![](https://img.aspxhome.com/file/2023/9/138859_0s.gif)
C#实现动态创建接口并调用的实例
![](https://img.aspxhome.com/file/2023/4/92924_0s.jpg)
Prometheus 入门教程之SpringBoot 实现自定义指标监控
![](https://img.aspxhome.com/file/2023/7/67487_0s.jpg)
详解AngularJs与SpringMVC简单结合使用
使用java实现各种数据统计图(柱形图,饼图,折线图)
![](https://img.aspxhome.com/file/2023/5/81365_0s.png)
C# 9 新特性——record的相关总结
![](https://img.aspxhome.com/file/2023/5/116655_0s.png)
Mybatis配置错误:java.lang.ExceptionInInitializerError
![](https://img.aspxhome.com/file/2023/4/77114_0s.jpg)
java编程调用存储过程中得到新增记录id号的实现方法
Springboot如何添加server.servlet.context-path相关使用
![](https://img.aspxhome.com/file/2023/9/113469_0s.png)
Flutter实现图文并茂的列表
![](https://img.aspxhome.com/file/2023/3/139283_0s.jpg)
详细聊聊如何在C#循环中捕获局部变量
android JSON解析数据 android解析天气预报
![](https://img.aspxhome.com/file/2023/6/94986_0s.jpg)