SSH框架整合之注册登录

上一篇介绍了三大框架的搭建,今天基于上一次的框架使用MVC模式完成注册和登录功能。

MVC模式:模型(Model),视图(View)和控制Controller)。 MVC模式的目的就是实现Web系统的职能分工。 Model层实现系统中的业务逻辑,通常可以用JavaBean或EJB来实现。 View层用于与用户的交互,通常用JSP来实现。 Controller层是Model与View之间沟通的桥梁,它可以分派用户的请求并选择恰当的视图以用于显示,同时它也可以解释用户的输入并将它们映射为模型层可执行的操作。

开发工具:1.MyEclipse6.5                2.mysql-5.0

 

1.创建数据库 

这里使用的是mysql5.0

数据库名:mytest

表名:user

create database mytest;

use mytest;

create table user(

user_id int primary key auto_increment not null,

username varchar(50) not null,

password varchar(50) not null);

 最后注意要导入数据库驱动jar包:mysql-connector-java-5.0.3-bin.jar

将mysql-connector-java-5.0.3-bin.jar放到WEB-INF的lib文件里

2.导入Struts2.0所需要的jar包,删除不需要的jar包(否则程序运行的时候会报错)

导入6个jar包,删除一个jar包

将以下Jar包直接复制到WEB-INF下的lib文件夹

 

删除asm-2.2.3.jar

 

3. User.class和User.hbm.xml

新建Package:com.softeem.pojo

新建Class:User.class

新建XML:User.hbm.xml

User.class

package com.softeem.pojo;

public class User {  
    private int user_id;  
    private String username;  
    private String password;  
    public int getUser_id() {  
        return user_id;  
    }  
    public void setUser_id(int user_id) {  
        this.user_id = user_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;  
    }  
  
}  

 

User.hbm.xml





	
		
		
		
		
		
				
	
	




4.UserDAO接口

新建Package:com.softeem.dao

新建Interface:UserDAO.class

package com.softeem.dao;

import java.util.List;

import com.softeem.pojo.User;

public interface UserDAO {
	//声明增加和查找方法
	public void saveUser(User user);
	
	public User findUser(User user);
}



5.UserDAO接口的实现类UserDAOImpl.class

新建Package:com.softeem.dao.Impl

新建Class:UserDAOImpl.class

package com.softeem.dao.Impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.softeem.dao.UserDAO;
import com.softeem.pojo.User;


public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{
	//增加用户
	public void saveUser(User user){
		this.getHibernateTemplate().save(user);
	}
	
	//查询验证用户是否存在
	public User findUser(User user){
		User firstuser = new User();
		//HQL查询语句
		String hql = "from User user where user.username='"
				+ user.getUsername() + "' and user.password= '"
				+ user.getPassword() + "'";
		//将查询出的结果放到List
		List userlist = this.getHibernateTemplate().find(hql);
		//判断是否有查询结果,换句话说就是判断用户是否存在
		if(userlist.size()>0){
		//取出查询结果的第一个值,理论上数据库是没有重复的用户信息
		firstuser = userlist.get(0);
		}
		return firstuser;
	}
}


6.UserService接口

新建Package:com.softeem.service

新建Interface:UserService.class

package com.softeem.service;

import com.softeem.pojo.User;

public interface UserService {
	//声明增加和查找方法
	public void saveUser(User user);
	
	public boolean findUser(User user);
}


7.UserService接口的实现类UserServiceImpl.class

新建Package:com.softeem.service.Impl

新建Class:UserServiceImpl.class

package com.softeem.service.Impl;

import com.softeem.dao.UserDAO;
import com.softeem.pojo.User;
import com.softeem.service.UserService;

public class UserServiceImpl implements UserService{
	//注入DAO,生成GET SET 方法
	private UserDAO userdao;

	public UserDAO getUserdao() {
		return userdao;
	}

	public void setUserdao(UserDAO userdao) {
		this.userdao = userdao;
	}

	//保存用户信息
	public void saveUser(User user){
		this.userdao.saveUser(user);
	}
	//查找验证用户信息
	public boolean findUser(User user){
		//
		User firstuser = this.userdao.findUser(user);
		//在UserDAO查询中已经判断了只有当用户名和密码都存在时才返回firstuser
		//所以在这里只用判断firstuser里面用户名或者密码中的一个是否存在就可以了
		if(firstuser.getUsername()!=null){
			return true;
		}else{
			return false;
		}
	}
}

8.RegistAction

新建Package:com.softeem.action

新建Class:RegistAction.class

package com.softeem.action;
import com.opensymphony.xwork2.ActionSupport;
import com.softeem.pojo.User;
import com.softeem.service.UserService;

public class RegistAction extends ActionSupport{
	private User user;
	//注入Service,生成SET GET方法
	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;
	}
	
	//execute方法
	@Override
	public String execute() throws Exception {
		this.userservice.saveUser(this.user);
		return SUCCESS;
	}

}


 

9.LoginAction

新建Class:LoginAction.class

package com.softeem.action;

import com.opensymphony.xwork2.ActionSupport;
import com.softeem.pojo.User;
import com.softeem.service.UserService;

public class LoginAction extends ActionSupport{
	private User user;
	//注入Service,生成Set和Get方法
	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 {
		boolean flag = userservice.findUser(user);
		if(flag){
			return SUCCESS;
		}else{
			return INPUT;
		}
		
	}
}


10.Spring配置文件

打开WebRoot--->WEB-INF--->applicationContext.xml







	
	
	
	
	
	
	
	
	
	
	
	
	



	
	
		
			org.hibernate.dialect.MySQLDialect
			true
		
	
	
		
			com/softeem/pojo/User.hbm.xml
		
	





	
		 
	



	



	



	





 

11.web.xml配置文件

打开WebRoot--->WEB-INF--->web.xml




	
		struts2
		
			org.apache.struts2.dispatcher.FilterDispatcher
		
	

	
		struts2
		/*
	

	
		index.jsp
	

	
		
			org.springframework.web.context.ContextLoaderListener
		
	
	



12.Struts配置文件

在src目录下新建XML文件

struts.xml





	
		
		
			/success.jsp
		
		
		
			/success.jsp
			/input.jsp
		

	


13.最后是JSP页面

regist.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    regist
    
  
  
  
 
  
用户
密码


login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
        
    login
    
  
  
  
 
  
用户
密码


success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'success.jsp' starting page


  
  
  
   SUCCESS~ 


input.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'input.jsp' starting page

  
  
  
    登录失败! 


 14.通过以上操作,SSH整合之登录注册已经完成

需要注意的是SSH框架的正确搭建和jar包的正确导入,对于新手这可能并不简单。

框架搭建可以看上一篇文章   SSH框架整合之搭建

找不到jar包的直接下载项目吧    SSH整合之登陆注册

稍后会放出SSH框架整合之增删查改操作。

你可能感兴趣的:(SSH整合)