springMVC web.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_3_0.xsd" id="WebApp_ID" version="3.0">
  	<display-name>attendance</display-name>
  	
  	<!-- 配置过滤器,同时把所有的请求都转为utf-8编码 --> 
  	<filter>
	  	<filter-name>encoding_filter</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>encoding_filter</filter-name>
	  	<url-pattern>/*</url-pattern>
  	</filter-mapping>
  	<!-- 过滤器配置结束 -->
  
    <!-- log4j配置 -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>600000</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <!-- log4j配置结束 -->

  	<filter>
      	<filter-name>httpMethodFilter</filter-name>
      	<filter-class>
          	org.springframework.web.filter.HiddenHttpMethodFilter
      	</filter-class>
  	</filter>
  	
  	<filter-mapping>
      	<filter-name>httpMethodFilter</filter-name>
      	<url-pattern>/*</url-pattern>
  	</filter-mapping>    
    
  	<!-- spring配置文件加载  -->
  	<context-param>
    	<param-name>contextConfigLocation</param-name>
		<param-value>classpath:attendance-servlet.xml</param-value>
  	</context-param>
  	<listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  	</listener>
  	<!-- spring配置文件结束 -->
  
  	<!-- springMVC配置 -->
  	<servlet>
  		<servlet-name>attendance</servlet-name>
  		<servlet-class>
  			org.springframework.web.servlet.DispatcherServlet
  		</servlet-class>
  		<init-param>
        	<param-name>contextConfigLocation</param-name>
        	<param-value>classpath:attendance-servlet.xml</param-value>
    	</init-param>
  		<load-on-startup>1</load-on-startup>
  	</servlet>
  	<servlet-mapping>
  		<servlet-name>attendance</servlet-name>
  		<url-pattern>/</url-pattern>
  	</servlet-mapping>
  	<!-- springMVC配置结束 -->
  
  
  	<welcome-file-list>
    	<welcome-file>index.html</welcome-file>
    	<welcome-file>index.htm</welcome-file>
    	<welcome-file>index.jsp</welcome-file>
    	<welcome-file>default.html</welcome-file>
    	<welcome-file>default.htm</welcome-file>
    	<welcome-file>default.jsp</welcome-file>
  	</welcome-file-list>
</web-app>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SpringMVC是一个基于MVC架构的Web框架,它可以帮助我们快速地开发Web应用程序。在使用SpringMVC时,我们需要在web.xml文件中进行配置。下面是SpringMVC web.xml配置的详解: 1. 配置DispatcherServlet 在web.xml文件中,我们需要配置一个Servlet来处理所有的请求。这个Servlet就是DispatcherServlet。我们需要在web.xml文件中添加以下配置: ``` <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> ``` 在这个配置中,我们指定了Servlet的名称为dispatcherServlet,Servlet的类为org.springframework.web.servlet.DispatcherServlet。我们还指定了一个初始化参数contextConfigLocation,它指定了SpringMVC配置文件的位置。在这个例子中,配置文件的位置为/WEB-INF/springmvc-servlet.xml。最后,我们还指定了Servlet的启动顺序为1。 2. 配置Servlet映射 在web.xml文件中,我们还需要配置Servlet的映射。我们需要将所有的请求都映射到DispatcherServlet上。我们需要在web.xml文件中添加以下配置: ``` <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 在这个配置中,我们将Servlet的名称设置为dispatcherServlet,将URL的模式设置为/。这样,所有的请求都会被映射到DispatcherServlet上。 3. 配置字符编码过滤器 在web.xml文件中,我们还需要配置一个字符编码过滤器。这个过滤器可以确保请求和响应的字符编码都是正确的。我们需要在web.xml文件中添加以下配置: ``` <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> ``` 在这个配置中,我们指定了一个字符编码过滤器。我们将过滤器的名称设置为encodingFilter,将过滤器的类设置为org.springframework.web.filter.CharacterEncodingFilter。我们还指定了字符编码为UTF-8,并强制使用UTF-8编码。最后,我们将过滤器映射到所有的URL上。 4. 配置静态资源处理器 在web.xml文件中,我们还需要配置一个静态资源处理器。这个处理器可以帮助我们处理静态资源,如CSS、JavaScript和图片等。我们需要在web.xml文件中添加以下配置: ``` <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> <url-pattern>*.js</url-pattern> <url-pattern>*.png</url-pattern> <url-pattern>*.jpg</url-pattern> <url-pattern>*.gif</url-pattern> </servlet-mapping> ``` 在这个配置中,我们将所有的静态资源的URL模式都映射到默认的Servlet上。这样,当我们访问静态资源时,就可以直接访问它们了。 以上就是SpringMVC web.xml配置的详解。通过这些配置,我们可以快速地搭建一个SpringMVC应用程序。 ### 回答2: Spring MVC是一种常见的Java Web开发框架,供开发人员快速构建Web应用程序。在使用Spring MVC框架进行开发时,需要在Web.xml文件中进行一些配置来确保正确运行。下面是一个关于Spring MVC Web.xml配置的详细解释。 1. 配置DispatcherServlet 在Web.xml文件中,需要配置DispatcherServlet,这是Spring MVC中的核心Servlet,它接收所有来自客户端的请求,然后将请求分派到相应的控制器上。配置DispatcherServlet时,需要指定Servlet名称、Servlet类、URL模式。 <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 2. 配置Spring ContextLoaderListener Spring MVC框架需要依赖Spring Framework,因此需要在Web.xml中配置一个Spring ContextLoaderListener来加载Spring上下文。Spring ContextLoaderListener会在Web应用程序启动时自动加载Spring配置文件,并将所有Spring bean加载到应用程序上下文中。 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 3. 配置字符集编码过滤器 为了确保应用程序能够处理国际化的文本消息,需要配置一个字符集编码过滤器,将接收到的请求和响应都设置为指定的字符集编码。 <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> 4. 配置Spring MVC资源处理器 Spring MVC框架在处理Web请求时,需要使用资源处理器来加载和处理请求中使用的所有静态文件,如CSS、JavaScript、图片等。可以通过以下方式配置资源处理器: <mvc:resources mapping="/resources/**" location="/resources/" /> 5. 配置SpringMVC的视图解析器 Spring MVC框架使用视图解析器来获取请求处理器返回的视图名称,并将其解析为可执行的视图。通过以下方式配置视图解析器: <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> 以上是关于Spring MVC Web.xml配置的详细解释。这些配置项将确保Spring MVC应用程序正常运行并且能够接收和响应客户端的请求。当然,Web.xml配置也有一些其他的配置项,如果您需要自定义一些配置,可以参考相关的文档或向社区寻求帮助。 ### 回答3: Spring MVC是一种基于Java的Web框架,可以用于快速开发Web应用程序。在Web应用程序中,Web.xml配置文件,用于描述Web应用程序的URL映射和其他关键元素。在SpringMVC中,Web.xml也需要进行配置,以确保SpringMVC能够正确运行。下面我们来详细介绍SpringMVCWeb.xml的配置。 分为以下几个方面进行介绍: 一、基本介绍: SpringMVC中的Web.xml文件主要用于注册SpringMVC模块,并将请求路径与对应的Controller进行映射。Web.xml文件配置了Servlet,Filter,Listener等元素,这些元素都会被由Servlet容器Tomcat读取并执行。 二、配置DispatcherServlet: 处理所有类型请求,即所有请求都交由DispatcherServlet进行处理。 DispatcherServlet有两个参数: 1、contextConfigLocation:指定Spring的上下文配置文件,比如applicationContext.xml等。 2、springmvc:指定Spring MVC 配置文件的路径,比如springmvc-servlet.xml等。 <!-- SpringMVC 配置 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 三、配置CharacterEncodingFilter: CharsetEncodingFilter是在Spring MVC中用于对请求进行编码和解码的过滤器,它可以使用init-param来设置特定的编码。 <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 IoC容器: 我们可以使用ContextLoaderListener来配置Spring IoC容器。 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 五、配置spring-security: 创建spring-security.xml文件用于配置spring-security. <context:annotation-config/> <context:component-scan base-package="com.spring.bean"/> <http> <intercept-url pattern="/**" access="ROLE_USER" /> <access-denied-handler error-page="/403" /> <csrf disabled="true"/> </http> <authentication-manager> <authentication-provider user-service-ref="myUserDetailsService" /> </authentication-manager> 总结: Web.xml文件配置了Servlet,Filter,Listener等元素,这些元素都会被由Servlet容器Tomcat读取并执行。主要是通过配置DispatcherServlet、CharsetEncodingFilter、Spring IoC容器、spring-security等,来实现SpringMVC的配置。除此之外,Web.xml的配置还有很多,以上仅是几个常用的配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值