Lưu Quang Triệu

Không ngừng sáng tạo thì sẽ không sợ bị diệt vong

Login vs struct and hibernate

Posted by millionking on August 28, 2012

Bước 1:

Tạo model:

private Long userid;
private String useraccount;
private String password;
private Date createdate;
private Long createby;
private Date updatedate;
private Long updateby;
private String isdeleted;

// Constructors

/** default constructor */
public User() {
}

/** minimal constructor */
public User(String useraccount, String password) {
super();
this.useraccount = useraccount;
this.password = password;
}
public User(String useraccount, String password, String isdeleted) {
this.useraccount = useraccount;
this.password = password;
this.isdeleted = isdeleted;
}

/** full constructor */
public User(String useraccount, String password, Date createdate, Long createby, Date updatedate, Long updateby, String isdeleted) {
this.useraccount = useraccount;
this.password = password;
this.createdate = createdate;
this.createby = createby;
this.updatedate = updatedate;
this.updateby = updateby;
this.isdeleted = isdeleted;
}
public Long getUserid() {
return this.userid;
}

public void setUserid(Long userid) {
this.userid = userid;
}
public String getUseraccount() {
return this.useraccount;
}

public void setUseraccount(String useraccount) {
this.useraccount = useraccount;
}

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

Bước 2: Tạo ra controller xử lý

package controllers;

import java.util.Iterator;
import org.hibernate.*;

import models.User;
import utils.HibernateUtil;

public class LoginController {

@SuppressWarnings(“unchecked”)
public boolean find(String name,String password){
Session session = HibernateUtil.getSession();
String SQL_QUERY =” from User ih where ih.useraccount='” + name + “‘ and ih.password='” + password + “‘”;
Query query = session.createQuery(SQL_QUERY);
Iterator<User> it=query.iterate();
while(it.hasNext())
{
session.close();
return true;
}
session.close();
return false;
}
}

Bước 3: Viết actions thực thi

package actions;

import com.opensymphony.xwork2.ActionSupport;
import models.User;
import controllers.LoginController;

public class LoginAction extends ActionSupport {

/**
* @return
*/
private static final long serialVersionUID = 1L;
private String useraccount;
private String password;
@SuppressWarnings(“unused”)
private User us = new User();

public void validate(){
if(useraccount.length()==0)
this.addFieldError(“name”, “Name is required..”);
if(password.length()==0)
this.addFieldError(“password”, “Password is required..”);
}
public String execute() {
LoginController login = new LoginController();
if(login.find(getUseraccount(), getPassword()))
return SUCCESS;
else
this.addActionError(“Invalid UserName and Password..”);
return “error”;
}
public String getUseraccount() {
return useraccount;
}
public void setUseraccount(String useraccount) {
this.useraccount = useraccount;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

Tạo trang jsp

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<link href=”<%=basePath%>/themes/<%=currentTheme%>/common/css/login.css” rel=”stylesheet” type=”text/css” />
<title><%=resourceBundle.getString(“login.page.title”) %></title>
</head>

<body topmargin=”0″ leftmargin=”0″>
<!–Bắt Đầu–>
<center><div>
<div>
<s:actionerror/>
<s:form action=”login” >
<s:textfield name=”useraccount” label=”Name”/>
<s:password name=”password” label=”Password”/>
<s:submit value=”Login”/>
</s:form>
</div>
<!– <div>COPYRIGHT 2011 BY RUNSYSTEM. ALL RIGHTS RESERVED.</div> –>
</div></center>
<!–Kết Thúc–>
</body>
</html>

Config structs

<package name=”default” extends=”struts-default” namespace=”/”>
<action name=”login”>
<result name=”success”>Index.jsp</result>
<result name=”input”>login.jsp</result>
<result name=”error”>login.jsp</result>
</action>
</package>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: