SpringMVC学习笔记(二)-----配置文件

来源:互联网 发布:显卡超频软件哪里 编辑:程序博客网 时间:2024/06/07 22:48

web.xml

Maven自动生成的web.xml是默认不支持el表达式。将其更改成2.4版本以上的。
更改文件头为如下格式:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee           http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">           ………………</web-app>

并为其加上Spring的配置文件,以及配置文件的路径。

    <!-- Spring应用上下文,理解层次化的ApplicationContext -->    <context-param>       <param-name>contextConfigLocation</param-name>       <param-value>/WEB-INF/configs/spring/applicationContext*.xml</param-value>    </context-param>

SpringMVC的配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context.xsd           http://www.springframework.org/schema/mvc           http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <!-- 本配置是工名为mvc-dispatcher的DispatcherServlet使用,提供其相关的Spring MVC配置 -->    <!-- 启用Spring基于annotation的DI,使用户可以在Spring MVC中使用Spring的强大功能 。 -->    <!-- 激活@Required @AutoWire, JSR 250's @PostConsttruct,@PreDestory and @Resouce等标注 -->    <context:annotation-config />    <!-- DispatcherServlet 上下文,只搜索@Controller标注的类 不搜索其他标注的类 -->    <context:component-scan base-package="com.shen.mvcdemo">        <context:include-filter type="annotation"            expression="org.springframework.stereotype.Controller" />    </context:component-scan>    <!-- HandlerMapping,无需配置,Spring MVC可以默认启动 。       DefaultAnnotationHandlerMapping annotation-driven HandlerMapping    -->    <!-- 扩充了注解驱动,可以讲请求参数绑定到控制器参数  -->    <mvc:annotation-driven />    <!-- 静态资源处理,css,js,imgs -->    <mvc:resources location="/resources/" mapping="/resources/**" />    <!-- 配置ViewResolver。         可以使用多个ViewResolver。         使用order属性排序。         InternalResourceViewResolver放在最后。     -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass"            value="org.springframework.web.servlet.view.JstlView" />        <property name="prefix" value="/WEB-INF/jsps/" />        <property name="suffix" value=".jsp"></property>    </bean></beans>

Spring的配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context.xsd           http://www.springframework.org/schema/mvc           http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <!-- Spring的注解支持 -->    <context:annotation-config />    <!-- Spring的自动扫描组件,除了Controller标记 -->    <context:component-scan base-package="com.shen.mvcdemo">        <context:exclude-filter type="annotation"            expression="org.springframework.stereotype.Controller" />    </context:component-scan></beans>