SSH 登录与注册

代码:

User.java

package model;

public class User {
	private long id;
	private String name;
	private String password;
	
	public User() {
		// TODO Auto-generated constructor stub
	}
	public User(long id,String password) {
		this.id = id;
		this.password = password;
	}
		
	public User(long id,String name,String password) {
		this(id,password);
		this.name = name;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	

}

User.hbm.xml

<?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>
	<class name="model.User" table="user">
		<!-- 映射标识属性 -->
		<id name="id" column="id" type="long">
			<generator class="assigned"/>
		</id>
		<!-- 映射普通属性 -->
		<property name="name"  column="name"  type="string"/>
		<property name="password" column="password"   type="string"/>
	</class>
</hibernate-mapping>

UserService.java

package service;

import dao.UserDao;
import model.User;

public class UserService {
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public boolean login(User user) {
		if (userDao.verifyUserId(user.getId())&&userDao.verifyPassword(user)) {
			return true;			
		}else {
			return false;
		}		
	}
	
	public boolean register(User user) {
		if (userDao.addUser(user)) {
			return true;
		}else {
			return false;
		}				
	}

}

UserDao.java

package dao;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import model.User;

public class UserDao extends HibernateDaoSupport{
	// 添加用户
    public boolean addUser(User user) {
        if(!verifyUserId(user.getId())) {
            getHibernateTemplate().save(user);
            return true;
        }
        return false;
    }

    // 验证用户名是否存在
    public boolean verifyUserId(long id) {
        @SuppressWarnings("unchecked")
		List<User> users = (List<User>) getHibernateTemplate().find("from User where id=?", id);
        return users.isEmpty() ? false:true;
    }

    // 验证密码是否正确
    @SuppressWarnings("rawtypes")
	public boolean verifyPassword(User user) {
    	long id = user.getId();
    	String password = user.getPassword();
        List list1 = getHibernateTemplate().find("select password from User where id=?", id);
        List list2 = getHibernateTemplate().find("select name from User where id=?", id);
        user.setName(list2.get(0).toString());
        return ( list1.get(0).toString() ).equals(password);
    }

}

LoginAction.java

package action;

import com.opensymphony.xwork2.Action;

import model.User;
import service.UserService;

public class LoginAction implements Action{
	private User user;
	private UserService userService;
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@Override
	public String execute() throws Exception {
		if (userService.login(user)) {
			return "success";
		}else {
			return "error";	
		}		
	}	
}

RegisterAction.java

package action;

import com.opensymphony.xwork2.Action;

import model.User;
import service.UserService;

public class RegisterAction implements Action{
	private User user;
	private UserService userService;	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	@Override
	public String execute() throws Exception {
		if (userService.register(user)) {
			return "success";			
		}else {
			return "error";	
		}		
	}

}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Struts 2配置文件的根元素 -->
<struts>
	<!-- 配置了系列常量 -->
	<constant name="struts.i18n.encoding" value="UTF-8"/>	
	<constant name="struts.devMode" value="true"/>	
    <constant name="struts.objectFactory" value="spring" />
	
    <package name="action" namespace="/" extends="struts-default"> 
		 
		<action name="registerAction" class="registerAction">
			<result name="error">/register.jsp</result>
			<result name="success">/index.jsp</result>
		</action>
		<action name="loginAction" class="loginAction">
			<result name="success">/welcome.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<!-- 定义数据源Bean,使用C3P0数据源实现 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<!-- 指定连接数据库的驱动 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver"/>
		<!-- 指定连接数据库的URL -->
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/userinfo"/>
		<!-- 指定连接数据库的用户名 -->
		<property name="user" value="root"/>
		<!-- 指定连接数据库的密码 -->
		<property name="password" value="123456"/>
		<!-- 指定连接数据库连接池的最大连接数 -->
		<property name="maxPoolSize" value="40"/>
		<!-- 指定连接数据库连接池的最小连接数 -->
		<property name="minPoolSize" value="1"/>
		<!-- 指定连接数据库连接池的初始化连接数 -->
		<property name="initialPoolSize" value="1"/>
		<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
		<property name="maxIdleTime" value="20"/>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 依赖注入数据源,注入正是上面定义的dataSource -->
		<property name="dataSource" ref="dataSource"/>
		<!-- mappingResouces属性用来列出全部映射文件 -->
		<property name="mappingResources">
			<list>
				<!-- 以下用来列出Hibernate映射文件 -->
				<value>model/User.hbm.xml</value>
			</list>
		</property>
		<!-- 定义Hibernate的SessionFactory的属性 -->
		<property name="hibernateProperties">
			<!-- 配置Hibernate属性 -->
			<value>
			hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
			hibernate.hbm2ddl.auto=update
			hibernate.show_sql=true
			hibernate.format_sql=true;
			</value>
		</property>
	</bean>
	<!-- 定义DAO Bean-->
	<bean id="userDao" class="dao.UserDao">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 创建action -->
	<bean id="registerAction" class="action.RegisterAction" scope="prototype"> 
		<property name="userService" ref="userService"></property>
	</bean>
		
	<bean id="loginAction" class="action.LoginAction" scope="prototype">
	<property name="userService" ref="userService"></property>
	</bean>
	
	<!-- 配置一个业务逻辑组件 -->
	<bean id="userService" class="service.UserService">
		<property name="userDao" ref="userDao"/>
	</bean>
	
	
	
	<bean id="transactionManager" 
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<!-- 配置HibernateTransactionManager时需要依注入SessionFactory的引用 -->
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>


</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    <filter>
       <filter-name>struts2</filter-name>
       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    
    <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户登录</title>
<link rel="stylesheet" href="style/loginStyle.css" type="text/css">
</head>
<body>
<div class="wrapper">
	<div class="container">
		<h1>Welcome</h1>
		<form class="form" action="loginAction" method="post">
			<input type="text" placeholder="Username" id="name" name="user.id">
			<input type="password" placeholder="Password" id="password" name="user.password">
			<button type="submit" id="button">Login</button>
			<a onclick="window.location.href='register.jsp'">注册</a>
		</form>
	</div>
	<ul class="bg-bubbles">
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
	</ul>
</div>
</body>
</html>

loginStyle.css

@charset "UTF-8";
*{
	box-sizing: border-box;
	margin: 0;
	padding: 0;	
	font-weight: 300;
}
body{
	font-family: 'Source Sans Pro', sans-serif;
	color: white;
	font-weight: 300;	
}
::-webkit-input-placeholder {
	font-family: 'Source Sans Pro', sans-serif;
		color:    white;
	font-weight: 300;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
	font-family: 'Source Sans Pro', sans-serif;
	 color:    white;
	 opacity:  1;
	font-weight: 300;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
	font-family: 'Source Sans Pro', sans-serif;
	 color:    white;
	 opacity:  1;
	font-weight: 300;
}
:-ms-input-placeholder { /* Internet Explorer 10+ */
	font-family: 'Source Sans Pro', sans-serif;
	 color:    white;
	font-weight: 300;
}
.wrapper{
	background: #50a3a2;
	background: -webkit-linear-gradient(top left, #50a3a2 0%, #53e3a6 100%);
	background: -moz-linear-gradient(top left, #50a3a2 0%, #53e3a6 100%);
	background: -o-linear-gradient(top left, #50a3a2 0%, #53e3a6 100%);
	background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%);
	position: absolute;
	margin: 0;
	width: 100%;
	height: 650px;
	overflow: hidden;
}
.container{
	max-width: 600px;
	margin: 40px auto;
	padding: 80px 0;
	height: 400px;
	text-align: center;
}
h1{
		font-size: 40px;
		font-weight: 200;
}
form{
	padding: 20px 0;
	position: relative;
	z-index: 2;	
}
input{
		display: block;
		appearance: none;
		outline: 0;
		border: 2px solid white;
		background-color: rgba(255,255,255,0.4);
		width: 300px;	
		height: 50px;
		border-radius: 3px;
		padding: 15px 15px 15px 54px;
		margin: 0 auto 10px auto;
		text-align: left;
		font-size: 18px;
		color: black;	
		font-weight: 300;
		transition:width 1s;
}
#name{
	background-image: url(../picture/User.png);
	background-size: 30px 30px;
	background-position: 11px 8px;
	background-repeat: no-repeat;
}
#name:focus{
	background-image:url(../picture/User.png);
	background-size: 30px 30px;
	background-position: 8px 5px;
    background-position: 11px 8px;
	background-repeat: no-repeat;
}
#password{
	background-image: url(../picture/Lock1.png);
	background-size: 30px 30px;
	background-position: 11px 8px;
	background-repeat: no-repeat;
}
#password:focus{
	background-image:url(../picture/Lock1.png);
	background-size: 30px 30px;
    background-position: 11px 8px;
	background-repeat: no-repeat;
}
input:hover{
	background-color:white;
	width: 320px;
}
input:focus{
	background-color: white;
	width: 320px;	
}
button{
		appearance: none;
		outline: 0;
		background-color: #53e3a6;
		border: 0;
		padding: 10px 15px;
		color: white;
		border-radius: 3px;
		width: 300px;
		cursor: pointer;
		font-size: 20px;
		transition-duration: 0.25s;	
		font-weight: 500;
		margin-left:10px;
}
button:hover{
		background-color: #0f9; 
		color: white;
}
a{
	text-decroation:none;
	cursor:pointer;
	display:inline-block;
	margin-left:10px;
}
.bg-bubbles{
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	z-index: 1;
}
.bg-bubbles	li{
		position: absolute;
		list-style: none;
		display: block;
		width: 40px;
		height: 40px;
		background-color: rgba(255,255,255,0.5);
		bottom: -160px;
		-webkit-animation: square 15s infinite;
		animation:square 15s infinite;
		-webkit-transition-timing-function: linear;
		transition-timing-function: linear;
}
.bg-bubbles li:nth-child(1){
			left: 10%;
		}
.bg-bubbles li:nth-child(2){
	left: 20%;
	width: 80px;
	height: 80px;
	animation-delay: 2s;
	animation-duration: 17s;
}
.bg-bubbles li:nth-child(3){
	left: 25%;
	animation-delay: 4s;
}
.bg-bubbles li:nth-child(4){
	left: 40%;
	width: 60px;
	height: 60px;
	animation-duration: 22s;
	background-color: fade(white, 25%);
}
.bg-bubbles li:nth-child(5){
	left: 70%;
}
.bg-bubbles li:nth-child(6){
	left: 80%;
	width: 120px;
	height: 120px;
	animation-delay: 3s;
	background-color: fade(white, 20%);
}
.bg-bubbles li:nth-child(7){
	left: 32%;
	width: 160px;
	height: 160px;
	animation-delay: 7s;
}
.bg-bubbles li:nth-child(8){
	left: 55%;
	width: 20px;
	height: 20px;
	animation-delay: 15s;
	animation-duration: 40s;
}
.bg-bubbles li:nth-child(9){
	left: 25%;
	width: 10px;
	height: 10px;
	animation-delay: 2s;
	animation-duration: 40s;
	background-color: fade(white, 30%);
}
.bg-bubbles li:nth-child(10){
	left: 90%;
	width: 160px;
	height: 160px;
	animation-delay: 11s;
}
@-webkit-keyframes square {
  0%   { transform: translateY(0); }
  100% { transform: translateY(-900px) rotate(600deg); }
}
@keyframes square {
  0%   { transform: translateY(0); }
  100% { transform: translateY(-900px) rotate(600deg); }
}

register.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户注册</title>
<style type="text/css">
div{
margin:auto;
width:600px;
font-family:"Microsoft YaHei";
font-size:16px;
background:#f8f8f8;
}
h4{
width:500px;
height:30px;
padding:10px 50px;
text-decroation:none;
background:#9999cc;
color:#fff;
font-size:18px;
font-weight:blod;
border:2px solid #fff;
border-radius:4px 4px 0 0;
}
form{
margin-left:50px;
}
input[type=text],input[type=password]{
width:180px;
height:15px;
}

label{
display:inline-block;
width:120px;
text-align:right;
padding-right:8px;
}
span{
color:red;
}
.btn1,.btn2{
width:60px;
height:30px;
font-size:16px;
margin:-2px 0 16px 0;
color:#fff;
border:2px solid #fff;
border-radius:4px;
}
.btn1{
margin-left:130px;
background:#3399cc;
}
.btn1:hover{
background:#3366ff;
}
.btn2{
margin-left:2px;
background:#ff6666;
}
.btn2:hover{
background:#ff3333;
}
</style>
</head>
<body>
<div>
<h4>用户注册</h4>
<form action="registerAction" method="post" onSubmit="return check()">
<p>
<label>ID<span>*</span>:</label>
<input type="text" name="user.id" id="userId">
</p>
<p>
<label>姓名:</label>
<input type="text" name="user.name" id="userName">
</p>
<p>
<label>密码:</label>
<input type="text" name="user.password" id="userPassword">
</p>
<p>
<label>确认密码:</label>
<input type="text" name="repassword" id="rePassword">
</p>
<p>
<input type="submit" value="注册" class="btn1">
<input type="reset" value="重置" class="btn2">
</p>
</form>
</div>
</body>
<script type="text/javascript">
	var password = document.getElementById("userPassword").value;
	var repassword = document.getElementById("rePassword").value;
	var userid = document.getElementById("userId").value;
	var username = document.getElementById("userName").value;
	
	if("".equals(password)||"".equals(userid)||"".equals(username)){		
		alert("信息不能为空。");
		return false;
	}else{
		if(password.equals(repassword)){
			return true;	
		}else{
			return false;
		}
			
	}
</script>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welcome</title>
<style type="text/css">
*{
font-family:"Consolas";

}
</style>
</head>
<body>
hi,<s:property value="user.name"/>.
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>error</title>
</head>
<body>
登录失败,请检查!
</body>
<script type="text/javascript">
setTimeout("javascript:location.href='index.jsp'", 5000); 
</script>
</html>

jar 包地址:

链接:https://pan.baidu.com/s/1mq6E_aw5bNqT9XFeHz9hNg 
提取码:m4vm 

致qq:

[1] tomacat 不及时更新我的修改:project->clean 把tomacat 的其它serlet 删除

[2] getHibernateTemplate().find("from User where id=?", id) 中 User 是类名,不是数据库的表名

[3] bean ref action 相应部分应对应一致

[4] 提示 id 没有默认值时,可以将主键的class 改为 assigned

[5] 表单数据提交给类时,名字对应一致,input name="user.id"

[6] 页面可以使用<s:property value="user.id"/>获取值

图片:

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值