首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts国际化,从资源文件读取错误信息

2012-10-08 
struts国际化,从资源文件读取异常信息示例演示登录时从资源文件读取用户名或密码错误信息logon.jsp%@ pag

struts国际化,从资源文件读取异常信息

示例演示登录时从资源文件读取用户名或密码错误信息

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><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>

?

action

?

package com.lwf.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessage;import org.apache.struts.action.ActionMessages;import com.lwf.struts.form.LogonForm;public class LogonAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {LogonForm logonForm = (LogonForm)form;String name = logonForm.getUsername();String pwd = logonForm.getPassword();ActionMessages errors = new ActionMessages();ActionForward forward = new ActionForward();if(pwd !=null && !pwd.equals("admin")){errors.add("logon", new ActionMessage("error.login.user"));}if(errors.size()<1){request.getSession().setAttribute("user", logonForm);forward = mapping.findForward("success");}else{saveErrors(request, errors);forward = mapping.findForward("error");}return forward;}}

?

?

配置文件:

<action path="/logon" type="com.lwf.struts.action.LogonAction"name="logonForm" input="/index.jsp" scope="session" validate="true" /><form-bean name="logonForm" type="com.lwf.struts.form.LogonForm"></form-bean>

?

?

资源文件:

ApplicationResources.properties

error.login.user= user or password is not correct

?

ApplicationResources_zh_CN.txt

?

error.login.user= 用户或密码不正确

?转化后的内容:

ApplicationResources_zh_CN.properties

error.login.user= \u7528\u6237\u6216\u5bc6\u7801\u4e0d\u6b63\u786e

?

ApplicationResources_en_US.properties

error.login.user= user or password is not correct

?

下面页面可供客户更改语言

changLang.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>

?

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");}this.setLocale(request, locale);//request.getSession().setAttribute(Globals.LOCALE_KEY, locale);return mapping.findForward("success");}}

?

热点排行