注册/登录系统
定义持久化类
package com.Struts;
public class User {
private Long id;
private String username;
private String password;
private int age;
private String email;
public User() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "用户名:" + getUsername() + " 口令:" + getPassword()
+ " 年龄:" + getAge() + " Email:" + getEmail() ;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.Struts">
<class name="User" table="userinfo" >
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="username" type="string" column="username"/>
<property name="password" type="string" column="password"/>
<property name="age" type="integer" column="age"/>
<property name="email" type="string" column="email"/>
</class>
</hibernate-mapping>
定义Action动作类
RegisterAction.java
package com.Struts;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.opensymphony.xwork2.ActionSupport;
import com.util.HibernateUtil;
public class RegisterAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
public String register() throws Exception{
try{
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
HibernateUtil.getSession().close();
return ERROR;
}
}
public String login() throws Exception {
try{
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from User Where username=:uname");
query.setParameter("uname", user.getUsername());
List list = query.list();
tx.commit();
if(list.size()==1){
return SUCCESS;
}else
return ERROR;
}catch(Exception e){
e.printStackTrace();
HibernateUtil.getSession().close();
return ERROR;
}
}
}
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户注册</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<p>注册一个新用户</p>
<s:form action="Register">
<s:actionerror/><s:fielderror/>
<s:textfield name="user.username" label="用户名"/>
<s:password name="user.password" label="口令"/>
<s:textfield name="user.age" label="年龄"/>
<s:textfield name="user.email" label="Email地址"/>
<s:submit value="注册"/>
</s:form>
<br><br>
<p><a href="<s:url action="index"/>">返回 首页</a></p>
</body>
</html>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登录页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<p>请输入用户和密码:</p>
<s:form action="Login">
<s:textfield name="user.username" label="用户名" tooltip="输入用户名" labelposition="left"/>
<s:password name="user.password" label="密码" tooltip="输入密码" labelposition="left"/>
<s:submit value="登录" align="center"/>
</s:form>
<br><br>
<p><a href="<s:url action="index"/>">返回 首页</a></p>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>注册成功页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<p>注册成功</p>
<s:property value="user"/>
<p><a href="<s:url action="index"/>">返回 首页</a></p>
</body>
</html>
welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登陆成功</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<p align="center"><font color="#000080" size="5">欢迎登录本系统</font></p>
<br><br>
<p><a href="<s:url action="index"/>">返回 首页</a></p>
</body>
</html>
index.jsp
<p><a href="<s:url action='registerInput'/>">用户注册</a></p>
<p><a href="<s:url action='loginInput'/>">用户登录</a></p>
修改struts.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="basicstructs2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<action name="registerInput">
<result>/register.jsp</result>
</action>
<action name="loginInput">
<result>/login.jsp</result>
</action>
<action name="Register" class="com.Struts.RegisterAction" method="register">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result> <!-- 当输入校验失败后,Action动作类自动返回"input"的结果 -->
<result name="error">/error.jsp</result>
</action>
<action name="Login" class="com.Struts.RegisterAction" method="login">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
配置文件名格式:<Action-Class-Name>-validation.xml
RegisterAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="user.username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空!</message>
</field-validator>
</field>
<field name="user.password">
<field-validator type="requiredstring" short-circuit="true">
<param name="trim">true</param>
<message>口令不能为空!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">14</param>
<message>口令包含的字符在6-14位之间</message>
</field-validator>
</field>
<field name="user.age">
<field-validator type="int">
<param name="min">16</param>
<param name="max">60</param>
<message>用户年龄应在16-60岁之间</message>
</field-validator>
</field>
<field name="user.email">
<field-validator type="requiredstring" short-circuit="true">
<message>邮箱地址必填!</message>
</field-validator>
<field-validator type="email">
<message>邮箱地址不合法!</message>
</field-validator>
</field>
</validators>