--资源文件引入
第一种方式
<context:property-placeholder location="classpath*:/xxx.properties" ignore-resource-not-found="true" ignore-unresolvable="true" />
第二种方式
<bean id="PropertiesConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:log4j.properties</value> </list> </property> </bean>
--自动扫描注解组件
<context:component-scan base-package="xom.xxx..."> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
--自动注册DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter
<mvc:annotation-driven />
--交给应用容器处理静态资源请求
<mvc:default-servlet-handler/>
当加入该配置后,Spring会添加一个org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler专门来检查静态资源的请求,然后转给应用容器(如Tomcat、Jetty、WEBLOGIC等)默认的Servlet进行处理,Spring自已不作处理,如果不是静态资源的请求,才由DispatcherServlet继续处理。
--交给Spring自己处理静态资源请求
<mvc:resources location="/,classpath:/META-INF/publicResources/" mapping="/resources/**"/>
当加入该配置后,由Spring MVC框架自己处理静态资源,并添加一些有用的附加值功能。比如,允许静态资源放在任意位置(可以在jar包中、可以在WEB-INF中、可以是classpath路径),完全打破传统的只能将静态资源存放在应用的根路径下(即WEB-INF上面一层目录)这一限制。同时,增加了缓存设置,一般可将该时间设置为一年,以充分利用浏览器端的缓存。
以上配置将Web根路径"/"及类路径下 /META-INF/publicResources/ 的目录映射为/resources路径。假设Web根路径下拥有images、js这两个资源目录,在images下面有bg.gif图片,在js下面有test.js文件,则可以通过 /resources/images/bg.gif 和 /resources/js/test.js 访问这二个静态资源。
--配置指定请求的处理方式
<mvc:view-controller path="/" view-name="forward:/index"/> <mvc:view-controller path="/" view-name="redirect:/index"/> <mvc:view-controller path="/" view-name="index"/>
第一种,当请求http://127.0.0.1:8080/release/时,将会通过forward的方式,请求交给@RequestMapping("/index")方法进行处理。
第二种,当请求http://127.0.0.1:8080/release/时,将会通过redirect的方式,请求交给@RequestMapping("/index")方法进行处理。
第三种,当请求http://127.0.0.1:8080/release/时,将会根据视图解析器,直接跳转到相应的页面。