Using hibernate and struts to achieve a simple project login and registration _struts

Source: Internet
Author: User

-First of all, because you are a novice, now is also just contact with the three major Java framework so there may be some problems I hope you can point out to me, I will humbly accept.
First of all, I use the annotation of the hibernate to achieve the first hibernate annotation environment, I used reverse engineering to remove the entity class and its main configuration file Hibernate.cfg.xml file, and then Hbm.xml files that were generated at the same time, adding Current_session_context_ in the properties above The class value for thread is primarily for the subsequent session acquisition.
then write the configured properties in the entity class for example

@Entity @Table (name= "Types") public class types implements Java.io.Serializable {//Fields @Id @GeneratedVal UE (generator= "Seqs", Strategy=generationtype.sequence) @SequenceGenerator (name= "Seqs", sequencename= "seq_id",

    allocationsize=1) private Integer ID;

    @Column private String name;


@OneToMany (mappedby= "types") private set 

public void Validatelogin () {

    if (Username.trim (). Length () ==0)
    {
        this.addfielderror ("ErrorName", "User name cannot be empty");
    if (Password.trim (). Length () ==0)
    {
        this.addfielderror ("errorpwd", "Password cannot be null");
    }




 -Implement data access to the DAO layer
here's how to get a session:

Static Sessionfactory Sf=null;
static {

    Annotationconfiguration CF = new annotationconfiguration (). Configure ();
    Sf=cf.buildsessionfactory ();
}
public static Sessions GetSession () {return

    sf.getcurrentsession ();
}





> Here is the login data access code due to the way the session is accessed so must open things.

Public Users Login (String username,string password) throws Exception
{
Users U=null;
Transaction Tx=null;
Session S=null;
try {
S=hibernateutil.getsession ();
Tx=s.begintransaction ();
String hql= "from Users where name=?" and password=? ";
Query query=s.createquery (HQL);
Query.setstring (0, username);
Query.setstring (1,password);
u= (Users) Query.uniqueresult ();
Map Session=actioncontext.getcontext (). GetSession ();
Session.put ("username", u.getusername ());
Tx.commit ();

    catch (Exception e) {

        throw new Exception (e);
    }

    return u;

}

"'

Registration needs to be noted in the action to encapsulate it into the user class at the data access layer
The code follows, of course, in the action should implement the registered checksum.
' Public Integer register (Users u) throws exception{

 int result=0;
    Session s=hibernateutil.getsession ();

    Transaction tx=s.begintransaction ();

        try {result= (Integer) s.save (U);


    Tx.commit ();
    catch (Exception e) {throw new Exception (e);      
return result; }

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.