Struts提供了國際化的功能
下面來用一個登錄實例來演示一下Struts的國際化配置和顯示
創建一個login_i
[java]
package com
import org
/**
* 登錄ActionForm
* 表單的數據必須和ActionForm的get
* @author Longxuan
*
*/
@SuppressWarnings(
public class LoginActionForm extends ActionForm {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this
}
}
登錄時會驗證密碼是否正確
[java]
package com
/**
* 密碼錯誤異常
* @author Longxuan
*
*/
@SuppressWarnings(
public class PasswordErrorException extends RuntimeException {
public PasswordErrorException() {
// TODO Auto
}
public PasswordErrorException(String message) {
super(message)
// TODO Auto
}
public PasswordErrorException(Throwable cause) {
super(cause)
// TODO Auto
}
public PasswordErrorException(String message
super(message
// TODO Auto
}
public PasswordErrorException(String message
boolean enableSuppression
super(message
// TODO Auto
}
}
package com
/**
* 用戶未找到異常
* @author Longxuan
*
*/
@SuppressWarnings(
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException() {
// TODO Auto
}
public UserNotFoundException(String message) {
super(message)
// TODO Auto
}
public UserNotFoundException(Throwable cause) {
super(cause)
// TODO Auto
}
public UserNotFoundException(String message
super(message
// TODO Auto
}
public UserNotFoundException(String message
boolean enableSuppression
super(message
// TODO Auto
}
}
提供用戶管理類
[java]
package com
/**
* 用戶管理類
* @author Longxuan
*
*/
public class UserManager {
/**
* 簡單處理登錄邏輯
* @param username 用戶名
* @param password 密碼
*/
public void login(String username
if(!
throw new UserNotFoundException()
}
if(!
throw new PasswordErrorException()
}
}
}
現在寫LoginAction的處理
[java]
package com
import javax
import javax
import org
import org
import org
import org
import org
import org
/**
* 登錄Action 負責取得表單數據
*
* @author Longxuan
*
*/
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
throws Exception {
//獲取數據
LoginActionForm laf = (LoginActionForm) form;
String username = laf
String password = laf
UserManager userManager = new UserManager()
ActionMessages messages = new ActionMessages()
try {
//用戶登錄
userManager
//獲取登錄成功的國際化消息
ActionMessage success= new ActionMessage(
messages
//傳遞消息
this
return mapping
} catch (UserNotFoundException e) {
e
//獲取登錄成功的國際化消息
ActionMessage error = new ActionMessage(
messages
//傳遞消息
this
} catch (PasswordErrorException e) {
e
//獲取登錄成功的國際化消息
ActionMessage error = new ActionMessage(
messages
//傳遞消息
this
}
return mapping
}
}
來一個手動切換語言的類
[java]
package com
import java
import javax
import javax
import org
import org
import org
import org
/**
* 完成語言的手動切換
* @author Longxuan
*
*/
public class ChangeLanguageAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
throws Exception {
//獲取語言
String lang = request
String[] split = lang
//設置語言
Locale locale = new Locale(split[
this
return mapping
}
}
新建國際化信息文件
英文語言包和默認語言包設置成一樣的
[java]
#
errors
errors
errors
errors
login
login
login
login
login
login
中文語言包
[java]
#
errors
errors
errors
errors
login
login
login
login
login
login
把login
[html]
<%@ page language=
pageEncoding=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<!
<fmt:setLocale value=
<fmt:setBundle basename=
<html>
<head>
<meta http
<title>Struts登錄</title>
</head>
<body>
<a >中文登錄</a>|
<%
<html:link action=
<html:link action=
<hr>
<html:errors />
<hr>
<h
struts標簽讀取國際化文件
</h
<form action=
<bean:message key=
:
<input type=
<br />
<bean:message key=
:
<input type=
<br />
<input type=
value=
</form>
<hr>
<h
jstl讀取國際化文件
</h
<form action=
<fmt:message key=
:
<input type=
<br />
<fmt:message key=
:
<input type=
<br />
<input type=
value=
</form>
</body>
</html>
login_success
[html]
<%@ page language=
pageEncoding=
<%@ taglib uri=
<%@ taglib uri=
<!DOCTYPE html PUBLIC
<html>
<head>
<meta http
<title>Insert title here</title>
</head>
<body>
<!
<html:messages id=
<bean:write name=
</html:messages>
</body>
</html>
最後的最後
[html]
<?xml version=
<web
xmlns=
xmlns:xsi=
xsi:schemaLocation=
/xml/ns/j
<welcome
<welcome
</welcome
<servlet>
<servlet
<servlet
<init
<param
<param
</init
<init
<param
<param
</init
<init
<param
<param
</init
<load
</servlet>
<!
<servlet
<servlet
<url
</servlet
</web
在Struts
[html]
<?xml version=
<!DOCTYPE struts
<struts
<form
<form
</form
<action
<action path=
type=
name=
scope=
<forward name=
<!
<forward name=
</action>
<action path=
type=
>
<forward name=
</action>
</action
<message
</struts
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27934.html