您的位置:首页 > 编程语言 > Java开发

springMVC国际化实现,页面获取错误信息properties中信息

2016-10-17 10:05 447 查看
之前用到SpringMVC国际化的时候,查了很多资料后完成的,这里自己总结一下:

1.首先在eclipse的工程的web.xml配置如下:

在原来的基础上加上这个:

<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>


2.配置springmvc.xml以及propoties文件,使用注解的方式配置:

<!-- controller配置 -->
<!-- 组件扫描 Controller层 -->
<context:component-scan base-package="cn.guojihua.controller"></context:component-scan>

<!-- 注解驱动 -->
<mvc:annotation-driven/>

<!-- 视图解析器 解析JSP视图 默认使用JSTL标签 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 国际化 -->
<bean id="messageSource"
class="org.springframework.context.support.Relo
4000
adableResourceBundleMessageSource">
<!-- 资源文件名 -->
<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>
<!-- 资源文件编码格式 -->
<property name="fileEncodings" value="utf-8" />
<!-- 对资源文件内容缓存时间,单位秒 -->
<property name="cacheSeconds" value="120" />
</bean>

<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>


关于其中的配置解释一下这个:

<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>


这里的classpath:messages配置的是资源路径的路径以及文件名称的头,比如这里设置为messages,那么国际化的资源文件就必须配置成这样:



如果你配置成:

<property name="basenames">
<list>
<value>classpath:messagesasd</value>
</list>
</property>


那么在运行时会出现这样的错误:

DEBUG [http-bio-8080-exec-11] - No properties file found for [classpath:messagesasd_zh_CN] - neither plain properties nor XML
DEBUG [http-bio-8080-exec-11] - No properties file found for [classpath:messagesasd_zh] - neither plain properties nor XML
DEBUG [http-bio-8080-exec-11] - No properties file found for [classpath:messagesasd] - neither plain properties nor XML


这里浏览器默认语言是zh_CN,首先找messagesasd_zh_CN.propoties文件,找不到再找messagesasd_zh,最后找messagesasd,所以这一项配置要配置正确才行。

3.准备工作完成,接下来是controller的编写,直接上代码:

@Controller
public class GuojihuaController implements ApplicationContextAware {

private static ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
applicationContext = arg0;

}

@RequestMapping("/guojihua")
public ModelAndView guojihua(HttpServletRequest request, String langType) {
// 返回的modelAndView
ModelAndView modelAndView = new ModelAndView();

// 判断语言类型,在页面显示不同的国际化版本
Locale locale = null;
if ("cn".equals(langType)) {
locale = new Locale("cn");
// 配置本地信息
request.getSession()
.setAttribute(
SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
locale);
} else if ("en".equals(langType)) {
locale = new Locale("en");
request.getSession()
.setAttribute(
SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
locale);
} else {
// 这里默认的,不用配置也可以
request.getSession().setAttribute(
SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
RequestContextUtils.getLocale(request));
}

// 这是在后台获取国际化信息,然后显示到前台jsp
String guojihua = applicationContext.getMessage("guojihua", null,
RequestContextUtils.getLocale(request));

// 后台保存值
modelAndView.addObject("guojihua", guojihua);

// 返回modelAndView
modelAndView.setViewName("guojihua");
return modelAndView;
}
}


这里使用ApplicationContextAware,相关的解释都在注释里面;

4.最后是jsp页面的编写了,页面使用spring标签输出信息:

加入标签:

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


页面输出以及切换语言的链接:

<a href="guojihua.action?langType=cn">中文</a> | <a href="guojihua.action?langType=en">英文</a><br/>
<spring:message code="guojihua"/>


这是这个的eclipse的工程:

工程连接点这里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐