Struts 1 学习笔记-5-2(编写一个简单的支持I18N的登录系统)
2008-02-16 17:21
861 查看
1.首先编写了一个ChangeLanguageAction.java:
package com.codedestiny.struts;
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 ChangeLanguageAction extends Action ...{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
String lan = request.getParameter("lan");
Locale currentLocale = null;
if ("zh".equals(lan))...{
currentLocale = new Locale("zh", "CN");
} else if ("en".equals(lan)) ...{
currentLocale = new Locale("en", "US");
}
if (currentLocale != null) ...{
request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
}
return mapping.findForward("success");
}
}
2.这样在显示层增加如下链接,通过点击即可实现切换Locale:
<a href="changelan.do?lan=zh">切换成中文</a><br>
<a href="changelan.do?lan=en">切换成英文</a><br>
3.MessageResources.properties(前面的代码和Struts默认的相同):
# -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Blank Application
welcome.heading=Welcome!
welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the application.properties file with this message in the /WEB-INF/src/java/resources folder.)
drp.user.name=User Name
drp.user.password=User Password
drp.user.login=Please Login
drp.user.login.success=Login Success!
drp.user.not.found=Not Found User
dpr.user.password.error=Password Error
4.MessageResources_zh_CN.properties用来支持中文(只贴出和上面不同的部分,中文已转为ISO-8859-1):
drp.user.name=User Name
drp.user.password=User Password
drp.user.login=Please Login
drp.user.login.success=Login Success!
drp.user.not.found=Not Found User
dpr.user.password.error=Password Error
5.LoginAction.java:
package com.codedestiny.struts;
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;
public class LoginAction extends Action ...{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
LoginActionForm lag = (LoginActionForm)form;
String username = lag.getUsername();
String password = lag.getPassword();
ActionMessages messages = new ActionMessages();
try ...{
//调用助手类进行合法性检查
UserHelper.validate(username, password);
//创建消息
ActionMessage msg = new ActionMessage("drp.user.login.success");
messages.add("loginsuccess", msg);
//传递消息
this.saveMessages(request, messages);
return mapping.findForward("success");
}catch(UserNotFoundException unfe) ...{
unfe.printStackTrace();
ActionMessage msgError = new ActionMessage("drp.user.not.found");
messages.add("usernotfound", msgError);
//传递错误消息
this.saveErrors(request, messages);
//this.saveMessages(request, messages);
}catch(PasswordErrorException pee) ...{
pee.printStackTrace();
ActionMessage msgError = new ActionMessage("dpr.user.password.error");
messages.add("passworderror", msgError);
//传递错误消息
this.saveErrors(request, messages);
//this.saveMessages(request, messages);
}
return mapping.findForward("error");
}
}
6.login.jsp:
<%...@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%...@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<%...@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<!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=GB18030">
<title></title>
</head>
<body>
<hr>
<html:errors/>
<form action="login.do">
<bean:message key="drp.user.name"/>:<input type="text" name="username"><br>
<bean:message key="drp.user.password"/>:<input type="password" name="password"><br>
<hr>
<input type="submit" name="btn_login" value="<bean:message key="drp.user.login"/>">
</form>
</body>
</html>
7.login_success.jsp:
<%...@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%...@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<%...@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<!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=GB18030">
<title></title>
</head>
<body>
<html:messages id="msg" message="true" property="loginsuccess">
<bean:write name="msg"/>
</html:messages>
</body>
</html>
8.测试截图:
中文:
英文:
登录成功:
package com.codedestiny.struts;
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 ChangeLanguageAction extends Action ...{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
String lan = request.getParameter("lan");
Locale currentLocale = null;
if ("zh".equals(lan))...{
currentLocale = new Locale("zh", "CN");
} else if ("en".equals(lan)) ...{
currentLocale = new Locale("en", "US");
}
if (currentLocale != null) ...{
request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
}
return mapping.findForward("success");
}
}
2.这样在显示层增加如下链接,通过点击即可实现切换Locale:
<a href="changelan.do?lan=zh">切换成中文</a><br>
<a href="changelan.do?lan=en">切换成英文</a><br>
3.MessageResources.properties(前面的代码和Struts默认的相同):
# -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Blank Application
welcome.heading=Welcome!
welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the application.properties file with this message in the /WEB-INF/src/java/resources folder.)
drp.user.name=User Name
drp.user.password=User Password
drp.user.login=Please Login
drp.user.login.success=Login Success!
drp.user.not.found=Not Found User
dpr.user.password.error=Password Error
4.MessageResources_zh_CN.properties用来支持中文(只贴出和上面不同的部分,中文已转为ISO-8859-1):
drp.user.name=User Name
drp.user.password=User Password
drp.user.login=Please Login
drp.user.login.success=Login Success!
drp.user.not.found=Not Found User
dpr.user.password.error=Password Error
5.LoginAction.java:
package com.codedestiny.struts;
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;
public class LoginAction extends Action ...{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
LoginActionForm lag = (LoginActionForm)form;
String username = lag.getUsername();
String password = lag.getPassword();
ActionMessages messages = new ActionMessages();
try ...{
//调用助手类进行合法性检查
UserHelper.validate(username, password);
//创建消息
ActionMessage msg = new ActionMessage("drp.user.login.success");
messages.add("loginsuccess", msg);
//传递消息
this.saveMessages(request, messages);
return mapping.findForward("success");
}catch(UserNotFoundException unfe) ...{
unfe.printStackTrace();
ActionMessage msgError = new ActionMessage("drp.user.not.found");
messages.add("usernotfound", msgError);
//传递错误消息
this.saveErrors(request, messages);
//this.saveMessages(request, messages);
}catch(PasswordErrorException pee) ...{
pee.printStackTrace();
ActionMessage msgError = new ActionMessage("dpr.user.password.error");
messages.add("passworderror", msgError);
//传递错误消息
this.saveErrors(request, messages);
//this.saveMessages(request, messages);
}
return mapping.findForward("error");
}
}
6.login.jsp:
<%...@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%...@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<%...@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<!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=GB18030">
<title></title>
</head>
<body>
<hr>
<html:errors/>
<form action="login.do">
<bean:message key="drp.user.name"/>:<input type="text" name="username"><br>
<bean:message key="drp.user.password"/>:<input type="password" name="password"><br>
<hr>
<input type="submit" name="btn_login" value="<bean:message key="drp.user.login"/>">
</form>
</body>
</html>
7.login_success.jsp:
<%...@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%...@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<%...@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<!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=GB18030">
<title></title>
</head>
<body>
<html:messages id="msg" message="true" property="loginsuccess">
<bean:write name="msg"/>
</html:messages>
</body>
</html>
8.测试截图:
中文:
英文:
登录成功:
相关文章推荐
- 用c++编写一个简单的登录系统
- Struts 1 学习笔记-1(简单登录模块的实现,Struts初步了解)
- Struts 1 学习笔记-5-1(Struts中I18N的简单测试)
- 学习笔记 提取常量和一个简单的聊天系统(python)from python基础教程
- Servlet学习笔记_03_使用servlet编写一个简单的helloworld程序
- Silverlight学习笔记一(理解一下机制,使用一下布局,实现一个简单的用户登录)
- ROS学习笔记十:用C++编写一个简单的服务和客户端
- 【Rebol 学习笔记】第一篇:编写一个简单的 CGI 程序
- java学习笔记之使用MyEclipse编写一个简单的登陆界(一)
- 系统学习JavaWeb之三编写一个简单的Servlet应用程序
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- 【Linux学习】 写一个简单的Makefile编译源码获取当前系统时间
- shell语法的简单学习以及编写一个简单的进度条
- 深入浅出学习Struts1框架(一):一个简单mvc模式代码示例开始
- 学习LSM(Linux security module)之二:编写并运行一个简单的demo
- Core Java 8 学习笔记(四) 打印一个简单的日历
- C#网站登录学习笔记(一):登录简单网站
- 简单权限系统基于shiro-springmvc-spring-mybatis(学习笔记 1)
- python Django 学习笔记(二)—— 一个简单的网页
- DayDayUp之HTML5学习笔记 二 使用header、aside、section、footer实现一个简单的界面