汇总SpringMVC
配置文件springmvc.xml
相关配置。
基本配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 把controller交给spring管理,可配置多个 -->
<context:component-scan base-package="com.xxx.controller,com.yyy.controller" />
<!-- 引入外部资源文件,classes根目录下 -->
<context:property-placeholder location="classpath:resources.properties"/>
<!-- 注解驱动 -->
<mvc:annotation-driven>
<!-- HTML 消息转换 utf-8,解决@ResponseBody中文乱码 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 放行静态资源 -->
<mvc:default-servlet-handler />
<!-- 视图解析器,设置逻辑视图前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 根目录重定向 /admin/index-->
<mvc:view-controller path="/" view-name="redirect:/admin/index" />
<!-- 配置sprignmvc 拦截器 ,多个拦截器,顺序执行 -->
<mvc:interceptors>
<!-- 管理后台登录拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/admin/**" />
<bean class="com.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 配置Spring MVC文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传文件大小,单位为字节(10MB) -->
<property name="maxUploadSize" value="20971520"></property>
<!-- 请求的编码格式,必须和jsp的pageEncoding属性一致,以便正确读取表单内容,默认为iso-8859-1 -->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
</beans>
增强配置
重定向
- 如果当前路径是
/
则重定向到/admin/index
<mvc:view-controller path="/" view-name="redirect:/admin/index"/>
- 如果当前路径是
/
则交给相应的视图解析器直接解析为视图
请求:/
,返回视图/WEB-INF/jsp/admin/index.jsp
<mvc:view-controller path="/" view-name=admin/index"/>
静态资源
放行静态资源文件,或访问jsp
不进Controller
。
- 第一种方案:放行静态资源文件
在springmvc.xml
添加默认的servlet
处理器注解,将静态资源的请求交给Web
服务器默认的servlet处理。
<!-- springmvc.xml 添加注解 -->
<mvc:default-servlet-handler />
- 第二种方案:将映射路径指指定到需要放行的文件路径
此方案可将资源文件放在项目任意位置,如:放在WEB-INF
目录,可将资源文件打入jar
包;
因location
属性是Resources
类型,也可使用classpath:
等的资源前缀指定资源位置。
<!-- cache-period:静态资源文件在浏览器的缓存时间,1年=365天=31556926秒,一般设小点;
在输出静态资源时,会根据配置设置好响应报文头的Expires 和 Cache-Control值。
location="/img/,/css/,js/,/static/,/WEB-INF/static/,classpath:/META-INF/publicResources/"
如下:拦截所有以 /static 开头的所有请求路径,并当作静态资源交由Servlet处理
-->
<mvc:resources mapping="/static/**" location="/static/" cache-period="31556926"/>
- 第三种方案:在
web.xml
配置默认的servlet-mapping
,放行指定目录和文件类型。
在web.xml
配置默认的servlet-mapping
,设置放行的资源类型或目录。
此方案处理的静态文件方式不能访问WEB-INF
下的文件,静态文件不能放在该目录下。一般放在webapp
下面,WEB-INF
外面。
<!-- web.xml添加默认servlet -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
....
</servlet-mapping>
xml读取propterties文件参数
- Maven工程在
src/main/resources
下创建资源文件resources.properties
;写入key=value
类型数据。 - 在
xml
配置加载xxx.properties资源文件。
Spring有个<context:property-placeholder location="classpath:xxx.properties"/>
标签,用来加载properties
配置文件,location
是配置文件的路径。- 使用
EL
表达式根据key获取xxx.properties文件中对应的值。
<!-- resources.propterties文件 -->
mapping_path=static
local_path=static
<!-- springmvc.xml文件 -->
<!-- 引入外部资源文件 -->
<context:property-placeholder location="classpath:resources.properties"/>
<!-- 根据key获取在资源文件中的值 -->
<mvc:resources mapping="/${mapping_path}/**" location="/${local_path}/" cache-period="31556926"/>
应用配置
在springmvc.xml
文件的beans
标签里添加bean
。
shiro配置
<!-- Shiro注解开启 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor" />
<!--
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
-->
<!-- springMVC异常处理类 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthenticatedException">redirect:/admin/login</prop>
<prop key="org.apache.shiro.authz.UnauthorizedException">404</prop>
</props>
</property>
</bean>
参考资料
更多内容请访问:IT源点
注意:本文归作者所有,未经作者允许,不得转载