springMVC国际化实现,页面获取错误信息properties中信息
2016-10-17 10:05
447 查看
之前用到SpringMVC国际化的时候,查了很多资料后完成的,这里自己总结一下:
1.首先在eclipse的工程的web.xml配置如下:
在原来的基础上加上这个:
2.配置springmvc.xml以及propoties文件,使用注解的方式配置:
关于其中的配置解释一下这个:
这里的classpath:messages配置的是资源路径的路径以及文件名称的头,比如这里设置为messages,那么国际化的资源文件就必须配置成这样:
如果你配置成:
那么在运行时会出现这样的错误:
这里浏览器默认语言是zh_CN,首先找messagesasd_zh_CN.propoties文件,找不到再找messagesasd_zh,最后找messagesasd,所以这一项配置要配置正确才行。
3.准备工作完成,接下来是controller的编写,直接上代码:
这里使用ApplicationContextAware,相关的解释都在注释里面;
4.最后是jsp页面的编写了,页面使用spring标签输出信息:
加入标签:
页面输出以及切换语言的链接:
这是这个的eclipse的工程:
工程连接点这里
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的工程:
工程连接点这里
相关文章推荐
- 获取错误信息并到指定页面
- Apache支持.htaccess以及“No input file specified” 和“ 重新载入页面以获取源代码”错误信息解决方案
- 基于jQuery.i18n.properties插件实现前端页面国际化
- springMVC 校验时,CustomValidationMessages.properties中的错误提示信息的中文乱码 问题
- asp.net 获取错误信息并到指定页面
- 用Android studio创建手机信息页面并实现国际化
- Python实现可获取网易页面所有文本信息的网易网络爬虫功能示例
- Python+Selenium 自动化实现实例-获取页面元素信息
- 基于jQuery.i18n.properties 实现前端页面的资源国际化
- SpringMVC+HibernateValidator,配置在properties文件中的错误信息回显前端页面出现中文乱码
- Java 简单解决springmvc获取properties文件里面中文内容出现论码问题(我遇到的问题是数据提交检验时返回的错误提示出现乱码)
- 获取错误信息并到指定页面
- .获取错误信息并到指定页面
- 利用java filter 实现业务异常拦截 跳转到错误信息提示页面
- SpringMVC之国际化资源显示错误信息
- SpringMVC+HibernateValidator,配置在properties文件中的错误信息回显前端页面出现中文乱码
- SpringMVC和FreeMarker整合Hibernate-validator在页面渲染校验错误信息
- Asp.net项目基于jQuery.i18n.properties 实现前端页面的资源国际化
- 用Android studio创建手机信息页面并实现国际化
- SpringMVC+HibernateValidator,配置在properties文件中的错误信息回显前端页面出现中文乱码