当前位置: 首页 > 编程日记 > 正文

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

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功能。

@Controller
public class LocalChange {

@Autowired
private 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中的配置示例如下

    <bean
class="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,来提供换肤功能。

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

@Autowired
private 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

相关文章:

iOS逆向(4)-代码注入,非越狱窃取微信密码

利用LLDB对微信进行分析&#xff0c;然后利用分析的结果&#xff0c;再逐步讲解如何Hook微信的登录过程&#xff0c;截获微信密码。 在上一篇文章(APP重签名)中&#xff0c;已经介绍了如何对APP重签名&#xff0c;并且利用XCode将微信跑起来&#xff0c;既然到了这一步&#xf…

java http请求 工具类_Java 实现 Http 请求工具类

1 packagecom.demo.util;23 importjava.io.BufferedReader;4 importjava.io.IOException;5 importjava.io.InputStreamReader;6 importjava.io.OutputStreamWriter;7 importjava.net.URL;8 importjava.net.URLConnection;910 public classHttpUtil {11 /**12 * 向指定URL发送GE…

Entity Framework学习三:查询、插入、更新和删除操作

1.LINQ过滤数据 var query from person in context.Peoplewhere person.FirstName.StartsWith("a")select person; var methodQuery context.People.Where(p > p.FirstName.StartsWith("a")); 两种不同的写法&#xff0c;效果一样。 多条件组合查找…

c/s开发基础自学纪录为主

1&#xff0e;常用属性 &#xff08;1&#xff09;Name属性&#xff1a;用来获取或设置窗体的名称。 &#xff08;2&#xff09;WindowState属性&#xff1a;用来获取或设置窗体的窗口状态。 &#xff08;3&#xff09;StartPosition属性&#xff1a;用来获取或设置运行时窗体的…

不错的威盾PHP加密专家解密算法

<?php /*********************************** *威盾PHP加密专家解密算法 http://www.my400800.cn ***********************************/ $filename"phpfilename.php";//要解密的文件 $lines file($filename);//0,1,2行 //第一次base64解密 $content"&quo…

java网络编程udp_java网络编程 UDP网络编程问题

为什么我的代码运行后&#xff0c;黑窗口&#xff0c;不显示一端发来的数据&#xff0c;而是黑窗口打印很多空格&#xff1f;请帮一下&#xff0c;初学者&#xff01;谢谢&#xff0c;下面是二个具有发送和接受功能的代码&#xff1f;发送端————importjava.net.*;imp...为什…

权限组件(10):三级菜单的展示和增删改查

效果图&#xff1a; 三级菜单的实现和一级、二级菜单差不多。需要注意的是增加三级菜单时&#xff0c;三级菜单是用户提交后在后台通过二级菜单的id添加的。 一、路由分发 rbac/urls.py ... from django.urls import re_pathfrom rbac.views import menu ...urlpatterns [...…

ROS知识(4)----初级教程之常见问题汇总

一、开机启动ROS的工作空间的路径设置失败 现象&#xff1a;在教程&#xff1a;http://wiki.ros.org/cn/ROS/Tutorials/CreatingPackage中的第5.1小节&#xff0c;运行以下命令失败&#xff1a; $ rospack depends1 beginner_tutorials 提示错误&#xff1a;[rospack] Error: …

sql server 海量数据速度提升:SQL优化-索引(11) 【转】

12、高效的TOP 事实上&#xff0c;在查询和提取超大容量的数据集时&#xff0c;影响数据库响应时间的最大因素不是数据查找&#xff0c;而是物理的I/0操作。如&#xff1a; select top 10 * from ( select top 10000 gid,fariqi,title from tgongwen where neibuyonghu办公室or…

java重定向带参数_急 求助重新封装重定向带参数问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼这是我写的代码 不知道行不行 求助package base.web.resolver.result;import java.util.HashMap;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.logging.log4j.…

Java程序员从笨鸟到菜鸟之(五)java开发常用类(包装,数字处理集合等)(下)...

本文来自&#xff1a;曹胜欢博客专栏。转载请注明出处&#xff1a;http://blog.csdn.net/csh624366188 写在前面&#xff1a;由于前天项目老师建设局的项目快到验收阶段&#xff0c;所以&#xff0c;前天晚上通宵&#xff0c;昨天睡了大半天&#xff0c;下午我们宿舍聚会&#…

对数组中的数字 1 和 2 进行排序,使得数字 1、2 分别位于前、后部分

问题描述&#xff1a;假设某个数组中只有数字 1 和 2&#xff0c;进行排序&#xff0c;使得数字 1 位于数组前部分&#xff0c;数字 2 位于后部分。 这道算法题其实不是很难&#xff0c;使用各种排序算法应该都能解出&#xff0c;但是若要考虑性能问题&#xff0c;那就得选择一…

@class和#import

class 作用&#xff1a; 可以简单的引用一个类 简单使用&#xff1a; class Dog; 仅仅是告诉编译器&#xff0c;Dog是一个类&#xff1b;并不会包含Dog这个类的所有内容 具体使用&#xff1a; 在.h文件中使用class引用一个类 在.m文件中使用#import包含这个类的.h文件 作用上的…

java登陆界面连接数据库_java 登陆界面怎么写,连接数据库后

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼界面是package 界面类;import javax.jws.soap.SOAPBinding.Use;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing…

C# 汉字编码GB2312转换

功能界面 源码&#xff1a; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace wordsConvert {public partial class Fo…

python批量爬取文档

最近项目需要将批量链接中的pdf文档爬下来处理&#xff0c;根据以下步骤完成了任务&#xff1a; 将批量下载链接copy到text中&#xff0c;每行1个链接&#xff1b;再读txt文档构造url_list列表&#xff0c;利用readlines返回以行为单位的列表&#xff1b;利用str的rstrip方法&a…

[Android]webview直接加载网页允许JS,进度条,当前应用内跳转

webview&#xff0c;用于在应用里面直接加载网页本代码参考了&#xff1a;官方的webview实例介绍&#xff1a;https://developer.android.com/guide/tutorials/views/hello-webview.html 加上进度条&#xff1a; http://blog.csdn.net/stoneson/article/details/6068089 整个源…

ubuntu 14.04 安装java_Ubuntu 14.04中安装Java

第三&#xff1a;在Ubuntu 和 Linux Mint上安装Java看了各种类型"java";的不同之后&#xff0c;让我们看如何安装他们。1)在Ubuntu和Linux Mint上安装JRE打开终端&#xff0c;使用下面的命令安装JRE&#xff1a;sudo apt-get install default-jre2)在Ubuntu和Linux M…

C# 生成系统唯一号

生成唯一号&#xff1a;思路&#xff0c;根据yymmddhhmmss自增长号唯一服务器号( SystemNo)生成唯一码&#xff0c;总长度19&#xff0c;例如&#xff1a;1509281204550000101. public class UniqueNumber{private static long num 0;//流水号private static object lockObj …

EBS上用过的一些接口表整理信息

AP接口表&#xff1a;AP_INVOICES_INTERFACEAP_INVOICE_LINES_INTERFACE涉及的请求&#xff1a;应付款管理系统开放接口导入涉及案例&#xff1a; 运费导AP、费用导APPO接口表&#xff1a;申请&#xff1a;PO_REQUISITIONS_INTERFACE_ALL涉及请求&#xff1a;导入申请采购&…

linux源码编译安装nginx

1.从nginx的官方网站下载nginx的安装源码包&#xff0c;要下载.gz格式的包才是linux安装包 网址http://nginx.org/download/ wget http://nginx.org/download/nginx-1.5.9.tar.gz 2.解压 tar -zxvf nginx-1.5.9.tar.gz yum -y install pcre-devel gcc gcc-c autoconf automak…

usr share里没有mysql_无法在ubuntu 12.04上安装mysql,找不到消息文件’/usr/share/mysql/errmsg.sys’...

尝试使用apt-get安装mysql但它失败了# apt-get install MysqL-serverReading package lists... DoneBuilding dependency treeReading state information... DoneThe following extra packages will be installed:MysqL-server-5.5Suggested packages:tinycaThe following NEW …

android:更改PagerTabStrip背景颜色,标题字体样式、颜色和图标,以及指示条的颜色...

1.更改PagerTabStrip背景颜色我们直接在布局中设置background属性可以&#xff1a;<android.support.v4.view.ViewPagerandroid:id"id/pager"android:layout_width"fill_parent"android:layout_height"fill_parent" ><android.support.…

敏捷开发日常跟进系列之二:燃尽图(中)

这是敏捷开发日常跟进系列的第二篇&#xff08;栏目目录&#xff09;。 迭代及燃尽图的目标 燃尽图的目标是完成迭代的目标&#xff0c;迭代的目标是什么呢&#xff1f; 1. 按产品经理的要求&#xff0c;交付计划会中计划的用户故事 2. 尽量完成1 之后还会看到&#xff0c;这个…

[python][jupyter notebook]之菜鸟安装[pyecharts]中Geo或Map显示问题

作为菜鸟&#xff0c;在学习使用pyecharts模块进入jupyter notebook的时候&#xff0c;又遇到了问题——那就是&#xff0c;可以使用一下代码&#xff0c;导入Geo和Map模块&#xff0c;但是弄了之后看不见地图。 from pyecharts import Geo from pyecharts import Map 所以&…

c语言多线程mysql_多线程读写mysql数据库

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼unsigned int __stdcall scan(PVOID pM){char ip[20];strcpy(ip, (char*)pM);MYSQL mysql;MYSQL_RES* result;//初始化mysql句柄mysql_init(&mysql);//连接mysql数据库if(!mysql_real_connect(&mysql,"localhost"…

[C#,Java,PHP] - IMAP文件夹名称编码和解码方法

[C#] 来源&#xff1a;http://www.oschina.net/code/snippet_110991_2237 // 编码private string IMAPEncode(string folder){string rtn "", base64;int index 0; Regex regAsis new Regex("\G(?:[\x20-\x25\x27-\x7e])"); Regex reg26 new Rege…

fzu 2150 Fire Game 【身手BFS】

称号&#xff1a;fzu 2150 Fire Game &#xff1a;给出一个m*n的图&#xff0c;‘#’表示草坪&#xff0c;‘ . ’表示空地&#xff0c;然后能够选择在随意的两个草坪格子点火。火每 1 s会向周围四个格子扩散&#xff0c;问选择那两个点使得燃烧全部的草坪花费时间最小&#xf…

K-Means聚类算法原理

来自&#xff1a;https://www.cnblogs.com/pinard/p/6164214.html K-Means算法是无监督聚类算法&#xff0c;它有很多变体。包括初始化优化K-Means&#xff0c;距离计算优化elkan K-Means算法和大样本优化Mini Batch K-Means算法。 1. K-Means原理 K-Means算法思想&#xff1a;…

safari java插件故障_safari flash插件故障怎么办 mac safari flash插件故障解决方法

近几日&#xff0c;许多网友都在关注safari flash插件故障怎么办 mac safari flash插件故障解决方法这个话题&#xff0c;那么safari flash插件故障怎么办 mac safari flash插件故障解决方法具体情况是怎么样的呢&#xff1f;safari flash插件故障怎么办 mac safari flash插件故…