1. 程式人生 > >簡單的介面登入實現(struts+hibernate實現)

簡單的介面登入實現(struts+hibernate實現)

#建立表User
CREATE TABLE USEE(
ID int(11),
NUM VARCHAR(45),
NAME VARCHAR(45),
PASSWORD VARCHAR(45),
PRIMARY KEY (ID))

在編譯器中生成相應的bean類以及對映xml(也可以使用註解)

public class Usee implements Serializable {

    private int ID;
    private String num;
    private String name;
    private String password;

    public
Usee() { } public int getID() { return ID; } public String getNum() { return num; } public String getName() { return name; } public String getPassword() { return password; } public void setID(int ID) { this.ID = ID; } public
void setNum(String num) { this.num = num; } public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } } 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="lee.Hibernate.Usee" table="USEE"> <id name="ID" type="java.lang.Integer" column="ID"> <generator class="increment"></generator> </id> <property name="name" type="java.lang.String" column="NAME" length="45"/> <property name="num" type="java.lang.String" column="NUM" length="45"/> <property name="password" type="java.lang.String" column="PASSWORD" length="45"/> </class> </hibernate-mapping>

新增hibernate.cfg.xml配置

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--配置資料庫JDBS的驅動-->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/leehuan</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">lihuan</property>
        <property name="connection.pool_size">1</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property> <!--全域性事務-->
        <property name="connection.isolation">4</property> <!--設定隔離級別-->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <mapping resource="Usee-hbm.xml"/>
        <mapping resource="User-hbm.xml"/>
    </session-factory>
</hibernate-configuration>

然後開始生成Action

package lee.struts;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import lee.Hibernate.Usee;
import lee.Hibernate.User;
import lee.Hibernate.UserDAO;
import org.apache.commons.logging.Log;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by user on 16-12-22.
 */
public class LoginAction extends ActionSupport {


    private String name;
    private String pwd;

    public String getName() {
        return name;
    }

    public String getPwd() {
        return pwd;
    }

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

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String execute() throws Exception {
        Login();
        return super.execute();
    }

    public String Login(){
        ActionContext context = ActionContext.getContext();
        HttpServletRequest request = ServletActionContext.getRequest();
        String name = (String) request.getParameter("name");
        String pwd = (String) request.getParameter("pwd");
        if (name.isEmpty() || pwd.isEmpty()){
            return ERROR;
        }else {
            Usee login = UserDAO.login(name, pwd);
            if(login!=null){
                return SUCCESS;
            }else {
                return ERROR;
            }
        }

    }



}

其中的UserDao.Login(name,pwd)用來進行傳值,同時進行資料庫的查詢

public class UserDAO {

    public static Usee login(String name,String pwd){
        Session session = HibernateUtls.getSession();
        List<Usee> list = new ArrayList<>();

        list = session.createQuery("from Usee u where u.name=? and u.password=?").setParameter(0,name).setParameter(1,pwd).list();
        if(list.size()!=0) {
            Usee usee = list.get(0);
            return usee;
        }else {
            return null;
        }
    }
}

開始配置struts.xml

<?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" namespace="/" extends="struts-default">
        <action name="login" class="lee.struts.LoginAction">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

新增suceess.jsp與error.jsp,在index.jsp中新增form表單

index.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 16-12-21
  Time: 上午11:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
      <s:form action="login" method="post" theme="simple">
          <p align="center">
              使用者名稱:
              <s:textfield name="name" size="8" theme="simple"></s:textfield><br>
              密碼:
              <s:password name="pwd" size="8" theme="simple"></s:password><br>
              <s:submit value="提交" method="Login"></s:submit>
              <input type="reset" value="取消" name="sumit">
          </p>
      </s:form>
  </body>
</html>

success.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 16-12-21
  Time: 下午2:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>welcome <s:property value="name"></s:property></h1>
</body>
</html>

error.jsp:
<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 16-12-22
  Time: 上午10:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    使用者或密碼錯誤,請點選<a href="index.jsp">返回</a>重新登入
</body>
</html>