SSH2简单登录+注册例子

1、在完成了SSH2简单登录例子的前提下添加注册功能

2、在WebRoot下新建、编写register.jsp、修改login.jsp添加注册链接

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>


   
    register.jsp
  
  
   
    
    	
    		
    		
    		
    		
   				
   		

Welcome Register

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>


     
    ssh2 login 
  
  
  
   	

USER LOGIN

have not a account?click here

3、新添加service服务层类,提供业务方法调用Dao的操作,彻底解耦action层与dao层。在src>com.service包下编写InterUserService接口类、UserServiceImpl实现类,其中实现类包含InterUserDao的对象属性方法。

package com.service;

import com.pojo.Loginusers;

public interface InterUserService {
	
	public Loginusers validate(String name,String password);  //提供用户验证业务方法
	
	public Loginusers register(Loginusers user);			//提供用户注册业务方法
}
package com.service;

import com.dao.InterUserDao;
import com.pojo.Loginusers;

public class UserServiceImpl implements InterUserService {
	
	private InterUserDao  userDaoImpl;
	public InterUserDao getUserDaoImpl() {
		return userDaoImpl;
	}

	public void setUserDaoImpl(InterUserDao userDaoImpl) {
		this.userDaoImpl = userDaoImpl;
	}

	public Loginusers validate(String name, String password) {
		// 直接调用Dao层数据库验证方法
		return userDaoImpl.validate(name, password);
	}

	public Loginusers register(Loginusers user) {
		// 先进行表单验证,id和name是否已存在
		if(!userDaoImpl.checkAccountAndId(user.getUserName(),user.getId())){
			if(userDaoImpl.saveUser(user))
				return userDaoImpl.validate(user.getUserName(), user.getPassword());
			else 
				return null;
		}else
			return null;
	}

}

4、修改添加dao层数据库操作方法类

package com.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class BaseDao {
	
	 //Dao持久层访问SessionFactor获取session对象的中间组件
	//解耦Dao层与SessionFactory
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	} 
	public Session getSession(){
		return sessionFactory.openSession();
	}
}
package com.dao;

import com.pojo.Loginusers;

public interface InterUserDao {
	public Loginusers validate(String name,String password);	//提供验证方法
	public boolean checkAccountAndId(String name,int id);	//根据用户名和id检查是否有该用户
	public boolean saveUser(Loginusers user);	   //保存用户
}
package com.dao;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dao.BaseDao;
import com.pojo.Loginusers;

public class UserDaoImpl extends BaseDao implements InterUserDao{
	
	@Override
	public Loginusers validate(String name,String password) {
		// TODO Auto-generated method stub
		String hql="from Loginusers u where u.userName=? and u.password=?";	
		Session session=getSession();	//从继承的类中获得session对象
		Query query=session.createQuery(hql);
		query.setParameter(0,name);
		query.setParameter(1, password);
		List list=query.list();
		Iterator it=list.iterator();
		while(it.hasNext()){
			if(list.size()!=0){
				Loginusers user=(Loginusers)it.next();
				return user;
			}
		}
		session.close();
		return null;
	}

	@Override
	public boolean checkAccountAndId(String name,int id) {
		// TODO Auto-generated method stub
		String hql="from Loginusers u where u.userName=? or u.id=?";
		Session session=getSession();	//从继承的类中获得session对象
		Query query=session.createQuery(hql);
		query.setParameter(0, name);
		query.setParameter(1, id);
		List list=query.list();
		Iterator it=list.iterator();
		while(it.hasNext()){
			if(list.size()!=0){
				return true;
			}
		}
		session.close();
		return false;
	}	

	@Override
	public boolean saveUser(Loginusers user) {
		// TODO Auto-generated method stub
		Session session=getSession();	//从继承的类中获得session对象
		try{
			Transaction ts=session.beginTransaction();
			session.save(user);
			ts.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		session.close();
		return true;
	}
}

5、修改action类ValidateAction,在其中加入register()注册控制流程方法、添加InterUserService接口对象的属性和get/set方法

取消直接从spring获取实例bean调用Dao方法,改由service接口调用。spring负责bean的依赖注入。

package com.action;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.dao.InterUserDao;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pojo.Loginusers;
import com.service.InterUserService;

public class ValidateAction extends ActionSupport{
	private Loginusers user;
	ActionContext acontext;
	private InterUserService userServiceImpl;

	public InterUserService getUserServiceImpl() {
		return userServiceImpl;
	}

	public void setUserServiceImpl(InterUserService userServiceImpl) {
		this.userServiceImpl = userServiceImpl;
	}

	public Loginusers getUser() {
		return user;
	}

	public void setUser(Loginusers user) {
		this.user = user;
	}
	
	//用户注册,通过调用业务注册service方法完成
	public String registerUser(){
		Loginusers u=new Loginusers(user.getId(),user.getUserName(),user.getPassword());
		Loginusers user1=null;
		user1=userServiceImpl.register(u);
		if(user1!=null){
			acontext=ActionContext.getContext();
			Map session=acontext.getSession();
			session.put("user", user1);
			return SUCCESS;
		}else
			return ERROR;
	}
	
	public String execute()throws Exception{
		String account=user.getUserName();
		String password=user.getPassword();
		boolean flag=false;
		Loginusers user1=null;
		
		acontext=ActionContext.getContext();
		Map session=acontext.getSession();
		user1=(Loginusers)session.get("user");
		
		if(user1==null){
			System.out.println("null user1");
			user1=userServiceImpl.validate(account, password);
			if(user1!=null){
				System.out.println("not nll");
				session.put("user",user1);
				flag=true;
			}
		}else{
			flag=true;
		}
		if(flag)
			return SUCCESS;
		else
			return ERROR;
	}	
	
}

6、修改applicationContext.xml文件,添加UserService的bean配置,包含UserDaoImpl的属性设置




	
		
		
		
		
		
		
	
	
		
			
		
		
			
				
					org.hibernate.dialect.MySQLDialect
				
			
		
		
			
				com/pojo/Loginusers.hbm.xml
		
	
	
		
	
	
	
	
		
	
	
	
	
	
	
		
	
	
	
	
		
	
	
	
	
	
	
	

7、在struts.xml中为在action中新添加的register方法配置action信息



	
		
		
			
			
				/success.jsp
				/error.jsp
			
			
			
			
				/success.jsp
				/error.jsp
			
			
		
		
	
 

8、保存修改启动Tomcat,浏览器测试

SSH2简单登录+注册例子_第1张图片

SSH2简单登录+注册例子_第2张图片

SSH2简单登录+注册例子_第3张图片

SSH2简单登录+注册例子_第4张图片

在这次实例中仅进行了id值及用户名的去重没有做进一步的表单验证,而这项功能实现并不困难,使用struts的验校器便可做到。或者甚至可在此基础上轻易地增加更多想要的功能,足可见框架带来的便捷性。

 

你可能感兴趣的:(SSH2简单登录+注册例子)