配置文件web.xml和springmvc-config.xml的

web.xml中配置的DispatcherServlet。
    该servelt会在服务启动的时候立即加载,并且在加载时需要一个springMVC配置文件,默认情况下,会去应用程序文件夹的web-info下找[servlet-name].xml文件,也可以把springMVC配置文件放到程序文件夹的任何地方,用init-param描述,contextConfigLocation描述的就是配置文件所在位置,这样DispatcherServlet会去加载springmvc-config.xml文件,并根据该文件信息创建一个WebApplicationContext容器对象,也成为上下文环境。这个对象继承与ApplicationContext容器,他的初始化与BeanFactory、ApplicationContext有所区别,因为WebApplicationcontext需要ServletContext实例,也就是说他必须在拥有Web容器的前提下才能完成启动SpringWeb应用上下文的工作。有了WebApplicationcontext,就可以使用spring的IOC和AOP等其他功能了。

DispatcherSerlet是前端控制器,核心功能是分发请求,分发给对应的java类,在springMVC中称为Handle
spring2.5之前控制器唯一的方法就是实现org.springframework.web.servlet.mvc.Controller接口,并且只能只处理单一动作的单一请求spring2.5之后控制器可以支持注解的方式,并且无需实现任何接口,可以支持同时处理多个请求.

基于实现Controller接口的控制器的web应用,配置springmvc-config.xml。基于注解的控制器的web应用,配置springmvc-config.xml。视图解析器如果配置前缀和后缀,调用视图时会自动拼接前缀和后缀

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
	http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<!-- 配置加载Spring文件的监听器-->	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<!-- 编码过滤器 -->
	<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>
	<!-- 配置Spring MVC前端核心控制器 -->
	<servlet>
		<servlet-name>crm</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc-config.xml</param-value>
		</init-param>
		<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>crm</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	<!-- 系统默认页面 -->
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

springmvc-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.3.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!-- 加载属性文件 -->
    <context:property-placeholder 
               location="classpath:resource.properties" />
    <!-- 配置扫描器 -->
    <context:component-scan 
               base-package="com.itheima.core.web.controller" />
    <!-- 注解驱动:配置处理器映射器和适配器 -->
    <mvc:annotation-driven />
    <!--配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->
    <mvc:resources location="/js/" mapping="/js/**" />
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/fonts/" mapping="/fonts/**" />
    <mvc:resources location="/images/" mapping="/images/**" />	
    <!-- 配置视图解释器ViewResolver -->
    <bean id="jspViewResolver" class=
    "org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
    </bean>	
    
    <!-- 配置拦截器 -->
	<mvc:interceptors>
    	<mvc:interceptor>
        	<mvc:mapping path="/**" />
        	<bean class="com.itheima.core.interceptor.LoginInterceptor" />
    	</mvc:interceptor>
	</mvc:interceptors>	
</beans>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值