www.pudn.com > struts2.rar > LoginAction.java, change:2009-08-20,size:3385b


package com.test.action; 
 
import java.util.Map; 
 
import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.apache.struts2.ServletActionContext; 
import org.apache.struts2.interceptor.ServletRequestAware; 
import org.apache.struts2.interceptor.ServletResponseAware; 
 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionSupport; 
 
public class LoginAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{ 
	 
	private String username; 
	private String password; 
	public String getUsername() { 
		return username; 
	} 
	public void setUsername(String username) { 
		this.username = username; 
	} 
	public String getPassword() { 
		return password; 
	} 
	public void setPassword(String password) { 
		this.password = password; 
	} 
	 
	//通过ServletActionContext类直接获得HttpServletRequest HttpServletResponse 
	private HttpServletRequest request1 = ServletActionContext.getRequest(); 
	private HttpServletResponse response1 = ServletActionContext.getResponse(); 
	 
	//通过ServletRequestAware,ServletResponseAware接口获得ServletRequestAware,ServletResponseAware 
	private HttpServletRequest request; 
	private HttpServletResponse response; 
	//在action被实例化是由struts直接调用,并传入HttpServletRequest对象 
	public void setServletRequest(HttpServletRequest request) { 
		this.request = request; 
		 
	} 
	//在action被实例化是由struts直接调用,并传入HttpServletResponse对象 
	public void setServletResponse(HttpServletResponse response) { 
		this.response = response; 
	} 
	public void validate() { 
		// TODO Auto-generated method stub 
		if(null == this.getUsername()||"".equals(this.getUsername().trim())) 
		{ 
			this.addFieldError("username", "username is required."); 
		} 
		 
		if(null == this.getPassword()||"".equals(this.getPassword().trim())) 
		{ 
			this.addFieldError("password", "password is required."); 
		} 
	} 
	 
	public String execute() throws Exception 
	{ 
		if("hello".equals(this.getUsername().trim()) && "world".equals(this.getPassword().trim())) 
		{ 
			Map map = ActionContext.getContext().getSession(); 
			 
			map.put("user","valide"); 
			 
			ActionContext.getContext().put("yourname","zhangsan");//相当于request.setAttribute() 
			 
			//HttpServletRequest 
			request.setAttribute("myname","ck"); 
			 
			//HttpServletResponse 
			Cookie cookie = new Cookie("username",this.getUsername()); 
			response.addCookie(cookie); 
			 
			return "success"; 
		}else 
		{ 
			this.addFieldError("username","username or password is wrong."); 
			return "failer"; 
		} 
	} 
	 
	public String denglu() throws Exception 
	{ 
		System.out.println("denglu function invoked."); 
		 
		if("hello".equals(this.getUsername().trim()) && "world".equals(this.getPassword().trim())) 
		{ 
			Map map = ActionContext.getContext().getSession(); 
			 
			map.put("user","valide"); 
			 
			ActionContext.getContext().put("yourname","zhangsan");//相当于request.setAttribute() 
			 
			//HttpServletRequest 
			request.setAttribute("myname","ck"); 
			 
			//HttpServletResponse 
			Cookie cookie = new Cookie("username",this.getUsername()); 
			response.addCookie(cookie); 
			 
			return "success"; 
		}else 
		{ 
			this.addFieldError("username","username or password is wrong."); 
			return "failer"; 
		} 
	} 
	 
 
}