Action的实现

使用Struts 2 开发,需要大量使用Action类,并在struts.xml文件中配置Action。

Struts 2 通常直接使用Action来封装HTTP请求参数,因此,Action类还要有与请求参数对应的属性,并生成setter和getter方法。

public class LoginAction {

	//提供两个属性来封装HTTP请求参数
	private String username;
	private String password;
	//生成的getter和setter方法
	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;
	}
	//Action类默认处理用户请求的方法:execute方法
	public String execute(){
		return "SUCCESS";
	}
}
在Action中不严格区分哪个属性是用于封装请求参数,哪个属性是用于封装处理结果。
Struts 2 中提供了一个Action接口和一个Action接口的实现类:ActionSupport。自己写Action时,可以实现Action接口或继承ActionSupport来重写execute方法。

Action访问Servlet API的方法:

1、Struts 2提供了一个ActionContext类,Action可以通过该类来访问Servlet API。

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements Action {

	//提供两个属性来封装HTTP请求参数
	private String username;
	private String password;
	//生成的getter和setter方法
	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;
	}
	//Action类默认处理用户请求的方法:execute方法
	public String execute(){
		
		ActionContext actionContext = ActionContext.getContext();
		//通过ActionContext访问application范围的属性值
		Integer counter = (Integer)actionContext.getApplication().get("counter");
		if(counter == null){
			counter = 1;
		} else {
			counter++;
		}
		//通过ActionContext设置application范围的属性值
		actionContext.getApplication().put("counter", counter);
		//通过ActionContext设置session范围的属性
		actionContext.getSession().put("username", username);
		if(getUsername().equals("admin") && getPassword().equals("123")){
			//通过ActionContext设置request范围的属性
			actionContext.put("tip", "登录成功");
			return SUCCESS;
		} else {
			//通过ActionContext设置request范围的属性
			actionContext.put("tip", "登录失败");
			return ERROR;
		}
	}
}

2、Struts 2提供了ServletContextAware、ServletRequestAware、ServletResponseAware,Action可以通过实现接口来访问Servlet API。

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements Action,ServletResponseAware{

	//提供两个属性来封装HTTP请求参数
	private String username;
	private String password;
	
	private HttpServletResponse response;
	//生成的getter和setter方法
	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;
	}
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	//Action类默认处理用户请求的方法:execute方法
	@Override
	public String execute(){
		
		ActionContext actionContext = ActionContext.getContext();
		//通过ActionContext访问application范围的属性值
		Integer counter = (Integer)actionContext.getApplication().get("counter");
		if(counter == null){
			counter = 1;
		} else {
			counter++;
		}
		//通过ActionContext设置application范围的属性值
		actionContext.getApplication().put("counter", counter);
		//通过ActionContext设置session范围的属性
		actionContext.getSession().put("username", username);
		if(getUsername().equals("admin") && getPassword().equals("123")){
			//通过response添加cookie
			Cookie cookie = new Cookie("username", getUsername());
			cookie.setMaxAge(60 * 60);
			response.addCookie(cookie);
			//通过ActionContext设置request范围的属性
			actionContext.put("tip", "登录成功");
			return SUCCESS;
		} else {
			//通过ActionContext设置request范围的属性
			actionContext.put("tip", "登录失败");
			return ERROR;
		}
	}
}


3、Struts 2提供了ServletActionContext,Action可以通过该类来访问Servlet API。

import javax.servlet.http.Cookie;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements Action{

	//提供两个属性来封装HTTP请求参数
	private String username;
	private String password;
	//生成的getter和setter方法
	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;
	}
	
	//Action类默认处理用户请求的方法:execute方法
	@Override
	public String execute(){
		
		ActionContext actionContext = ActionContext.getContext();
		//通过ActionContext访问application范围的属性值
		Integer counter = (Integer)actionContext.getApplication().get("counter");
		if(counter == null){
			counter = 1;
		} else {
			counter++;
		}
		//通过ActionContext设置application范围的属性值
		actionContext.getApplication().put("counter", counter);
		//通过ActionContext设置session范围的属性
		actionContext.getSession().put("username", username);
		if(getUsername().equals("admin") && getPassword().equals("123")){
			
			//通过response添加cookie
			Cookie cookie = new Cookie("username", getUsername());
			cookie.setMaxAge(60 * 60);
			ServletActionContext.getResponse().addCookie(cookie);
			
			//通过ActionContext设置request范围的属性
			actionContext.put("tip", "登录成功");
			return SUCCESS;
		} else {
			//通过ActionContext设置request范围的属性
			actionContext.put("tip", "登录失败");
			return ERROR;
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值