【java学习之旅】——Struts2的I18n实现登陆页面中英文切换

来源:互联网 发布:数据安全管理规定 编辑:程序博客网 时间:2024/06/05 15:03

前言:

小编最近在学习Struts2的时候,刚好学习到了关于I18n的相关的知识,其实刚刚接触到I18n不知道为什么这么叫,后来在学习的过程中才发现是internationalization的首尾字母加上中加18个字母而成,其实就是国际化的简称。

内容:

1.为什么使用国际化?
    事实上,Java不可能支持所有的国家和语言,可以通过Locale类的getAvailableLocale方法获取支持的,该方法返回一个Locale数组,该数组中包含了所有支持的国家和语言。让产品(出版物,软件,硬件等)无需做大的改变就能够适应不同的语言和地区的需要
2.如何实现国际化?
    Java.util.ResourceBundle:用于加载资源包
    Java.util.Locale:对应一个特定的国家/地区、语言环境

    Java.text.MessageFormat:用于将消息格式化 

3.Struts2中英文切换demo下载:链接:http://pan.baidu.com/s/1ge5Corp 密码:irkd

登陆界面程序的结构图:

1.设置login.jsp的界面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title><s:text name="reg.title"></s:text></title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>  <a href="${basePath}lang.action?local=zh_CN">中文</a>  <a href="${basePath}lang.action?local=en_US">English</a><br>    <s:form>     <s:textfield name="username" key="username"></s:textfield>        <s:textfield name="password" key="password"></s:textfield>    <s:submit key="submit"></s:submit>  </s:form>     </body></html>

2.设置cyl_en_US.properties和cyl_zh_CN.properties两个文件,其中cyl是basename是可以随意命名的,其他的必须按照这种方式,否则Struts无法识别。其中属性文件中key-value形式,和上面的login.jsp页面中的key要保持一致。如下图的cyl_en_US.properties文件:



3.DoFilter:过滤器主要的功能是获取local值

package com.cyl.filter;import java.io.IOException;import java.util.Locale;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;public class DoFilter implements Filter {public void destroy() {// TODO Auto-generated method stub}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {    String local=request.getParameter("local"); //获取截取的local值  //  Locale locale=new Locale()    if(local!=null){    String loc[]=local.split("_");    Locale locale=new Locale(loc[0],loc[1]);    //把Locale存入session中,系统后面就会使用这个Locale    ((HttpServletRequest) request).getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);    }    chain.doFilter(request, response);}public void init(FilterConfig filterConfig) throws ServletException {// TODO Auto-generated method stub}}
4.在web.xml中配置dofilter过滤器:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 自定义的过滤器 --><filter><filter-name>testChinaUs</filter-name><filter-class>com.cyl.filter.DoFilter</filter-class></filter><filter-mapping><filter-name>testChinaUs</filter-name><url-pattern>/*</url-pattern><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher></filter-mapping><!-- 默认的过滤器 --><filter>  <filter-name>struts2</filter-name>  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</url-pattern></filter-mapping></web-app>

5.I18nTest.java的编写:

package com.cyl.i18n;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;/** * @author cyl * @version:1.0 *  */@SuppressWarnings("serial")public class I18nTest extends ActionSupport {@Overridepublic String execute() throws Exception {return super.execute();}/******************* * 只是一个测试,正式的中英文切换应该在网站的首页上,并具要写一个中英文切换的Action * @return * @throws Exception */public String doReg() throws Exception {//下面的4行代码不用写也可,我只是做一个测试,看一下session中的Locle值Object locale = ActionContext.getContext().getSession().get("WW_TRANS_I18N_LOCALE");if(locale != null){System.out.println("locale:"+locale);}return "showReg";}}
6.struts.xml的配置:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 指定为开发模式 --><constant name="struts.devMode" value="true"></constant><!-- 指定资源文件基名 --><constant name="struts.custom.i18n.resources" value="cyl"></constant><package name="testChinaUs" namespace="/" extends="struts-default">   <action name="lang" class="com.cyl.i18n.I18nTest" method="doReg">       <result>/login.jsp</result>       <result name="showReg">/login.jsp</result>   </action> </package></struts>
7.界面的效果

       


总结:

   Struts2的确可以做很多事情,还需要深入体会!感谢大家的浏览,希望对你有帮助!


原创粉丝点击