欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts国际化,资源文件读取三

程序员文章站 2022-07-03 23:18:17
...

本示例演示客户进行语言设置,从而更改整个系统语言.

客户在changLang.jsp进行设置提交之后转发到logon.jsp查看该页面标签语言变化

如下:

changeLang.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@include file="share/html_head_taglib.jspf" %>
<!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 action="changLangAction.do">
	<input type="radio" name="lang" value="zh"/>中文&nbsp;<input type="radio" name="lang" value="en"/>英文
	<br>
	<input type="submit" value="submit"/>
	</form>
</body>
</html>

ChangLangAction.java

package com.lwf.struts.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ChangLangAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String lang = request.getParameter("lang");
		lang = (lang == null) ? ""  : lang.trim();
		
		Locale locale = null;
		if(lang.equals("zh")){
			locale = new Locale("zh","cn");
		}else{
			locale = new Locale("en","us");
		}
		request.getSession().setAttribute(Globals.LOCALE_KEY, locale);
                             //this.setLocale(request, locale);可以这行代码替换上一行代码
		return mapping.findForward("success");
	}

}

 

配置文件:

 

<action path="/changLangAction" type="com.lwf.struts.action.ChangLangAction"  scope="request">
			<forward name="success" path="/logon.jsp"></forward>
		</action>

 logon.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<script type="text/javascript">
	

</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>logon</title>
</head>
<body>
<html:form action="logon">
	<table border="0" width="100%">
		<tr>
			<th align="right"><bean:message key="login.user"/></th>
			<td align="left"><html:text property="username" size="20" maxlength="20"></html:text></td>
		</tr>
		<tr>
			<th align="right"><bean:message key="login.pwd"/></th>
			<td align="left"><html:password property="password" size="20" maxlength="20"></html:password></td>
		</tr>
		<tr>
			<td align="right"><html:submit>logonin</html:submit>	</td>
			<td align="left"><html:button property="register" >register</html:button><html:reset>reset</html:reset></td>
		</tr>
	</table>
</html:form>
</body>
</html:html>