您的位置:首页 > 编程语言 > Java开发

SSH(struts+spring+hibernate)迅速开发--第六章 用户登陆和注册(1)

2008-01-24 08:47 796 查看
第六章 用户登陆和注册

1. 动手前的准备
2. 编写jsp
3. 编写多国语言资源文件,改造jsp
4. 编写ActionForm和Action
5. 配置struts-config.xml
6. 编写登陆和注册的服务类(基于spring的事务管理)
7. 调试运行

1. 动手前的准备
为了不让用户直接访问web中jsp,我们将所有的jsp文件都放在WEB-INF目录下.这样为了方便管理,我们对每个模块的jsp建立一个目录.针对我们的样例项目,我们在WEB-INF下建立jsp/user, jsp/shop 和 jsp/order三个目录,分别存放用户登陆和注册, 商品查看和定购以及查看订单模块的jsp代码
2. 编写jsp
i) 编写用户登陆界面jsp—login.jsp,代码如下,注意jsp中,我们统一用的UTF-8编码格式(粗体红色部分)
<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="46%"> </td>
<td width="54%"> </td>
</tr>
<tr>
<td colspan="2"><div align="center">欢迎使用我们的演示系统,请登陆,或注册后再登陆</div></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><div align="right">用户名:</div></td>
<td><input name="userName" type="text" id="userName" size="15"></td>
</tr>
<tr>
<td><div align="right">密 码:</div></td>
<td><input name="password" type="password" id="password" size="15"></td>
</tr>
<tr>
<td colspan="2"><div align="center">
</div></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="登 陆">
<input type="reset" name="Submit" value="取 消">
<input type="button" name="Submit" value="注 册">
</div></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>

</body>
</html>

上面jsp源代码中,有很多汉字,接下来我们就用struts支持多国语言的特性,将上面出现的汉字改成读资源文件
a) 编辑资源文件,在UserResource_temp.properites文件中,添加如下内容. 注意前面的标签和=号,都必须是英文输入法输入的,否则会影响后面的读取的.
user.title=欢迎使用演示系统
user.welcome=欢迎使用演示系统,请登陆,或注册后再登陆
user.username=用户名
user.password=密 码
user.login=登 陆
user.cancel=取 消
user.register=注 册

user.repassword=确认密码
user.name=姓名
user.email=邮箱
user.phone=电话
user.welcome.register=欢迎注册

user.error.null={0}不能为空
user.error.maxlength={0}的长度必须小于{1}
user.error.minlength={0}的长度必须大于{1}
user.error.invalid={0}输入值不合法

user.verify.success=用户名密码校验成功
user.no.username=用户名不存在
user.error.password=用户名密码错误
user.username.password.notnull=用户名或密码为空
user.register.success=注册成功
user.exist.username=用户名已存在
user.username.isnull=用户名为空
user.password.isnull=密码为空
user.name.isnull=姓名为空
user.error.others=系统通讯错误,请检查网络是否联通,或同管理员联系

b) 运行encoding.bat批处理文件,刷新sshDemo工程
用读取资源文件里面的配置,替代jsp中的汉字.替换后的代码如下.
struts框架中,是用bean:message读取资源文件里面的信息的.比如下面代码中,关于页面title的内容,是<bean:message bundle="user" key="user.title"/>.
bundle属性:指的是struts-config.xml中,配置资源标签
<message-resources parameter="cn.com.book.demo.struts.UserResources" key="user"/>
key属性 指的是UserResource_temp.properties中配置的等于前面的标签,等于后面的是要显示的文字.比如: user.title=欢迎使用演示系统

更改后的login.jsp代码:
<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><bean:message bundle="user" key="user.title"/></title>
</head>
<body>
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="46%"> </td>
<td width="54%"> </td>
</tr>
<tr>
<td colspan="2"><div align="center"><bean:message bundle="user" key="user.welcome"/></div></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><div align="right"><bean:message bundle="user" key="user.username"/>:</div></td>
<td><input name="userName" type="text" id="userName" size="15"></td>
</tr>
<tr>
<td><div align="right"><bean:message bundle="user" key="user.password"/>:</div></td>
<td><input name="password" type="password" id="password" size="15"></td>
</tr>
<tr>
<td colspan="2"><div align="center">
</div></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="<bean:message bundle="user" key="user.login"/>">
<input type="reset" name="Submit" value="<bean:message bundle="user" key="user.cancel"/>">
<input type="button" name="Submit" value="<bean:message bundle="user" key="user.register"/>">
</div></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐