需要的类库及工具
*struts1.3的jar包,直接去apache的官网上去下,最好是下的完整包,包含源代码(http://struts.apache.org/download.cgi#struts1310)
*然后工具我用的是eclipse的J2EE版
*服务器用的是apache-tomcat 6
然后要把struts用到的核心jar文件放到新建项目的web目录下的lib目录里,由于struts你打开里面的包的话是有很多很多的,但是我们现在只需要基础核心的jar包,因为只是做一个简单的例子,所以我们就直接把struts给的简单例子里用到的lib复制到当前项目的lib里。解压你的struts1.3.10压缩包,然后然后打开里面的apps目录,选择struts-blank-1.3.10例子进行解压,把里面的的web-inf下的lib里德jar包全部拷到当前项目下的lib下。拷贝好以后把eclipse下的项目刷新一下就可以了。此时项目在eclipse中的大致结构
先看一下我的页面的国际化实现的代码,首先是源代码 里面的
现在看一下配置文件,先是web.xml,
简要的说一下原理,为什么用
我们再看看上面的LocaleAction是怎么实现的,然后我们可以根据这个简单的实现在这上面进行一些改进以实现自己的目的,它的类声明public final class LocaleAction extends BaseAction {}一个final类继承BaseAction类,所有的action都会继承baseaction。然后private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());这段当然是用来日志记录的,不管它。接下来就是最重要的execute方法,简要的解释一下:public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Extract attributes we will need HttpSession session = request.getSession();//先去得到session Locale locale = getLocale(request);//这个应该是父类的方法,通过request得到locale String language = null;//这里就是配置文件里的三个参数了 String country = null; String page = null; try {//分别去获得三个参数的值,form就是那个DynaActionForm对象 language = (String) PropertyUtils.getSimpleProperty(form, 'language'); country = (String) PropertyUtils.getSimpleProperty(form, 'country'); page = (String) PropertyUtils.getSimpleProperty(form, 'page'); } catch (Exception e) { log.error(e.getMessage(), e); } boolean isLanguage = ((language != null) && (language.length() > 0)); boolean isCountry = ((country != null) && (country.length() > 0)); if ((isLanguage) && (isCountry)) {//如果都不会空的话,就建立新的locale对象 locale = new java.util.Locale(language, country); } else if (isLanguage) { locale = new java.util.Locale(language, '');//没有国家的话直接就是语言locale } session.setAttribute(Globals.LOCALE_KEY, locale);吧新的locale放到session里,这样放回过去,就可以根据新的locale来实现国际化了 if (null == page) { return mapping.findForward('success'); } else { return new ActionForward(page);//返回到page,就是还是之前的注册页面 } }
差不多一个简易的struts国际化就这样搞定,至于自己要实现跟多的功能就靠自己去摸索了,我想struts2的国际化也差不多吧,连接了大致的原理,用起来就快了。希望对大家有帮助。
原创内容,转载请注明出处,作者,否则后果自负