简单的界面登录实现(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>
  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值