struts2系列十之4中获取使用request,session,application的方法和页面显示的三种方法【后期ognl算第四种】

总结

第一种:通过直接获取

request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();

第二种 实现接口

class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware

第三种:通过直接获取

request = ServletActionContext.getRequest();
session = request.getSession();

第四种:通过实现接口

class LoginAction4 extends ActionSupport implements ServletRequestAware

第一种:

package com.bjsxt.struts2.user.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction1 extends ActionSupport {
	
	private Map request;
	private Map session;
	private Map application;
	
	public LoginAction1() {
		request = (Map)ActionContext.getContext().get("request");
		session = ActionContext.getContext().getSession();
		application = ActionContext.getContext().getApplication();
	}
	
	public String execute() {
		request.put("r1", "r1");
		session.put("s1", "s1");
		application.put("a1", "a1");
		return SUCCESS; 
	}
	
	
}
第二种:

package com.bjsxt.struts2.user.action;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {
	
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	
	//DI dependency injection
	//IoC inverse of control
	public String execute() {
		request.put("r1", "r1");
		session.put("s1", "s1");
		application.put("a1", "a1");
		return SUCCESS; 
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}
	
	
}

第三种:

package com.bjsxt.struts2.user.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction3 extends ActionSupport {
	
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;
	
	public LoginAction3() {
		request = ServletActionContext.getRequest();
		session = request.getSession();
//		application = session.getServletContext();
	}
	
	public String execute() {
		request.setAttribute("r1", "r1");
		session.setAttribute("s1", "s1");
//		application.setAttribute("a1", "a1");
		return SUCCESS; 
	}
	
}

第四中:

package com.bjsxt.struts2.user.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction4 extends ActionSupport implements ServletRequestAware {
	
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;
	
	
	
	public String execute() {
		request.setAttribute("r1", "r1");
		session.setAttribute("s1", "s1");
//		application.setAttribute("a1", "a1");
		return SUCCESS; 
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.session = request.getSession();
//		this.application = session.getServletContext();
							
	}
	
}

2

登录页面

<form name="f" action="" method="post">
用户名:<input type="text" name="name"/>
密码:<input type="text" name="password"/>
<br />
<input type="button" value="submit1" οnclick="javascript:document.f.action='login/login1';document.f.submit();" />
<input type="button" value="submit2" οnclick="javascript:document.f.action='login/login2';document.f.submit();" />
<input type="button" value="submit3" οnclick="javascript:document.f.action='login/login3';document.f.submit();" />
<input type="button" value="submit4" οnclick="javascript:document.f.action='login/login4';document.f.submit();" />
</form>

3 获取值得页面 

用了三种方法

a:#requestxxxx

b: request。getAttribute(...)

c: 直接属性取值#attr.ri

 <%@taglib uri="/struts-tags" prefix="s" %>

User Login Success!
	<br />
	<s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br />
	<s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br />
	<s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br />
	<s:property value="#attr.a1"/><br />
	<s:property value="#attr.s1"/><br />
	<s:property value="#attr.r1"/><br />
	<s:debug></s:debug>
	<br />






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值