1. 程式人生 > >Struts2國際化-中英文轉換

Struts2國際化-中英文轉換

先在src下建立兩個File,提供程式所需的資原始檔

(粗心的程式設計師可要注意了File檔名一定不要寫錯,該檔案以key,value的形式出現)具體程式碼如下:

接下來寫檢視層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>中英文切換</title>
  </head>
  
  <body>
  
   <s:i18n name="message">  
  	<s:text name="check"></s:text>:
  	<a href="login.action?request_locale=zh_CN"><s:text name="chinese"></s:text></a>  
	<a href="login.action?request_locale=en_US"><s:text name="english"></s:text></a>
   <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>
</html>

其中a標籤的login為struts.xml 的action name屬性。

其次是controller層封裝類indexAction但“execute”和“success”為可變因素但切記一定要在struts.xml中配置所對應的屬性名,否則就找不到所對應的action。

package com.hnpi.action;

import com.opensymphony.xwork2.ActionSupport;

public class indexAction extends ActionSupport{
	private String name;
	private String pwd;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public String execute(){
		return "success";
	}
}

在建立一個action,裡面只放一個execute方法

package com.hnpi.action;

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

}

然後配置最為重要的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" namespace="/" extends="struts-default">

		<action name="t1" class="com.hnpi.action.indexAction">
			<result name="success">/welcome.jsp</result>
		
		</action>
		<action name="login" class="com.hnpi.action.LanguageAction">
		<result name="success">/index.jsp</result>
		
		</action>

	</package>

</struts>

這裡一定要小心class的路徑和action name的屬性名稱(result name屬性的名稱為封裝類中execute類的返回內容)

最後配置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>

最後是我的程式執行效果圖