1. 程式人生 > >Struts2 國際化(實現在頁面中調換中文英文兩個模式)

Struts2 國際化(實現在頁面中調換中文英文兩個模式)

在原本搭建的Struts上進行新增程式碼頁面(詳情見本人第一篇文章)

建立資原始檔 在這裡插入圖片描述

package com.hnpi.blue;

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

}

在這裡插入圖片描述

package com.hnpi.blue; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport{ private String username; private String password;

public String getusername(){
	return username;
}
public void username(String username){
	this.username = username ;
}
public String getpassword(){
	return password;
}
public void setpassword(String password){
	this.password= password;
}
public String t1(){
	System.out.println(this.getText("login.username"));
	return "success";

} }

在這裡插入圖片描述

login.username=username
login.password=password
login.btn=login
check=Please check your language
chinese=chinese
english=ehglish

在這裡插入圖片描述

login.username=\u7528\u6237\u540D
login.password=\u5BC6\u7801
login.btn=\u767B\u5F55
check=\u8BF7\u9009\u62E9\u8BED\u8A00
chinese=\u4E2D\u6587
english=\u82F1\u6587

建立XML檔案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>  

    <constant name ="struts.costom.i18n.resources" value="message"></constant>
    <package name="v1" namespace="/" extends="struts-default">  
        <action name="check" class="com.hnpi.blue.ChangeLanguageAction">  
             <result name="success">/index.jsp</result>   
        </action>
        
        <action name="hellow" class="com.hnpi.blue.RegisterAction" method="t1">
        <result name="success">/show.jsp</result>
        
        </action>
    </package>  
     
 
    
</struts> 

外部檔案中 在這裡插入圖片描述

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <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>

如何人找到上圖第8行的程式碼

在這裡插入圖片描述

步驟如下

在這裡插入圖片描述

兩個頁面 在這裡插入圖片描述 在這個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>
  </head>
  <body>
    <!-- 區域性定義使用哪一種國際化語音 -->
   <s:i18n name="messages">  
   <s:text name ="check" ></s:text>
   <a href="check.action?request_locale=zh_CN"><s:text name="chinese"></s:text></a>
    <a href="check.action?request_locale=en_US"><s:text name="english"></s:text></a><br/>
   <form action="<%=basePath%>hello.action" 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>

在這裡插入圖片描述

<%@ 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%>">
    
    <title>My JSP 'show.jsp' starting page</title>
 

  </head>
  
  <body>
    歡迎 <br>
  </body>
</html>

然後開始了繁瑣的部署,然後執行,開啟瀏覽器輸入地址顯示如下畫面,表示你已經成功了!!!

在這裡插入圖片描述

在這裡插入圖片描述

希望我的程式碼能給你的學習帶來方便感謝!!!