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

<?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">

<hibernate-mapping>
	<class name="com.softeem.pojo.User" table="user">
		<id name="user_id" type="java.lang.Integer" column="user_id">
		<!--  主键生成策略 -->
		<generator class="increment"></generator>
		</id>
		<property name="username" type="string" column="username" length="50"></property>
		<property name="password" type="string" column="password" length="50"></property>		
	</class>
	
</hibernate-mapping>




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<User> 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

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


<!-- dbcp连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql://localhost:3306/mytest"></property>
	<property name="username" value="root"></property>
	<property name="password" value="root"></property>
	<!-- 最大连接数 -->
	<property name="maxActive" value="100"></property>
	<!-- 最大可空闲连接数 -->
	<property name="maxIdle" value="30"></property>
	<!-- 最大等待连接 -->
	<property name="maxWait" value="500"></property>
	<!-- 事务提交,true代表自动提交事物 -->
	<property name="defaultAutoCommit" value="true"></property>
	
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			<prop key="hibernate.show_sql">true</prop>
		</props>
	</property>
	<property name="mappingResources">
		<list>
			<value>com/softeem/pojo/User.hbm.xml</value>
		</list>
	</property>
</bean>



<bean id="UserDAO" class="com.softeem.dao.Impl.UserDAOImpl" scope="singleton">
	<property name="sessionFactory">
		 <ref bean="sessionFactory"/>
	</property>
</bean>

<bean id="UserService" class="com.softeem.service.Impl.UserServiceImpl">
	<property name="userdao" ref="UserDAO"></property>
</bean>

<bean id="RegistAction" class="com.softeem.action.RegistAction" scope="prototype">
	<property name="userservice" ref="UserService"></property>
</bean>

<bean id="LoginAction" class="com.softeem.action.LoginAction" scope="prototype">
	<property name="userservice" ref="UserService"></property>
</bean>



</beans>

 

11.web.xml配置文件

打开WebRoot--->WEB-INF--->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>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	
</web-app>



12.Struts配置文件

在src目录下新建XML文件

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<package name="user" extends="struts-default">
		<!-- class="RegistAction"与applicationContext.xml中的id对应 -->
		<action name="regist" class="RegistAction">
			<result>/success.jsp</result>
		</action>
		
		<action name="login" class="LoginAction">
			<result name="success">/success.jsp</result>
			<result name="input">/input.jsp</result>
		</action>

	</package>
</struts>


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+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>regist</title>
    <script>
    function jumpToLogin(){
    document.location.href="login.jsp";
    }
    </script>
  </head>
  
  <body>
 <s:form action="regist" method="post">
  <table>
    <tr><td>用户<input type="text" name="user.username"/></td></tr>
    <tr><td>密码<input type="password" name="user.password"/></td></tr>
    <tr><td><input type="submit" value="注册"/></td><td><input type="button" value="返回登录" οnclick="jumpToLogin()"/></td></tr>
    </table>
  </s:form>
  </body>
</html>


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+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    
    <title>login</title>
    <script>
    function jumpToRegist(){
    document.location.href="regist.jsp";
    }
    </script>
  </head>
  
  <body>
 <s:form action="login" method="post">
  <table>
    <tr><td>用户<input type="text" name="user.username"/></td></tr>
    <tr><td>密码<input type="password" name="user.password"/></td></tr>
    <tr><td><input type="submit" value="登录"/></td><td><input type="button" value="注册" οnclick="jumpToRegist()"/></td></tr>
    </table>
  </s:form>
  </body>
</html>


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+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>


  </head>
  
  <body>
   SUCCESS~ <br>
  </body>
</html>


input.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'input.jsp' starting page</title>

  </head>
  
  <body>
    登录失败! <br>
  </body>
</html>


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

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

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

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

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

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值