编写register.jsp
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.util.*"%>
<%@ 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" %>
<html:html lang="true">
<head>
<title>MLDN —— 我的智囊团</title>
<META NAME="Generator" CONTENT="Struts + Spring + Hibernate + MySQL + Tomcat + CP">
<META NAME="Author" CONTENT="李兴华">
<META NAME="Keywords" CONTENT="智囊团,SSH,tomcat,mysql">
<META NAME="Description" CONTENT="MLDN旗下网站 —— www.zhinangtuan.net.cn">
</head>
<body>
<center>
<jsp:include flush="true" page="inc/template.htm"/>
<h1>用户注册</h1>
<html:form action="" method="post">
用户 I D: <html:text property="userid"></html:text><br>
用户密码: <html:password property="userpwd"></html:password><br>
确认密码: <html:password property="confirmpwd"></html:password><br>
丢失密码提示问题: <html:text property="userques"></html:text><br>
丢失密码问题答案: <html:text property="userans"></html:text><br>
<html:submit value="注册"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
</center>
</body>
</html:html>
添加Action
struts-config.xml发生变动
<action-mappings >
<action
attribute="userForm"
input="/jsp/errors.jsp"
name="userForm"
parameter="status"
path="/user"
scope="request"
type="org.lxh.zngt.struts.action.UserAction" />
</action-mappings>
新建errors.jsp
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.util.*"%>
<%@ 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" %>
<html:html lang="true">
<head>
<title>MLDN —— 我的智囊团</title>
<META NAME="Generator" CONTENT="Struts + Spring + Hibernate + MySQL + Tomcat + CP">
<META NAME="Author" CONTENT="李兴华">
<META NAME="Keywords" CONTENT="智囊团,SSH,tomcat,mysql">
<META NAME="Description" CONTENT="MLDN旗下网站 —— www.zhinangtuan.net.cn">
</head>
<body>
<center>
<jsp:include flush="true" page="inc/template.htm"/>
<h1>程序出现了以下错误</h1>
<html:errors/>
<p><a href="#" onclick="history.back()">返回上一页</a>
</center>
</body>
</html:html>
<action
attribute="userForm"
input="/jsp/errors.jsp"
name="userForm"
parameter="status"
path="/user"
scope="request"
type="org.lxh.zngt.struts.action.UserAction">
<forward name="registersuccess" path="/jsp/index.jsp"></forward>
<forward name="registerfailure" path="/jsp/register.jsp"></forward>
</action>
</action-mappings>
<html:form action="" method="post">
用户 I D: <html:text property="userid"></html:text><br>
用户密码: <html:password property="userpwd"></html:password><br>
确认密码: <html:password property="confirmpwd"></html:password><br>
丢失密码提示问题: <html:text property="userques"></html:text><br>
丢失密码问题答案: <html:text property="userans"></html:text><br>
<input type="hidden" name="status" value="register">
<input type="hidden" name="type" value="1">
<html:submit value="注册"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
修改UserForm.java
先写ApplicationResources.properties
# Resources for parameter 'org.lxh.myzngt.struts.ApplicationResources'
# Project MyZhiNangTuanDemo
user.userid.null = <li> \u7528\u6237ID\u4e0d\u80fd\u4e3a\u7a7a\uff01
user.userpwd.null = <li> \u7528\u6237\u767b\u5f55\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01
user.userques.null = <li> \u627e\u56de\u5bc6\u7801\u63d0\u793a\u95ee\u9898\u4e0d\u80fd\u4e3a\u7a7a\uff01
user.userans.null = <li> \u627e\u56de\u5bc6\u7801\u95ee\u9898\u7b54\u6848\u4e0d\u80fd\u4e3a\u7a7a\uff01
user.confirmpwd.error = <li> \u4e24\u6b21\u8f93\u5165\u7684\u5bc6\u7801\u4e0d\u4e00\u81f4\uff01
user.userid.exist = <li> \u7528\u6237\uff29\uff24\u5df2\u7ecf\u5b58\u5728\uff01
user.integral.null = <li> \u5fc5\u987b\u586b\u5199\u7528\u6237\u7684\u79ef\u5206\uff01
user.integral.error = <li> \u79ef\u5206\u5fc5\u987b\u586b\u5199\u6570\u5b57\uff01
checkcode.null = <li> \u9a8c\u8bc1\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01
checkcode.error = <li> \u9a8c\u8bc1\u7801\u8f93\u5165\u9519\u8bef\uff01
question.title.null = <li> \u95ee\u9898\u6807\u9898\u4e0d\u80fd\u4e3a\u7a7a\uff01
question.content.null = <li> \u95ee\u9898\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a\uff01
question.offerscore.error = <li> \u95ee\u9898\u60ac\u8d4f\u5206\u6570\u5fc5\u987b\u662f\u6570\u5b57\uff01
question.qid.null = <li> \u95ee\u9898\u7f16\u53f7\u4e0d\u80fd\u4e3a\u7a7a\uff01
answer.quesans.null = <li> \u56de\u7b54\u7684\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a\uff01
answer.qid.null = <li> \u8bf7\u9009\u62e9\u8981\u56de\u7b54\u7684\u95ee\u9898\uff01
admin.adminid.null = <li> \u7ba1\u7406\u5458\uff29\uff24\u4e0d\u80fd\u4e3a\u7a7a\uff01
admin.adminpwd.null = <li> \u7ba1\u7406\u5458\u5bc6\u7801\u4e0d\u80fd\u7a7a\uff01
admin.confirmpwd.error = <li> \u4e24\u6b21\u8f93\u5165\u7684\u5bc6\u7801\u4e0d\u4e00\u81f4\uff01
admin.adminid.exist = <li> \u6b64\u7ba1\u7406\u5458\uff29\uff24\u5df2\u7ecf\u5b58\u5728\uff01
admin.oldpwd.null = <li> \u65e7\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01
item.itemname.null = <li> \u680f\u76ee\u540d\u79f0\u4e0d\u80fd\u4e3a\u7a7a\uff01
item.itemcode.null = <li> \u680f\u76ee\u7f16\u53f7\u4e0d\u80fd\u4e3a\u7a7a\uff01
item.itemcode.error = <li> \u680f\u76ee\u7f16\u53f7\u5fc5\u987b\u662f\u6570\u5b57\uff01
subitem.subname.null = <li> \u5b50\u680f\u76ee\u540d\u79f0\u4e0d\u80fd\u4e3a\u7a7a\uff01
subitem.subcode.null = <li> \u5b50\u680f\u76ee\u7f16\u53f7\u4e0d\u80fd\u4e3a\u7a7a\uff01
subitem.subcode.error = <li> \u5b50\u680f\u76ee\u7f16\u53f7\u5fc5\u987b\u662f\u6570\u5b57\uff01
验证码: <html:text property="checkcode"></html:text>
<img src="image.jsp"><br>
如果验证码不正确,也是一种错误,验证码不正确的判断在action中。
public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
// 先判断验证码是否正确
String ccode = (String)request.getSession().getAttribute("ccode");
String checkcode = userForm.getCheckcode();
if(!checkcode.equals(ccode)) {
ActionMessages errors = new ActionMessages();
errors.add("checkcode", new ActionMessage("checkcode.error"));
super.saveErrors(request, errors);
return mapping.getInputForward();
}
// 如果正确,则需要向数据库中插入
return null;
}
写IUserDAO.java, IUserDAOImpl.java
User.java
package org.lxh.myzngt.vo;
public class User {
/** sex property */
private String sex;
/** usermail property */
private String usermail;
/** userques property */
private String userques;
/** userid property */
private String userid;
/** realname property */
private String realname;
/** userans property */
private String userans;
/** userpwd property */
private String userpwd;
/** grade property */
private int grade;
/** integral property */
private int integral;
/** id property */
private int id;
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getUserans() {
return userans;
}
public void setUserans(String userans) {
this.userans = userans;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsermail() {
return usermail;
}
public void setUsermail(String usermail) {
this.usermail = usermail;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getUserques() {
return userques;
}
public void setUserques(String userques) {
this.userques = userques;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getIntegral() {
return integral;
}
public void setIntegral(int integral) {
this.integral = integral;
}
}
package org.lxh.myzngt.dao;
import org.lxh.myzngt.vo.User;
public interface IUserDAO {
// 用户注册
public void register(User user) throws Exception;
}
package org.lxh.myzngt.dao.impl;
import org.lxh.myzngt.dao.IUserDAO;
import org.lxh.myzngt.vo.User;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class IUserDAOImpl extends HibernateDaoSupport implements IUserDAO {
public void register(User user) throws Exception {
super.getSession().save(user);
}
}
生成User.hbm.xml
注意把catalog="mldn"去掉
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="org.lxh.myzngt.vo.User" table="user">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="userid" type="java.lang.String">
<column name="userid" length="50" not-null="true" />
</property>
<property name="userpwd" type="java.lang.String">
<column name="userpwd" length="50" />
</property>
<property name="userques" type="java.lang.String">
<column name="userques" length="50" />
</property>
<property name="userans" type="java.lang.String">
<column name="userans" length="50" />
</property>
<property name="usermail" type="java.lang.String">
<column name="usermail" length="50" />
</property>
<property name="integral" type="java.lang.Integer">
<column name="integral" />
</property>
<property name="grade" type="java.lang.Integer">
<column name="grade" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="2" />
</property>
<property name="realname" type="java.lang.String">
<column name="realname" length="50" />
</property>
</class>
</hibernate-mapping>
Abstract如果设置为true,表示该bean的定义是abstract的,spring将不会实例化该bean
<bean id="iuserdao" class="org.lxh.myzngt.dao.IUserDAO"
abstract="true">
</bean>
<bean id="iuserdaoimpl" class="org.lxh.myzngt.dao.impl.IUserDAOImpl"
parent="iuserdao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
把index.jsp移到jsp文件夹中,在外面建一个index.htm
<html>
<head>
<title>MLDN —— 我的智囊团</title>
<META NAME="Generator" CONTENT="Struts + Spring + Hibernate + MySQL + Tomcat + CP">
<META NAME="Author" CONTENT="李兴华">
<META NAME="Keywords" CONTENT="李兴华,MLDN,智囊团,SSH,tomcat,mysql">
<META NAME="Description" CONTENT="MLDN旗下网站 —— www.zhinangtuan.net.cn">
<META HTTP-EQUIV="refresh" CONTENT="0;URL=jsp/index.jsp">
</head>
<body>
</body>
</html>
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.util.*"%>
<%@ 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" %>
<html:html lang="true">
<head>
<title>MLDN —— 我的智囊团</title>
<META NAME="Generator" CONTENT="Struts + Spring + Hibernate + MySQL + Tomcat + CP">
<META NAME="Author" CONTENT="李兴华">
<META NAME="Keywords" CONTENT="智囊团,SSH,tomcat,mysql">
<META NAME="Description" CONTENT="MLDN旗下网站 —— www.zhinangtuan.net.cn">
</head>
<body>
<center>
<jsp:include flush="true" page="inc/template.jsp"/>
<logic:present name="userid" scope="session">
欢迎${userid}光临
等级:${grade}
<a href="personinfo.jsp">个人中心</a>
</logic:present>
</center>
</body>
</html:html>
下面进行验证,启动服务器,本节到此为止