【springmvc学习】配置文件

         

         springmvc是 基于DispatcherServlet的mvc框架,每一个请求最先访问的是DispatcherServlet,它负责response和request请求给相应的handler,DispatcherServlet是继承自HttpServlet的,所以需要在web.xml文件中配置前端处理器。

        我们先看一下web.xml文件,它里面配置的内容包括:前端控制器、springmvc配置文件的位置、请求的形式等 

<!-- Spring MVC配置 -->
<!-- ====================================== -->
<servlet>
    <servlet-name>springmvc</servlet-name>  <!--配置前端控制器-->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--通过contextConfigLocation来加载springmvc配置文件的信息,
	默认的位置为WEB-INF/前端控制器的servlet名字-servlet.xml,如spring-servlet.xml-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>Classpath:springmvc.xml</param-value>  
    </init-param>
   <!--表示随着servlet服务而启动-->
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
	<!--什么的后缀形式请求交给前端控制器处理-->
    <url-pattern>*.action</url-pattern>
</servlet-mapping>
  

<!-- Spring配置 -->
<!-- ====================================== -->
<!--默认加载spring的消息机制-->
<listener>
   <listenerclass>
     org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
  
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/applicationContext.xml</param-value>
</context-param>

     servlet的拦截方式有很多种,比如固定后缀拦截,.action,.do。拦截所有 /,但是拦截所有的时候会将静态资源也拦截掉。


      接下来我们来看一下springmvc.xml文件,springmvc的开发模式有注解和非注解俩种情况,非注解的话,需要配置处理器适配器和处理器映射器,比较麻烦,注解的开发形式比较简单,但是也是有很多种的,实际开发中比较用的是<mvc:annotation-driven> 


<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-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 设置使用注解所在的包 -->
	<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>
	
	<!-- 使用 mvc:annotation-driven代替注解映射器和注解适配器配置
	mvc:annotation-driven默认加载很多的参数绑定方法,
	比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置
	注解解析器RequestMappingHandlerMapping和注解适配器RequestMappingHandlerAdapter
	实际开发时使用mvc:annotation-driven
	 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	

	<!-- 视图解析器,解析jsp等,处理器controller负责返回值,但是到底返回的是什么的视图,需要视图解析器控制,
	jsp常用的视图解析器是InternalResourceViewResolver,他要求一个前缀和一个后缀-->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置jsp路径的前缀 -->
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<!-- 配置jsp路径的后缀 -->
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

        springmvc.xml中需要配注解要扫描的包,视图解析器的路径,要采用注解的开发等,当然了,异常处理,校验错误,拦截器等都是要在springmvc中配置的。


        

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值