Simple interface Login implementation (Struts+hibernate implementation)

Source: Internet
Author: User
Tags generator
#创建表User
CREATE TABLE usee (
ID int),
NUM varchar,
NAME varchar,
PASSWORD varchar,
PRIMARY KEY (ID))

Generate the appropriate bean class in the compiler and map the XML (you can also use annotations)

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;
    The 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.sourceforg E.net/hibernate-mapping-3.0.dtd "> <?xml version= ' 1.0 ' encoding= ' utf-8 '?> <! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration dtd//en" "Http://www.hibernat E.org/dtd/hibernate-configuration-3.0.dtd ">  

Then start building 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 the 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; }
        }

    }



}

The Userdao.login (NAME,PWD) is used for the value of the data, while the database query

public class Userdao {public

    static Usee login (String name,string pwd) {
        session session = Hibernateutls.getsessi On ();
        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;}}
}

Start Configuration 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>

Add suceess.jsp and error.jsp to add form forms in index.jsp

index.jsp: <%@ taglib prefix= "s" uri= "/struts-tags"%> <%--Created by IntelliJ idea. User:user date:16-12-21 time: The morning 11:38 to the change this template use File | Settings |
File Templates. --%> <%@ page contenttype= "Text/html;charset=utf-8" language= "java"%> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.