用Struts2.0+Hibernate+Spring实现用户登陆

来源:互联网 发布:分销系统 php 编辑:程序博客网 时间:2024/05/17 10:38

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.article.bean.User" table="user"> 
        
<id name="id" column="id"> 
            
<generator class="native"/> 
        
</id> 
        
<property name="name" column="name"/> 
        
<property name="password" column="password"/> 
    
</class> 
</hibernate-mapping>

User.java

package com.article.bean;

import java.io.Serializable;

public class User implements Serializable{
    
/**
     * 
     
*/

    
private static final long serialVersionUID = 1L;
    
private int id=0;
    
private String name="";
    
private String password="";
    
public String getName() {
        
return name;
    }

    
public void setName(String name) {
        
this.name = name;
    }

    
public String getPassword() {
        
return password;
    }

    
public void setPassword(String password) {
        
this.password = password;
    }

    
public int getId() {
        
return id;
    }

    
public void setId(int id) {
        
this.id = id;
    }

}

LogicDAO.java

package com.article.dao;

public class LogicDAO {
    
private UserDAO userDAO;
    
public UserDAO getUserDAO() {
        
return userDAO;
    }

    
public void setUserDAO(UserDAO userDAO) {
        
this.userDAO = userDAO;
    }

}

UserDAO.java

package com.article.dao;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.article.bean.User;

public class UserDAO {
    
private HibernateTemplate hibernateTemplate;

    
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        
this.hibernateTemplate = hibernateTemplate;
    }

    
public User Login(User user)
    
{
        List list
=hibernateTemplate.find("from User user where user.name='"+user.getName()+"' and user.password=md5('"+user.getPassword()+"')");
        
if(list!=null && list.size()==1)
            
return (User)list.get(0);
        
else
            
return null;
    }

}

BaseAction.java

package com.article.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.article.dao.LogicDAO;
import com.opensymphony.xwork2.Action;
public abstract class BaseAction implements Action{
    
public LogicDAO logicDAO() {
        ServletContext servletContext 
= ServletActionContext.getServletContext();
        WebApplicationContext webApplicationContext
= WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        
return (LogicDAO)webApplicationContext.getBean("logicDAO");    
    }

    
public HttpServletRequest request()
    
{
        
return ServletActionContext.getRequest();
    }

    
public HttpSession session()
    
{
        
return ServletActionContext.getRequest().getSession();
    }

    
public ServletContext application()
    
{
        
return ServletActionContext.getServletContext();
    }

    
public HttpServletResponse response()
    
{
        
return ServletActionContext.getResponse();
    }

}

LoginAction.java

package com.article.action;
import com.article.bean.User;
public class LoginAction extends BaseAction{
    
private User user=new User();
    
private String validateCode="";
    
public void setValidateCode(String validateCode) {
        
this.validateCode = validateCode;
    }


    
public User getUser() {
        
return user;
    }


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


    
public String execute() throws Exception {
        
if(this.session().getAttribute("rand")!=null)
        
{
            
if(this.validateCode.equals(this.session().getAttribute("rand").toString()))
            
{
                User resultUser
=this.logicDAO().getUserDAO().Login(user);
                
if(resultUser!=null)
                
{
                    
this.session().setAttribute("user", resultUser);
                }

            }

        }

        
return SUCCESS;
    }

    
public String getValidateCode() {
        
return validateCode;
    }

}

struts.xml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
<struts>
    
<include file="struts-default.xml"/>
    
<package name="default" extends="struts-default">
        
<action name="login" class="com.article.action.LoginAction">
             
<result name="success">/WEB-INF/jsp/index.jsp</result>
        
</action>                     
    
</package>
</struts>

applicationContext.xml

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

<beans>
    
<bean id="dataSource" 
          class
="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        
<property name="driverClassName"> 
            
<value>com.mysql.jdbc.Driver</value> 
        
</property> 
        
<property name="url"> 
            
<value>jdbc:mysql://localhost:3306/article?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=gbk</value> 
        
</property> 
        
<property name="username"> 
            
<value>root</value> 
        
</property> 
        
<property name="password"> 
            
<value>123456</value> 
        
</property> 
    
</bean> 
    
    
<bean id="sessionFactory" 
          class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" 
          destroy-method
="close"> 
        
<property name="dataSource"> 
            
<ref bean="dataSource"/> 
        
</property> 
        
<property name="mappingResources"> 
           
<list> 
          
<value>com/article/bean/User.hbm.xml</value>
           
</list> 
        
</property> 
        
<property name="hibernateProperties"> 
            
<props> 
                
<prop key="hibernate.dialect"> 
                    org.hibernate.dialect.MySQLDialect 
                
</prop> 
                
<prop key="hibernate.show_sql">
                    false
                   
</prop>
            
</props> 
        
</property> 
    
</bean> 

    
<bean id="hibernateTemplate"
          class
="org.springframework.orm.hibernate3.HibernateTemplate">
        
<property name="sessionFactory"> 
            
<ref bean="sessionFactory"/>
        
</property>            
    
</bean> 
     
<bean id="logicDAO" class="com.article.dao.LogicDAO">
         
<property name="userDAO">
             
<ref bean="userDAO"/>
         
</property>
     
</bean>
     
<bean id="userDAO"
           class
="com.article.dao.UserDAO">
        
<property name="hibernateTemplate"> 
            
<ref bean="hibernateTemplate"/>
        
</property>            
     
</bean> 
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns
="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
    
<filter>
        
<filter-name>article</filter-name>
        
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    
</filter>
    
<filter-mapping>
        
<filter-name>article</filter-name>
        
<url-pattern>/*</url-pattern>
    
</filter-mapping>
    
<listener>
        
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    
</listener>
</web-app>

login.jsp

            <form action="login.action" method="post">
            
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CDCDCD">
                
<tr bgcolor="#FAFAFA">
                    
<td>        
                        
<table border="0" cellspacing="0" cellpadding="0" align="center">
                            
<tr>
                                
<td>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="user.name" class="text"/></td>
                            
</tr>
                            
<tr>
                                
<td>&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="user.password" class="text"/></td>
                            
</tr>
                            
<tr>
                                
<td>验证码<input type="text" name="validateCode" class="text" style="width:38px;"/><img src="util/imgcode.jsp"/></td>
                            
</tr>
                            
<tr>
                                
<td align="right"><input type="submit" value="登陆"  style="border-style:none; background-color:#FAFAFA"/></td>
                            
</tr>
                        
</table>
                    
</td>
                
</tr>
            
</table>
            
</form>