1. 程式人生 > >struts2 國際化

struts2 國際化

struts2.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.custom.i18n.resources" value="lhy"></constant>

    <package name="default" namespace="/" extends="struts-default">
    <action name="TestAction_*" class="com.lhy.action.InternationalAction" method="{1}">
        <result name="i18n">login.jsp</result>
    </action>
    </package>
</struts>

jsp頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form>
        <s:text name="uname"></s:text>:<input type="text" name="uname"><br> 
        <s:text name="upwd"></s:text>:<input type="password" name="pwd"><br>
        <input type="submit" value='<s:text name="login"></s:text>'>
        <a href="TestAction_change?request_locale=ch_CN">中文</a>|<a href="TestAction_change?request_locale=en_US">English</a>
    </form>
</body>
</html>

Action部分:

package com.lhy.action;

import com.opensymphony.xwork2.ActionSupport;

public class InternationalAction extends ActionSupport {
	//國際化改變語言的方法
		public String change() {
			return "i18n";
		}

}

lhy_en_US.properties介面:

uname=username
upwd=password
login=login

lhy_zh_CN.properties介面:

uname=\u7528\u6237\u540D
upwd=\u5BC6\u7801
login=\u767B\u5F55