对于习惯于使用EL表达式的人来说,用struts的message标签简直是种倒退。message用到地方非常多,每次都敲<bean:message key="...."/>,在这个标签污染严重的时代可不是什么好事啊。
幸好,将其转换为EL表达式不难。
要点:
EL表达式对java.util.Map对象的点运算符的递归调用。举例登录页面,${message.login.username}。
解释一下:
引用
只要request里面有个属性名叫"message"的Map,这个Map里有个字符串"login"的key,这个Key指向一个Map,而这个Map里有个字符串"username"的key,这个key指向一个字符串。
而这个字符串就是我们要的Message。我们要做的就是把这些MessageResource文件中的key转换成这样的格式 -----
message.login.username。
唯一的缺点就是需要有一个"根"key在JSP的某个Context上下里(最好是Session,下面会讲到)。"message"就是"根"key,但我们可以让它更简便些${_msg.login.username}或者干脆${_.login.username}.这样,资源文件中的键值对就被转换到树形的Map群里了,这里我们给它个名字“
资源Map”。
好了,还有一个需要解决的问题,国际化。如果对struts有研究的话,应该会知道用户的语言种类是记录在HttpSession "org.apache.struts.action.LOCALE"(struts的org.apache.struts.Globals.LOCALE_KEY)属性中的java.util.Locale对象。通过HttpSessionAttributeListener可以侦测到其值变化(用户改变语言的时候)。这样,只需要一个全局的Map记住所有资源文件转换而成的“
资源Map”,key就是Locale.toString()。只要用户的Locale发生了变化,通过侦听器切换用户Session的“根”key指向“
资源Map”即可。
废话不多说,开始行动:
第一个,先做一个读取资源文件并转换“
资源Map”的类MessagePreLoader,假设你已经用上Spring了。
package com.your.struts.base.bean;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.factory.InitializingBean;
public class MessagePreLoader implements InitializingBean {
/**
* 默认资源Map对应的Key
*/
private static final String DEFAULT_LOCAL_KEY = "default";
/**
* 全局的Map,所有资源Map的挂载点。
*/
private Map resourceHolder = new HashMap();
/**
* struts的资源文件的位置。假设struts的配置是:
* <message-resources parameter="com.your.struts.message.ApplicationResources" >
* 那么此处则是com/your/struts/message/ApplicationResources
* 所有“.”换成“/”
*/
private String resourceName ;
/**
* 所支持的语言对象集合.
*/
private String[] locales = {};
/**
* 设定资源文件是xml格式还是普通的properties文件。此处默认是xml格式(不用借助插件就可以直接用XML编辑器编辑)
*/
private boolean xmlResouce=true;
public boolean isXmlResouce() {
return xmlResouce;
}
public void setXmlResouce(boolean xmlResouce) {
this.xmlResouce = xmlResouce;
}
public String[] getLocales() {
return locales;
}
public void setLocales(String[] locales) {
this.locales = locales;
}
public String getResourceName() {
return resourceName;
}
public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}
public Map getResourceHolder() {
return resourceHolder;
}
private void addMap(String localeKey) throws Exception{
String name=null;
if(DEFAULT_LOCAL_KEY.equals(localeKey)){
name=this.resourceName+(xmlResouce?".xml":".properties");
}else{
name=this.resourceName+"_"+localeKey+(xmlResouce?".xml":".properties");
}
Map map = new HashMap();
this.resourceHolder.put(localeKey, map);
ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
InputStream is = classLoader.getResourceAsStream(name);
Properties props = new Properties();
if(xmlResouce){
props.loadFromXML(is);//此处是因为本人项目所用的资源文件是xml,方便编辑而不必用特殊的插件。
}else{
props.load(is);//不用InputStreamReader的方式是因为struts资源文件本身就只使用InputStream方式。
}
for(Iterator iter = props.keySet().iterator();iter.hasNext();){
String key = (String)iter.next();
String[] strs=key.split("\\.");
String value = props.getProperty(key);
Map last = map;
for (int j = 0; j < strs.length; j++) {
Object o=last.get(strs[j]);
if(o==null){
if(j!=strs.length-1){//不是最后一个key
o=new HashMap();
last.put(strs[j], o);
last=(Map)o;
}else{//最后一个Key
last.put(strs[j], value);
}
}else{
if(j!=strs.length-1){//不是最后一个key
if(o instanceof String)throw new Exception(" is a java.lang.String ,expact java.util.Map," +
"cause by key conflict,such as: \"x.y.z\" and \"x.y\" all exists. current key:"+key);
last=(Map)o;
}else{//最后一个Key
throw new Exception(" dulpicat key exists , current key:"+key);//此类情况应该永远不会发生
}
}
}
}
}
public void afterPropertiesSet() throws Exception {
this.addMap(DEFAULT_LOCAL_KEY);
for (int i = 0; i < locales.length; i++) {
this.addMap(locales[i]);
}
}
public Map getLocaleMessageSet(String localeKey){
Object obj=this.resourceHolder.get(localeKey);
return (Map)(obj==null?this.resourceHolder.get(DEFAULT_LOCAL_KEY):obj);
}
Spring 配置:
<bean id="messagePreLoader" class="com.your.struts.base.bean.MessagePreLoader">
<property name="xmlResouce" value="false"></property>
<property name="resourceName" value="com/your/struts/message/ApplicationResources"></property>
<property name="locales">
<list>
<value>zh_CN</value>
<value>en</value>
</list>
</property>
</bean>
制作SessionListener的委派类:
SessionListenerDelegate用于侦听locale信息的改变。
package com.your.servlet.listener;
import java.util.Locale;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.your.struts.base.bean.MessagePreLoader;
public class SessionListenerDelegate implements HttpSessionAttributeListener{
/**
* Session里面资源Map的Key
*/
public static final String MESSAGE_SESSION_KEY="_msg";
public SessionListenerDelegate() {
super();
}
private static final Log logger = LogFactory.getLog(SessionListenerDelegate.class);
/**
* 侦测语言属性的变化,并设置用户对应的资源Map
* @param bindingEvent
*/
private void setLocale(HttpSessionBindingEvent bindingEvent) {
if(bindingEvent.getName().equals(Globals.LOCALE_KEY)){
HttpSession session=bindingEvent.getSession();
Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
ApplicationContext ctx = (ApplicationContext) WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext());
MessagePreLoader messagePreLoader = (MessagePreLoader) ctx.getBean("messagePreLoader");
String localeKey=null;
if(locale!=null){
localeKey=locale.toString();
}
Object localeMsgSet= messagePreLoader.getLocaleMessageSet(localeKey);
session.setAttribute(MESSAGE_SESSION_KEY, localeMsgSet);
}
}
public void attributeAdded(HttpSessionBindingEvent bindingEvent) {
setLocale(bindingEvent);
if(logger.isDebugEnabled())logger.debug("attribute added. name:{"+bindingEvent.getName()+"} old value:"+bindingEvent.getValue()+" new value:"+bindingEvent.getSession().getAttribute(bindingEvent.getName()));
}
public void attributeRemoved(HttpSessionBindingEvent bindingEvent) {
if(logger.isDebugEnabled())logger.debug("attribute remove. name:{"+bindingEvent.getName()+"} value:"+bindingEvent.getValue());
}
public void attributeReplaced(HttpSessionBindingEvent bindingEvent) {
this.setLocale(bindingEvent);
if(logger.isDebugEnabled())logger.debug("attribute replace. name:{"+bindingEvent.getName()+"} old value:"+bindingEvent.getValue()+" new value:"+bindingEvent.getSession().getAttribute(bindingEvent.getName()));
}
}
web.xml里面:
<listener>
<listener-class>
com.your.servlet.listener.SessionListenerDelegate
</listener-class>
</listener>
这样。我们就可以在JSP中使用${_msg....}这样的EL表达式来访问国际化的资源信息了。
<table>
<tr><td>${_msg.commons.login.username}:</td><td><input type='text' name='j_username' value='<c:if test="${not empty param.login_error}"><c:out value="${lastUserName}"/></c:if>'/></td></tr>
<tr><td>${_msg.commons.login.password}:</td><td><input type='password' name='j_password'></td></tr>
</table>
没有了message标签,输入也方便了。何乐不为?
最后,需要注意的地方,你的message文件中不能出现这样的打架现象:
commons.login=User Login
commons.login.username=User Name
这样小小的请求,我想你是可以忍受的。
另外,如果觉得这个方式对你项目没有帮助,请勿使用。。。
分享到:
相关推荐
struts jstl el表达式示例代码 struts jstl el 表达式 源码
struts2标签和OGNL表达式struts2标签和OGNL表达式
Struts2的标签库及OGNL表达式语言
帮助文档jQuery1.8+struts标签+正则表达式全中文,其中还有例子,非常适合速查,速学,容易理解!准确实用!
struts2标签和OGNL表达式.pdf
struts2标签使用方法以及el表达式的使用,整理收集供参考
struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签
深入了解struts1.x中的 struts - config.xml 弄清楚struts-config.xml中各项元素的作用,对于我们构建web项目有莫大的好处。<struts-config>是struts的根元素,它主要有8个子元素,DTD定义如下:
struts2标签库struts2标签库struts2标签库struts2标签库
struts标签库struts标签库struts标签库struts标签库struts标签库struts标签库struts标签库struts标签库
struts2标签使用例子 struts2标签使用例子 struts2标签使用例子struts2标签使用例子
JSTL使用教程 JSTL教学 Struts1.X
一个struts2的用户管理实例,涉及增删改查。 主要用到的技术: 1.struts2 2.国际化 3.类型转换 4.struts标签
Struts与表现层技术的结合 JSTL EL表达式语言简介
NULL 博文链接:https://johnson2132.iteye.com/blog/720769
Struts2标签库Struts2标签库Struts2标签库Struts2标签库Struts2标签库Struts2标签库
struts2 中 OGNL表达式的使用struts2 中 OGNL表达式的使用
主要介绍了struts2的标签的详细使用例子说明
Struts2正则表达式校验总结正则表达式验证如下,注意验证框架的XML文件中如下写
struts标签 • Bean Tags:该标签库包含的标签可以用来创建bean、访问bean和访问bean的属性。同时提供了依据cookies、headers和parameters的值创建相关bean的能力。 HTML Tags:该标签库包含的标签可以用来创建...