Spring MVC 学习笔记 对locale和theme的支持

Locale

Spring MVC缺省使用AcceptHeaderLocaleResolver来根据request header中的 Accept-Language 来确定访客的local。对于前端jsp页面上,spring提供了标签<spring:message>来提供从resource文件中获取的文字的动态加载功能。 例如 修改servlet context xml文件中的messageSource部分,增加对多国语言message的code resource的引入。

    <bean id="messageSource"        class="org.springframework.context.support.ReloadableResourceBundleMessageSource"        p:fallbackToSystemLocale="true" p:useCodeAsDefaultMessage="false"        p:defaultEncoding="UTF-8"><description>Base message source to handle internationalization</description><property name="basenames"><list><!-- main resources --><value>classpath:valid/validation</value>                [color=red]<value>classpath:local/message</value>[/color]</list></property></bean>

在 src/main/resources目录下增加local目录,并在其下增加messpage_zh_CN.properties文件,内容为 hello=\u6b22\u8fce,并增加message_en.properties文件内容为hello=welcome。
修改hellworld.jsp,增加如下代码

<h1>[color=red]<spring:message code='hello'/>[/color]</h1>

此时访问http://localhost:8080/mvc,根据你的客户端的不同,将分别显示中文和英文的欢迎语。
除缺省的AcceptHeaderLocaleResolver外,spring mvc还提供了CookieLocaleResolver和SessionLocaleResolver两个localResolver来提供在运行时由客户端强行指定local的功能。 分别使用cookie和session中存储的locale值来指定当前系统所使用的locale.
以SessionLocaleResolver为例,在servlet context xml配置文件中增加如下配置

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

新增一个controller,来提供更换locale功能。

@Controllerpublic class LocalChange {

    @Autowiredprivate LocaleResolver localeResolver;

    @RequestMapping("/changeLocale")public String changeLocal(String locale,            HttpServletRequest request,            HttpServletResponse response){        Locale l = new Locale(locale);        localeResolver.setLocale(request, response, l);return "redirect:helloworld";    } }

可分别访问http://localhost:8080/springmvc/changeLocale?locale=en http://localhost:8080/springmvc/changeLocale?locale=zh_CN 来查看更换语言后的结果。
除以以上方式来变更样式外,spring mvc还提供了一个 LocaleChangeInterceptor拦截器来在request时根据request 参数中的locale参数的内容来实时变更Locale。 示例代码如下 在servlet context xml配置文件中新增拦截器,

        <mvc:interceptor><mvc:mapping path="/*" /><bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /></mvc:interceptor>

此时访问 http://localhost:8080/springmvc/helloworld?locale=en ,可以查看动态更换locale后的效果。

Theme Spring MVC中通过ThemeSource接口来提供对动态更换样式的支持,并提供了ResourceBundleThemeSource这个具体实现类来提供通过properties配置文件对theme中的样式的配置 例如配置文件中 内容为 helloworld=theme/default/css/helloworld.css 而jsp文件中使用 <link rel="stylesheet" type="text/css" href="<spring:theme code='helloworld'/>" /> 来引用对helloworld这个样式文件的引入。由此来实现样式文件的动态引用,从而使spring mvc中可以实现换肤功能。 如果说ThemeSource接口是提供了如何去取当前的theme的code与实际内容的mapping关系,那么spring mvc提供的另外一个interface ThemeResolver则是提供了如何去设置当前使用的theme的手段。   Spring MVC提供了三个ThemeReslover的实现类,分别是 FixedThemeResolver:固定格式的theme,不能在系统运行时动态更改theme. SessionThemeResolver:theme name存放在session中key值为 org.springframework.web.servlet.theme.SessionThemeResolver.THEME 的session attribute中。可在运行中通过更改session中的相应的key值来动态调整theme的值。 CookieThemeResolver:theme name存放在cookie中key值为 org.springframework.web.servlet.theme.CookieThemeResolver.THEME 中。可在运行中通过更改cookie中的相应的key值来动态调整theme的值。 以上Themesource和ThemeResolver在servlet context xml中的配置示例如下

    <beanclass="org.springframework.ui.context.support.ResourceBundleThemeSource"        id="themeSource"><property name="basenamePrefix" value="theme."></property></bean>

<bean id="themeResolver"        class="org.springframework.web.servlet.theme.SessionThemeResolver"><property name="defaultThemeName" value="grey" /></bean>

从以上配置可以看出,我们使用了一个sessionThemeReslover(bean name 必须为themeReslover,因为这个值是hardcode在DispatcherServlet中的),缺省的themeName为grey。 而ThemeSource中我们的配置的basenamePrefix为”theme.”,这里表示spring mvc将从classes/theme/目录下对应的themename.properties中读取配置,例如我们这里配置的是grey,则将从classes/theme/grey.properties中读取theme code的配置。 下面我们将新建3个theme,分别为default,red和blue,存放目录如下。
并修改helloworld.jsp,按前面所介绍的,增加对换肤功能的支持。

<link rel="stylesheet" type="text/css"    href="<spring:theme code='helloworld'/>" /></head><body><h1>Hello World!</h1>    [color=red]<div id="divTheme"></div>[/color]

这里新增一个div来展示换肤前后的效果
新增一个controller,来提供换肤功能。

@Controllerpublic class ThemeChange {private final Log logger = LogFactory.getLog(getClass());

    @Autowiredprivate ThemeResolver themeResolver;

    @RequestMapping("/changeTheme")public void changeTheme(HttpServletRequest request,            HttpServletResponse response, String themeName) {        logger.info("current theme is " + themeResolver.resolveThemeName(request));        themeResolver.setThemeName(request, response, themeName);        logger.info("current theme change to " + themeResolver.resolveThemeName(request));    }}

可访问 http://localhost:8080/springmvc/ 看到一个缺省的灰色的div层, 访问 localhost:8080/springmvc/changeTheme?themeName=red 后,刷新页面,div的样式将发生变化
除以以上方式来变更样式外,spring mvc还提供了一个 ThemeChangeInterceptor 拦截器来在request时根据request 参数中的theme的内容来动态变更样式。 实例代码如下 在servlet context xml配置文件中新增拦截器,

        <mvc:interceptor><mvc:mapping path="/*" /><bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" /></mvc:interceptor>

此时访问 http://localhost:8080/springmvc/?theme=blue ,可以查看动态换肤后的效果。

posted on 2012-02-18 22:58 种菜得瓜 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/crazy-fox/archive/2012/02/18/2357733.html

Spring MVC 学习笔记 对locale和theme的支持相关推荐

  1. Spring MVC 学习笔记一 HelloWorld

    Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...

  2. Spring MVC 学习笔记(整理)

    SpringMVC学习 1.概述 Spring MVC是一种基于Java实现MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行解耦,基于请求-响应模型帮助我们 ...

  3. Spring MVC学习笔记(七)

    2019独角兽企业重金招聘Python工程师标准>>> 配置Spring MVC <?xml version="1.0" encoding="UT ...

  4. Spring MVC 学习笔记 json格式的输入和输出

    Spring mvc处理json需要使用jackson的类库,因此为支持json格式的输入输出需要先修改pom.xml增加jackson包的引用 <!-- json --><depe ...

  5. Spring MVC学习笔记——SiteMesh的使用(转)

    转自 SiteMesh的使用 SiteMesh的介绍就不多说了,主要是用来统一页面风格,减少重复编码的. 它定义了一个过滤器,然后把页面都加上统一的头部和底部. 需要先在WEB-INF/lib下引入s ...

  6. Spring MVC学习笔记

    文章目录 创建一个servlet项目 导入依赖 添加Web框架 编写Servlet 注册这个servlet 编写跳转页面 配置Tomcat 第一个Spring MVC程序 1. web.xml的配置 ...

  7. 【Spring MVC学习笔记 六】SpringMVC框架整合AJAX完成局部刷新

    本篇Blog介绍另一个常用的技术Ajax.虽然Ajax可以脱离SpringMVC去使用,但是SpringMVC对AJax有更好的支持 AJAX概念概述 AJAX即Asynchronous Javasc ...

  8. Spring MVC学习笔记——POJO和DispatcherServlet

    POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称. 使用POJO名称是为了避免和EJB(Enter ...

  9. Spring Boot学习笔记-基础(2)

    Spring Boot学习笔记-基础(2) Spring Boot 优点: – 快速创建独立运行的Spring项目以及与主流框架集成 – 使用嵌入式的Servlet容器,应用无需打成WAR包 – st ...

最新文章

  1. pandas最大的时间间隔_pandas生成时间列表(某段连续时间或者固定间隔时间段)(示例代码)...
  2. Java开源Web Service(转)
  3. python自学书籍顺序-【经验分享】自学Python的学习顺序!附学习资料
  4. linux 系统kill用法
  5. C项目实践--俄罗斯方块(2)
  6. 深度学习试题_高中生物:今年高考试题3点显著变化及5个备考建议!不看准吃亏...
  7. CentOS HarBor安装与配置
  8. Gnome即将满18岁,适用于Docker,Kali Linux 2.0的新工具以及更多新闻
  9. Oracle存储过程的异常处理
  10. HIVE 命令行操作和参数指引
  11. Mybatis源码SqlSession源码分析
  12. 计算机网络第七版第一章答案
  13. fmask云检测 matlab_ENVI5.3.1云检测工具
  14. html两列合并一列,如何将excel中两列数据合并到一列呢
  15. 微信小程序开发详细步骤是什么?
  16. IC人物志-Intel创世人Robert Norton Noyce(罗伯特·诺伊斯)
  17. 在校园网的环境下用树莓派搭建私人云
  18. 2018年度计划清单
  19. 常见的标识符命名风格
  20. forward(转发)和redirect(重定向)有什么区别

热门文章

  1. parcel react_如何使用Parcel捆绑React.js应用程序
  2. java ruby_Java,Ruby和Go,我的天哪!
  3. java case or_java – 在CriteriaBuilder中使用子句和’case w...
  4. linux打开 root .m2,小辣椒M2 (LA-M2)获取ROOT权限教程,新手root必看
  5. 软件测试培训分享:如何划分bug的严重级别
  6. Python培训教程分享:“高效实用” 的Python工具库
  7. Java遍历Map对象的四种方式
  8. 修改360浏览器 标题栏 显示的文字
  9. SQL Server不能启动
  10. 互联网+和创业潮,互联网+前提条件是什么?互联网+做什么?