Eclipse用SSH框架实现登陆注册功能

Eclipse用SSH框架实现登陆注册功能

第一次写博文,写的不好的地方还请多多指教。
关于SSH搭建大家可以看这篇博文,讲的很详细,是搭建SSH框架具有注册功能的例子http://blog.csdn.net/lpftobetheone/article/details/17783791

我也是第一次搭SSH框架参考这篇博文成功搭起来后也可以运行,但是其中的具体逻辑我还是不太懂,所以自己决定在此基础上增加登录的功能来进行摸索学习。

我先说一下每个类的功能:
dao是提供与数据库交互读取数据的接口
daoimpl是实现dao接口的
service是提供具体业务服务的接口
serviceimpl同样是实现接口的
service接口里提供了业务逻辑,serviceimpl具体实现这些业务逻辑,通过调用dao接口里的方法来读取数据库中的数据。最后在action中调用service接口里的业务逻辑,然后把从数据库中取出的数据赋给action中的属性。这样,struts2框架就能从页面中接受后台传来的数据,显示出来了。

下面来说具体过程:

大概流程:Login.jsp—->LoginAction.java—>UserManager.java中的logUser方法—>BaseDao.java中的queryObject方法
最后登录的用户名和密码是否正确是根据queryObject方法的返回值在LoginAction.java中判断的

  • 先定义Login.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


"Content-Type" content="text/html; charset=UTF-8">
Insert title here


     "login" >  
         "user.username" label="用户名" class="txt">s:textfield>  
         "user.password" label="密码" class="txt">s:password>    
         "登录" lass="btn">  
     


  • 然后新建LoginAction.java
package com.ynwi.ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.ynwi.ssh.forms.UserForm;
import com.ynwi.ssh.service.UserManager;

public class LoginAction extends ActionSupport{

   private static final long serialVersionUID=1L;

    private UserForm user;

    private UserManager userManager;

    public UserForm getUser() {
        return user;
    }

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

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }

    public String execute()
    {
        try {

            //this.setUserManager(new UserManagerImpl());


            boolean a=userManager.logUser(user);

            if(a==true) return SUCCESS;
            else return ERROR;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return ERROR;
        }
    }



}
  • 在struct2.xml中添加action
"1.0" encoding="UTF-8"?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

 
    "struts-default.xml"/>
    "ynwi" namespace="/" extends="struts-default">

         
         "register" class="regAction"> 
             <result  name="input">/Register.jspresult>
             <result  name="success">/Login.jspresult>
             <result  name="error">/Fail.jspresult>

         action>

         
         <action name="login" class="logAction"> 
             <result  name="input">/Login.jspresult>
             <result  name="success">/Success.jspresult>
             <result  name="error">/Fail.jspresult>

         action>
       package>
struts>
  • 在applicationContext.xml中写用户登录action的bean
"1.0" encoding="UTF-8"?>
"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-3.0.xsd"> 


    
    "dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        destroy-method="close">  
        <property name="driverClass">  
            <value>com.mysql.jdbc.Drivervalue>  
        property>  
        <property name="jdbcUrl">  
            <value>jdbc:mysql://localhost/test  
          
        "user">  
            root  
          
        "password">  
            199394  
          
        "maxPoolSize">  
            80  
          
        "minPoolSize">  
            1  
          
        "initialPoolSize">  
            1  
          
        "maxIdleTime">  
            20  
          
    

      
      
      
    "sessionFactory"  
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        <property name="dataSource">  
            <ref local="dataSource" />  
        property>  
        <property name="mappingResources">  
            <list>  
                <value>com/ynwi/ssh/beans/User.hbm.xmlvalue>  
                <value>com/ynwi/ssh/beans/Admin.hbm.xmlvalue>
            list>  
        property>  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>  
                <prop key="show_sql">trueprop>  
                <prop key="hibernate.jdbc.batch_size">20prop>  
            props>  
        property>  
    bean>  

    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    bean>  

    <bean id="baseDao" class="com.ynwi.ssh.daoImpl.UserDao">  
        <property name="sessionFactory">  
            <ref bean="sessionFactory" />  
        property>  
    bean>  

      
    <bean id="userManager" class="com.ynwi.ssh.serviceImpl.UserManagerImpl">  
        <property name="dao">  
            <ref bean="baseDao" />  
        property>  
    bean>  

      
    <bean id="regAction" class="com.ynwi.ssh.action.RegisterAction">  
        <property name="userManager">  
            <ref bean="userManager" />  
        property>  
    bean>

      
    <bean id="logAction" class="com.ynwi.ssh.action.LoginAction">  
        <property name="userManager">  
            <ref bean="userManager" />  
        property>  
    bean>
beans> 
  • 在BaseDao.java中写登录查询的接口
package com.ynwi.ssh.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.ynwi.ssh.beans.User;
import com.ynwi.ssh.beans.Event;
import com.ynwi.ssh.beans.Admin;

public interface BaseDao {

    //注册
    public void saveObject(Object obj) throws HibernateException;

    //登录
    public boolean queryObject(User obj) throws HibernateException;
}
  • 在UserDao中写接口的具体实现
package com.ynwi.ssh.daoImpl;

import java.util.List;

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

import com.ynwi.ssh.dao.BaseDao;
import com.ynwi.ssh.forms.UserForm;
import com.ynwi.ssh.beans.Admin;
import com.ynwi.ssh.beans.Event;
import com.ynwi.ssh.beans.User;

public class UserDao extends HibernateDaoSupport implements BaseDao{

    //注册
    @Override
    public void saveObject(Object obj) throws HibernateException {
        // TODO Auto-generated method stub
        getHibernateTemplate().save(obj);
    }

    //登录
    @Override
    public boolean queryObject(User obj) throws HibernateException {
        // TODO Auto-generated method stub

          String hql="from User where username='"+obj.getUsername()+"' and password='"+obj.getPassword()+"'";
          System.out.println(obj.getUsername());
          System.out.println(obj.getPassword());

          List userlist=this.getHibernateTemplate().find(hql);

          if (userlist.size()>0) 
          {
              System.out.println("登录成功"); 
              return true;
          }
          else 
          {
              System.out.println("登录失败"); 
              return false;
          }
    }
}
  • 在UserManager.java中写登录的接口
package com.ynwi.ssh.service;

import java.util.List;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.ynwi.ssh.forms.UserForm;
import com.ynwi.ssh.forms.AdminForm;
import com.ynwi.ssh.beans.Event;
import com.ynwi.ssh.beans.User;

public interface UserManager {

    //注册
    public void regUser(UserForm user);
    //登录
    public boolean logUser(UserForm user);
}
  • 在UserManagerImpl.java中写具体的实现
package com.ynwi.ssh.serviceImpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.springframework.beans.BeanUtils;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.ynwi.ssh.beans.Event;
import com.ynwi.ssh.beans.User;
import com.ynwi.ssh.beans.Admin;
import com.ynwi.ssh.dao.BaseDao;
import com.ynwi.ssh.forms.AdminForm;
import com.ynwi.ssh.forms.EventForm;
import com.ynwi.ssh.forms.UserForm;
import com.ynwi.ssh.service.UserManager;

public class UserManagerImpl  implements UserManager{

    private BaseDao dao;

    public void setDao(BaseDao dao)
    {
        this.dao=dao;
    }

    //注册
    @Override
    public void regUser(UserForm userForm) throws HibernateException {
        // TODO Auto-generated method stub

        User user=new User();
        BeanUtils.copyProperties(userForm, user);

        //session=HibernateSessionFactory.currentSession();
        //dao.setSession(session);
        // 获取事务  
       // Transaction ts = session.beginTransaction();  
        // 构造User对象  
       // User user = new User();  
       // user.setUsername(userForm.getUsername());  
      //  user.setPassword(userForm.getPassword());  
      //  user.setGender(userForm.getGender());  
        // 保存User对象  
        dao.saveObject(user);  
        // 提交事务  
        //ts.commit();  
        // 关闭Session  
       // HibernateSessionFactory.closeSession();

    }

    //登录
    @Override
    public boolean logUser(UserForm userForm) throws HibernateException{
        // TODO Auto-generated method stub

            User user=new User();
            BeanUtils.copyProperties(userForm, user);
            boolean a=dao.queryObject(user);
            return a;

    }
}

至此登录功能就已经完成,我会上传我的项目ZIP,需要的小伙伴可以请点击这里下载查看,一起学习,错误的地方请指出。


你可能感兴趣的:(SSH框架实现登录注)