struts2之实现Action

介绍:Action类作为业务逻辑控制器,包含了对用户请求的处理逻辑, struts2通常直接使用Action封装Http请求参数,因此Action里还应该包含与请求参数对应的实例变量,并提供setter和getter方法,Action类=普通pojo+excute()方法,Action类是否包含是否包含实例变量并不重要,重要的是包含setter和getter方法,因为系统是通过这两个方法处理HTTP请求参数。系统不会严格区分Action里哪个实例变量用于封装请求参数,哪个实例变量用于封装处理结果,对系统而言,封装请求参数的实例变量和处理结果的实例变量完全平等,因此,Struts2的标签既可以输出Action的处理结果,也可以输出HTTP请求参数值,而Action类里可以封装其他用户自定义的类,数组,集合对象和Map对象等

struts2框架中的Action接口和ActionSupport超类:
Action接口包含结果字符串一个execute()方法,结果字符串用于execute()方法返回。
ActionSupport超类则实现了Action,可以直接作为业务控制器,如果Action没有指定class属性,系统自动使用ActionSupport类作为Action处理类

Action访问Servlet API:
1.Web应用中通常需要访问的Servlet API为HttpServlet、HTTPSession、ServletContext,这三个接口分别代表JSP内置对象中的request、session和application。Struts2提供了ActionContext类来访问这些API,ActionContext类常用方法为:
Object get(Object key):类似调用HttPServletRequest的getAttribute(String name)方法;
Map getApplication():返回Map对象,类似ServletContext实例
Map getParameters():获取所有请求参数,类似HttPServletRequest
的getParameterMap()方法
Map getSession():返回Map对象,类似HttpSession实例
2.使用ServletContextAware、ServletRequestAware、ServletResponseAware直接访问Servlet API
3.使用ServletActionContext访问Servlet API

用法举例:
在LoginAction下的excute()方法:

//定义处理用户请求的execute方法
	public String execute(){
		ActionContext ac=ActionContext.getContext();
		//通过ActionContext访问application范围的属性值
		Integer counter=(Integer) ac.getApplication().get("counter");
		if(counter==null){
			counter=1;
		}else{
			counter+=1;
		}
		//打印counter计数器
		System.out.println(counter);
		//设置application范围属性
		ac.getApplication().put("counter", counter);
		//设置session范围属性
		ac.getSession().put("user", getUsername());
		//如果用户名为test,密码为test则登录成功
		if(getUsername().equals("test") && getPassword().equals("test")){
			ac.put("tip", "服务器登录成功");
			return SUCCESS;
		}
		ac.put("tip", "服务器登录失败");
		return ERROR;
	}

在welcome.jsp中添加:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎页面</title>
</head>
<body>
本站访问次数:${applicationScope.counter}<br/>
${sessionScope.user},您已经登录!<br/>
${requestScope.tip}<br/>
</body>
</html>

登录:http://localhost:8080/ssh/loginForm后点击login按钮,如图所示:
在这里插入图片描述

Action直接访问Servlet API:
ServletContextAware:实现该接口可直接访问Web应用中的ServletContext
ServletRequestAware:实现该接口可直接访问Web应用的HttpServletRequest实例
ServletResponseAware实现该接口可直接访问HttpServletResponse实例
将LoginAction.java改为:

public class LoginAction extends ActionSupport implements ServletContextAware,ServletRequestAware,ServletResponseAware{
	private String username;
	private String password;
	private ServletContext context;
	private HttpServletRequest request;
	private HttpServletResponse response;
	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;
	}
    public void setServletResponse(HttpServletResponse response) {
		this.response=response;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}
	public void setServletContext(ServletContext context) {
		this.context=context;
	}
	
	//定义处理用户请求的execute方法
	public String execute(){
		ActionContext ac=ActionContext.getContext();
		//通过ActionContext访问application范围的属性值
		Integer counter=(Integer) ac.getApplication().get("counter");
		if(counter==null){
			counter=1;
		}else{
			counter+=1;
		}
		//打印counter计数器
		System.out.println("计数器:"+counter);
		//设置application范围属性
		ac.getApplication().put("counter", counter);
		//设置session范围属性
		ac.getSession().put("user", getUsername());
		//如果用户名为test,密码为test则登录成功
		if(getUsername().equals("test") && getPassword().equals("test")){
			//通过response添加Cookie
			Cookie cookie=new Cookie("user", getUsername());
			cookie.setMaxAge(60*60);
			response.addCookie(cookie);
			//通过
			//通过ActionContext设置request范围属性
			ac.put("tip", "服务器登录成功");
			return SUCCESS;
		}
		ac.put("tip", "服务器登录失败");
		return ERROR;
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值