springmvc-4 springmvc视图解析、国际化、静态资源处理

来源:互联网 发布:淘宝装修市场怎么进 编辑:程序博客网 时间:2024/06/07 03:03

Springmvc视图解析

视图即view,例如“/jsp/login.jsp”

视图解析即view添加前缀和后缀,如【“/jsp/login.jsp”】配置了视图解析器后就会自动添加/”和“.jsp”,

StringModelAndViewViewModeMap这些请求处理方法返回值类型的底层都是ModelAndView实现的

视图解析器类型:

InternalResourceViewResolverjsp解析器

FreeMarkerViewResolver

BeanNameViewResolver

XmlViewResolver

需要配置视图解析器:,在springmvc配置中创建《bean

创建InternalResourceViewResolver

<!-- 配置视图解析器 --><bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--配置视图的前缀和后缀 prefix 前缀 suffix后缀 --><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean>

国际化

需要一个超类支持,MessageSource(消息源头)接口,它有两个实现类

org.springframework.context.support.ResourceBundleMessageSourceorg.springframework.context.support.ReloadableResourceBundleMessageSource

国际化条件:浏览器环境,properties资源文件(例:my_zh_CN.properties),

将浏览器与资源文件关联起来(spring配置中配置MessageSource

<!-- 注意一定要用messageSource作为bean名称,只需要设置basename即可 --><bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basename" value="cn/springmvc/resource/my"></property></bean>

MessageSource装配到controller中,用它的getMessage()方法取到资源文件中的key

Jsp中使用国际化:

首先引用一个标签

<%@taglib uri="http://www.springframework.org/tags"  prefix="tag" %>

通过code获取资源文件中的key

<tag:message code=”userName”></tag:message>

通过URL参数指定国家语言

很多国际型的网站都允许通过一个请求参数控制网站的本地化,如www.xxx.com? locale=zh_CN返回对应中国大陆的本地化网页,而www.xxx.com?locale=en返回本地化为英语的网页。这样,网站使用者可以通过URL的控制返回不同本地化的页面,非常灵活

配置如下:

<!-- 该拦截器用于拦截url上的参数 只是 当jsp经过action之后 才会将当前的国家和语言储存在session中 同时从session中获取 --><mvc:interceptors><bean  id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"><property name="paramName" value="local"></property></bean></mvc:interceptors><!-- 参数需要被临时存储在某个地方当用户在次访问时使用之前的参数 --><bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>

静态资源处理

自我理解:在web-INF下的所有资源都是安全的,如果配置了静态资源路径映射就可以访问web-inf下的资源,

示例配置如下:

<mvc:resources mapping="/img/**"  location="/WBE-INF/img/"/>

/WBE-INF/img/映射到/img/**中,要想访问/WBE-INF/img中的资源就可以通过/img/****代表所有资源)来访问。

spring4.2servlet3.0Javaee6)支持

spring4.0servlet2.5Javaee5)支持