1. 程式人生 > >struts2的國際化(中英文切換)

struts2的國際化(中英文切換)

國際化Internationalization的首末字元I和N之間字元數是18,簡稱I18N。

“國際化”是指一個應用程式在執行時,能夠根據客戶端請求所來自國家或地區語言的不同,而顯示不同的語言介面。

要求:Struts2框架搭建成功。

1. 專案截圖

2.建立資原始檔:

2.1   messages_en_US.properties(英文)

2.2   messages_zh_CN.properties(中文)

3.  新建檢視層 jsp頁面

3.1  index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</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>
     <!-- 區域性定義使用哪一種國際化語音 -->
     <s:i18n name="message">  
     
     <s:text name="check"></s:text>:
  	<a href="changeLanguage.action?request_locale=zh_CN"><s:text name="chinese"></s:text></a>  
	<a href="changeLanguage.action?request_locale=en_US"><s:text name="english"></s:text></a>  
	<br/>
     
     
     <form action="<%=basePath%>t1" method="post">
               <table>
                 <tr>
                   <td><s:text name="login.username"/></td>
                   <td><input type="text" name="user.userName"/></td>
                </tr>
                <tr>
                  <td><s:text name="login.password"/></td>
                  <td><input type="text" name="user.password"/></td>
                </tr>
                <tr>
                 <td colspan="2"><input type="submit" value="<s:text name="login.btn"/>"/></td>
               </tr>              
              </table>                             
     </form>
    </s:i18n> 
  </body>
<ml>

3.2 show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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%>">
   
  </head>
  
  <body>
    使用者名稱:${requestScope.name}<br/>
    密碼:${requestScope.pwd}
  </body>
</html>

4.  編寫action

4.1  區域設定Action           ChangeLanguageAction.java 

package com.hnpi.action;

public class ChangeLanguageAction {
	public String execute(){
		return "success";
	}
}

4.2 提交表單Action          RegisterAction.java

package com.hnpi.action;

public class RegisterAction {
	private String name;
	private String sex;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String execute(){
		return "success";
	}


}

5.  配置攔截器 (進行語言選擇的處理)

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <package name="default" extends="struts-default">
     
      <action name="t1" class="com.hnpi.action.RegisterAction" method="execute">
            <result name="success">/show.jsp</result>
      </action>
      
        <action name="changeLanguage" class="com.hnpi.action.ChangeLanguageAction">
            <result>/index.jsp</result>
      </action>
   </package>
</struts>

6.  web.xml

<?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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
   <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>

效果圖