码迷,mamicode.com
首页 > Web开发 > 详细

struts2+Hibernate实现用户登陆功能

时间:2014-12-15 18:56:08      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

实现的功能,在登陆页面输入Username和PassWord后,将username和password通过Hibernate匹对数据库是否含有一样的username和password,若有则登陆进去,若没有则返回到登陆页面

显示层

bubuko.com,布布扣
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
    <form action="userlogin">
          用户名:<input type="text" name="username"><br/>
          密码:<input type="password" name="password"><br/>
        <input type="submit" value="submit">
        <input type="reset" value="reset">
    </form>
</body>
</html>
View Code

 

model层

User

bubuko.com,布布扣
package com.zs.model;

public class User {
    private String username;
    private String password;
    private Short sex;
    private String email;
    private String address;
    private String grade;

    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;
    }

    public Short getSex() {
        return sex;
    }

    public void setSex(Short sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }
}
View Code

User.hbm.xml:

bubuko.com,布布扣
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-12-14 14:51:48 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.zs.model.User" table="USER">
        <id name="username" type="java.lang.String">
            <column name="USERNAME" />
            <generator class="assigned" />
        </id>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" />
        </property>
        <property name="sex" type="java.lang.Short">
            <column name="SEX" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
        <property name="grade" type="java.lang.String">
            <column name="GRADE" />
        </property>
    </class>
</hibernate-mapping>
View Code

 

Hibernate:

配置hibernate.hbm.xml:

bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
         <!--连接数据库的配置-->
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql:///hibernate</property>
        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--数据库映射Java用户-->
        <mapping resource="com/zs/model/User.hbm.xml" />
    </session-factory>
</hibernate-configuration>
View Code

 

Struts:

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

<struts>

    <package name="default" extends="struts-default">
        <!--登陆主页面时候的Action-->
        <action name="index">
            <result>/index.jsp</result>
        </action>
        <!--在index.jsp提交submit后若登陆成功则转到adminlogin,否则转到index-->
        <action name="userlogin" class="com.zs.web.admin.AdminLoginAction">
            <result name="success">/admin/adminlogin.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>

</struts>
View Code

 

Action:

bubuko.com,布布扣
package com.zs.web.admin;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zs.dao.UserDao;
import com.zs.dao.impl.UserDaoImpl;
import com.zs.model.User;

public class AdminLoginAction extends ActionSupport implements SessionAware {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    UserDao dao = new UserDaoImpl();
    private String username;
    private String password;
    private Map<String,Object> session;
    
    public String execute(){
            User user = dao.isValidAdmin(username, password);//将username和password传递到dao层交个hibernate判断
            if(user!=null){
                session.put("user", user);//若登陆成功则将该user记录在session中
                return SUCCESS;
            }else{
                addActionError("你没有管理权限");
                return INPUT;
            }
    }
    
    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;
    }

    public Map getSession() {
        return session;
    }

    public void setSession(Map session) {
        this.session = session;
    }

}
View Code

 

判断用户和密码是否存在于数据库中

bubuko.com,布布扣
package com.zs.dao.impl;

import java.util.List;

import com.zs.dao.BaseDaoImpl;
import com.zs.dao.UserDao;
import com.zs.model.User;

public class UserDaoImpl extends BaseDaoImpl implements UserDao {

    @Override
    public void saveUser(User user) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public List<User> getAll(Class clazz) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public User isValidUser(String username, String password) {
        // TODO Auto-generated method stub
        return null;
    }

    //判断数据库中时候含有该用户
    @Override
    public User isValidAdmin(String username, String password) {
        User user = null;
        //调用hibernate
        List<User> list =super.search("FROM User WHERE grade=‘admin‘ AND username =‘"+username+"‘ AND password = ‘"+password+"‘");
        if(list!=null && list.size()>0){//若找到的list不为空那么则说明数据库中包含这个人
            user = list.get(0);
        }
        return user;
    }

}
View Code

 

Hibernate初始化借口并且从数据库中取数据

bubuko.com,布布扣
package com.zs.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class BaseDaoImpl implements IBaseDao {

    private  SessionFactory sessionFactory;
    private  Session session;
    private  Transaction transaction;
    
    //初始session接口
    public void init(){
        Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                                  .buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
    }
    @Override
    public Session getSession() {
        init();
        return session;
    }

    @Override
    public void closeSession() {
        session.close();

    }

    @Override
    public List search(String hql) {
        //调用session接口
        Session session = null;
        session = getSession();
        List alist = null;
        alist = session.createQuery(hql).list();//从数据库中查询
        session.close();
        return alist;
    }

}
View Code

 

 

可以先建好一个名为User的数据库,设置一个用户,然后便可以测试了

完整代码可以在我github下载:https://github.com/calmound/web/tree/master/%E7%AE%A1%E7%90%86%E5%85%AC%E5%91%8A%E7%B3%BB%E7%BB%9F/GuanLiXiTong(%E7%AE%A1%E7%90%86%E5%91%98%E7%99%BB%E9%99%86)

struts2+Hibernate实现用户登陆功能

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/zsboy/p/4165365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!