1.在struts.xml中配置好默认的语言:
<constant value="zh_CN" />
2.在jsp页面中,设置一些链接(<a></a>),这些链接是Action类,然后通过这些链接,在Action中设置好相应的参数, 并将它保存在application对象中。
3.在跳转后的jsp页面中,通过获取application对象的相应的属性的值,以后在相应的表单中通过设置隐藏属性<input type="hidden" value="<%= (String)application.getAttribute("language") />,这样子在以后的跳转页面都会显示相应的语言了。
4.具体的代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Login</title>
</head>
<body>
<%
String lang = (String)application.getAttribute("lang");
if (lang == null)
{
lang = "zh_CN";
}
session.setAttribute("language", lang);
%>
<table>
<caption>Login</caption>
<s:form method="post" action="Login" namespace="/" validate="false">
<tr>
<td>
<s:text></s:text>
</td>
<td>
<s:textfield size="20"></s:textfield>
</td>
</tr>
<tr>
<td>
<s:text></s:text>
</td>
<td>
<s:textfield size="20"></s:textfield>
</td>
</tr>
<input type="hidden" value="<%=(String)session.getAttribute("language")%>"/>
<tr>
<td colspan="2">
<s:submit type="input" key="label_submit"></s:submit>
<s:reset type="password" key="label_reset"></s:reset>
</td>
</tr>
<tr>
<td colspan="2">
<s:fielderror>
</s:fielderror>
</td>
</tr>
</s:form>
</table>
<!-- 这里是进行设置语言选择的 -->
<s:url action="ChangeLang.action">
<s:param>zh_CN</s:param>
</s:url>
<s:a href="https://www.linuxidc.com/%{zhongwen}">中文</s:a>
<s:url action="ChangeLang.action">
<s:param>en_US</s:param>
</s:url>
<s:a href="https://www.linuxidc.com/%{english}">英文</s:a>
</body>
</html>
//User Model
package yang.www;
public class UserBean
{
private String userName;
private String passWord;
public UserBean(){}
public String getUserName() {
return userName;
}
public String getPassWord() {
return passWord;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
//进行语言的设置: