用SPRING+STRUTS+HIBERNATE实现注册与登录

目的:建立一个SPRING+STRUTS+HIBERNATE的登录注册WEB程序。

环境与工具:MYECLIPSE+TOMCAT+MYSQL 

步骤一:建立TOMCAT工程mySSH,在/WEB-INF/LIB目录下COPY 上 3个框架所需要的包,构建JAVA路径。

              建立包结构com.neusoft.vo;com.neusoft.action;com.neusoft.dao;com.neusoft.dao.impl;com.neusoft.service;com.neusoft.service.impl;

       用MYSQL在TEST数据库里建立表user;字段为id,user,password

步骤二:在WEB-INF目录下建立web.xml 如下:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>actionServlet</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>actionServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
 
 
  <welcome-file-list>
 <welcome-file>/jsp/login.jsp</welcome-file>
 </welcome-file-list>
 
  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/spring-config.xml</param-value>

  </context-param>
 
</web-app>

步骤三:在WEB-INF目录下建立STRUTS配置文件struts-config.xml如下:

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

<struts-config>
 
  <form-beans>
 <form-bean name="user" type="com.neusoft.vo.User" />
  </form-beans>

   <action-mappings >
    <action path="/input" type="org.apache.struts.actions.ForwardAction" parameter="/jsp/regedit.jsp" />
    <action path="/regeditte" type="org.springframework.web.struts.DelegatingActionProxy" name="user">
      <forward name="success" path="/jsp/success.jsp"></forward>
       <forward name="input" path="/jsp/regedit.jsp"></forward>
      </action>
    
     <action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="user">
      <forward name="success" path="/jsp/success.jsp"></forward>
       <forward name="login" path="/jsp/login.jsp"></forward>
      </action>
  </action-mappings>


<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"><set-property property="contextConfigLocation" value="/WEB-INF/spring-config.xml"/>
</plug-in>
</struts-config>

步骤四:在WEB-INF目录下建立SPRING配置文件spring-config.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<!-- 定义数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName">
   <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="url">
   <value>jdbc:mysql://localhost/test</value>
  </property>
  <property name="username">
   <value>root</value>
  </property>
  <property name="password">
   <value>mysql</value>
  </property>
 </bean>

<!-- 和HIBERNATE联系起来 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
  <property name="mappingResources">
 <value>com/neusoft/vo/User.hbm.xml</value>
</property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>


<!-- 进行事务处理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>


<!-- 进行DAO代理 -->
<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="target">
   <ref local="userDAO" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>


<bean id="userDAO" class="com.neusoft.dao.impl.UserDAOImpl">
<property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

<bean id="rege" class="com.neusoft.service.impl.RegeditImpl">
<property name="userDAO"><ref bean="userDAOProxy"/> </property>
</bean>

<bean name="/regeditte" class="com.neusoft.action.RegeditAction" >
<property name="regedit"><ref bean="rege" /> </property>
</bean>

<bean id="login" class="com.neusoft.service.impl.LoginImpl">
<property name="userDAO"><ref bean="userDAOProxy"/> </property>
</bean>
<bean name="/login" class="com.neusoft.action.LoginAction" >
<property name="login"><ref bean="login" /></property></bean>

</beans>


步骤五:在WEB-INF/src建立 LOG4J配置文件log4j.properties

# Configure logging for testing: optionally with log file
log4j.rootLogger=DEBUG, stdout
# log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

步骤六:在/WEB-INF/src/com/neusoft/vo下用myeclipse的DataBaseExplorer链接上数据库TEST后

         点击表user右键,让工具生成User.hbm.xml,POJO(User.java),如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="com.neusoft.vo.User" table="user" >
        <id name="id" type="java.lang.String">
            <column name="id" length="32" />
            <generator class="uuid.hex" />
        </id>
        <property name="username" type="java.lang.String">
            <column name="username" length="32" not-null="true" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="32" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

VO对象User.java如下:

package com.neusoft.vo;

import org.apache.struts.action.ActionForm;
// Generated by MyEclipse - Hibernate Tools

 

/**
 * User generated by MyEclipse - Hibernate Tools
 */
public class User extends ActionForm{

    private String id ;

    private String username ;

    private String password ;

    public User(String username,String password){

       this.username =username;

       this.password =password;

    }

    public User(){

      

    }

    public String getId() {

       return id ;

    }

    public void setId(String id) {

       this.id = id;

    }

    public String getPassword() {

       return password ;

    }

    public void setPassword(String password) {

       this.password = password;

    }

    public String getUsername() {

       return username ;

    }

    public void setUsername(String username) {

       this.username = username;

    }  

}

步骤七:在WEB-INF/src/com/neusoft/dao下建立 UserDAO.java

package com.neusoft.dao;
import com.neusoft.vo.*;
public interface UserDAO
{
  public abstract void createUser(User user);
  public abstract void updateUser(User user);
  public abstract void deleteUser(User user);
  public abstract User queryUser(String name);
}
步骤八:在WEB-INF/src/com/neusoft/dao/impl    下建立 UserDAOImpl.java

package com.neusoft.dao.impl;

import java.util.ArrayList;
import java.util.List;

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

import com.neusoft.dao.UserDAO;
import com.neusoft.vo.User;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO
{
 private Logger logger=Logger.getLogger(this.getClass().getName());
 private SessionFactory    sessionFactory;
/* public SessionFactory getSessionFactory(){return this.sessionFactory;}*/
 
 
 private String hql="from User where username=?";
    public void createUser(User user)
    {
     this.getHibernateTemplate().save(user);
    }

    public void deleteUser(User user)
    {
       this.getHibernateTemplate().delete(user);

    }

  

    public void updateUser(User user)
    {
        this.getHibernateTemplate().update(user);

    }
    public User queryUser(String name)
    {
        List userlist;
        if(this.getHibernateTemplate().find(hql,name)==null)
            {
            userlist=new ArrayList(); return new User();
            }
        else
            userlist=this.getHibernateTemplate().find(hql,name);
        return (User)userlist.get(0);
       

    }

}
步骤九:在WEB-INF/src/com/neusoft/service下建立 Login.java 和Regedit.java

package com.neusoft.service;

import com.neusoft.vo.User;

public interface Login
{
    public abstract User queryUser(String name);
}

 

package com.neusoft.service;
import com.neusoft.vo.*;
public interface Regedit
{
 public abstract void saveUser(User user);
}
步骤十:在WEB-INF/src/com/neusoft/service/impl下建立 LoginImpl.java 和RegeditImpl.java

 

package com.neusoft.service.impl;

import com.neusoft.dao.UserDAO;
import com.neusoft.service.Login;
import com.neusoft.vo.User;

public class LoginImpl implements Login
{
   
    private UserDAO userDAO;
    public UserDAO getUserDAO(){
        return userDAO;
    }
    public void setUserDAO(UserDAO userDAO){
       
        this.userDAO=userDAO;
    }
  

  
     public User queryUser(String name)
     {
        
         return userDAO.queryUser(name);

     }
  
}

 

如下:

package com.neusoft.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;


import com.neusoft.dao.UserDAO;
import com.neusoft.service.Regedit;
import com.neusoft.vo.User;

public class RegeditImpl implements Regedit
{
   private UserDAO userDAO;
   public UserDAO getUserDAO(){
       return userDAO;
   }
   public void setUserDAO(UserDAO userDAO){
      
       this.userDAO=userDAO;
   }
    public void saveUser(User user)
    {
        userDAO.createUser(user);

    }

   
    public void deleteUser(User user)
    {
        userDAO.deleteUser(user);

    }

  

    public void updateUser(User user)
    {
        userDAO.updateUser(user);

    }
    public User queryUser(String name)
    {
        return userDAO.queryUser(name);

    }
   
 
}
步骤十一:在mySSH/WEB-INF/src/com/neusoft/action建立LoginAction.java和RegeditAction.java

如下:

package com.neusoft.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.neusoft.service.Login;
import com.neusoft.vo.User;


public class LoginAction extends Action
{
  private Login login;
  public Login getLogin(){return this.login;}
  public void setLogin(Login login){this.login=login;}
  
  private Logger logger=Logger.getLogger(this.getClass().getName());
  public ActionForward execute(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)throws Exception
  {
    
      
      User user=this.getLogin().queryUser(((User)form).getUsername());
       if(user.getPassword().equals(((User)form).getPassword()))
       return mapping.findForward("success");
     
       else {
           request.setAttribute("msg", "登录失败,请重新登录");
           return mapping.findForward("login");}
  }
}

注册ACTION如下:

package com.neusoft.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.neusoft.dao.UserDAO;
import com.neusoft.service.Regedit;
import com.neusoft.vo.*;


public class RegeditAction extends Action
{
 private Logger logger=Logger.getLogger(this.getClass().getName());
 
 private Regedit regedit;
 
 public Regedit getRegedit(){
     return regedit;
 }
 public void setRegedit(Regedit regedit){
    
     this.regedit=regedit;
 }
/* private UserDAO userDAO;
 public UserDAO getUserDAO(){
     return this.userDAO;
 }
 public void setUserDAO(UserDAO userDAO)
 {
    
     this.userDAO=userDAO;
 }*/
 public ActionForward execute(ActionMapping mapping, ActionForm form,
         HttpServletRequest request, HttpServletResponse response)throws Exception
 {
   
     User user=(User)form;
 
     request.setAttribute("user", (User)form);
     this.getRegedit().saveUser(user);
     return mapping.findForward("success");
    
   
 }


}

工程图如下:

 

数据库图如下:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值