转载出处:https://blog.csdn.net/liliiii/article/details/41311531
在Struts2中,动作类虽然继承ActionSupport类,可以直接写我们自己定义的方法,但是却不能像在Struts1中,对reques/response/application/HttpServletRequest等等一些Web元素进行操作,所以Struts2提供了RequestAware,SessionAware,ApplicationAware/ServletRequestAware....接口.
实现这些接口就可以对其进行想要的操作了.
-
package actions;
-
-
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.ActionSupport;
-
-
import com.opensymphony.xwork2.ModelDriven;
-
-
import enetitys.Student;
-
-
public class StudentAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<Student>{
-
-
private Student student= new Student();
-
-
-
-
public Student getModel() {
-
-
return student;
-
-
}
-
-
//实现了RequestAware,SessionAware,ApplicationAware接口的类.
-
-
//谁调用执行这个action,谁就来初始化这些值
-
-
private Map<String,Object> request;
-
-
private Map<String,Object> session;
-
-
private Map<String,Object> application;
-
-
-
-
public void setApplication(Map<String, Object> application) {
-
-
this.application=application;
-
-
}
-
-
-
-
public void setRequest(Map<String, Object> request) {
-
-
this.request=request;
-
-
}
-
-
-
-
public void setSession(Map<String, Object> session) {
-
-
this.session=session;
-
-
}
-
-
public String delete(){
-
-
request.put( "list", "把一个集合的数据删掉");
-
-
return "delete";
-
-
}
Action中实现RequestAware,SessionAware,ApplicationAware接口,
实现这些接口,都会有相对应的setXXX()方法.就是说谁来执行这个action中的相应方法,
谁就对这些个对象进行初始化(Spring中的注入).也就是Struts2为我们进行了初始化,所以这三个值都不需要自己初始化.
Delete.jsp页面中通过el表达式访问request中存放的key为list的值
-
<body>
-
-
${request.list}
-
-
</body>
页面访问的时候:
还有一种方法,但是需要依赖于Struts2.也就是上一篇日志中,访问栈中的Stack Context属性值,
只需要在action中定义相应名称的Map集合,在构造函数或一个什么方法中进行取值就行,在当前action的运行环境中取值:
-
private Map request;
-
-
private Map session;
-
-
private Map application;
-
-
public UserAction(){
-
-
request=(Map)ActionContext.getContext().get( "request");
-
-
session=ActionContext.getContext().getSession();
-
-
application=ActionContext.getContext().getApplication();
-
-
}
-
******************************************************************
Struts2中访问web元素的四种方式及前台jsp页面获取后台值的方式
四种方式:
1. 通过ActionContext来访问request,session,application对象
2. 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象
3. 通过ServletActionContext来访问request,session,application对象
4. 通过实现ServletRequestAware接口来访问request,session,application对象
演示代码:
方式一:
-
/**
-
* 通过ActionContext来访问request,session,application对象
-
* @author 健
-
*/
-
public class UserAction1 extends ActionSupport{
-
/**
-
* 序列化
-
*/
-
private static final long serialVersionUID = 1L;
-
/**
-
* request
-
*/
-
private Map <String,Object>request;
-
/**
-
* response
-
*/
-
private Map <String,Object>session;
-
/**
-
* application
-
*/
-
private Map <String,Object>application;
-
/**
-
* 添加用户
-
* @return 用户是否添加成功
-
*/
-
"unchecked")(
-
-
public String execute(){
-
System.out.println( "通过ActionContext来访问request,session,application对象");
-
// 初始化
-
request = (Map<String,Object>)ActionContext.getContext().get( "request");
-
session = ActionContext.getContext().getSession();
-
application = ActionContext.getContext().getApplication();
-
// 赋值
-
request.put( "requestKey", "requestValue");
-
session.put( "sessionKey", "sessionValue");
-
application.put( "applicationKey", "applicationValue");
-
return "success";
-
}
-
}
方式二:
-
/**
-
* 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象
-
* @author 健
-
*/
-
public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
-
/**
-
* 序列化
-
*/
-
private static final long serialVersionUID = 1L;
-
/**
-
* request
-
*/
-
private Map <String,Object>request;
-
/**
-
* response
-
*/
-
private Map <String,Object>session;
-
/**
-
* application
-
*/
-
private Map <String,Object>application;
-
/**
-
* 控制器
-
*/
-
"unchecked")(
-
-
public String execute(){
-
System.out.println( "通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象");
-
// 赋值
-
request.put( "requestKey", "requestValue");
-
session.put( "sessionKey", "sessionValue");
-
application.put( "applicationKey", "applicationValue");
-
return "success";
-
}
-
/*
-
* 实现RequestAware中的方法
-
*/
-
-
public void setRequest(Map<String, Object> request) {
-
this.request = request;
-
}
-
/*
-
* 实现ApplicationAware中的方法
-
*/
-
-
public void setApplication(Map<String, Object> application) {
-
this.application = application;
-
}
-
/*
-
* 实现SessionAware中的方法
-
*/
-
-
public void setSession(Map<String, Object> session) {
-
this.session = session;
-
}
-
}
方式三:
-
+ /**
-
* 通过ServletActionContext来访问request,session,application对象
-
* @author 健
-
*/
-
public class UserAction3 extends ActionSupport{
-
/**
-
* 序列化
-
*/
-
private static final long serialVersionUID = 1L;
-
/**
-
* request
-
*/
-
private HttpServletRequest request;
-
/**
-
* response
-
*/
-
private HttpSession session;
-
/**
-
* application
-
*/
-
private ServletContext application;
-
/**
-
* 控制器
-
*/
-
"unchecked")(
-
-
public String execute(){
-
System.out.println( "通过ServletActionContext来访问request,session,application对象");
-
// 初始化
-
request = ServletActionContext.getRequest();
-
session = request.getSession();
-
application = session.getServletContext();
-
// 赋值
-
request.setAttribute( "requestKey", "requestValue");
-
session.setAttribute( "sessionKey", "sessionValue");
-
application.setAttribute( "applicationKey", "applicationValue");
-
return "success";
-
}
-
}
方式四:
-
/**
-
* 通过实现ServletRequestAware接口来访问request,session,application对象
-
* @author 健
-
*/
-
public class UserAction4 extends ActionSupport implements ServletRequestAware{
-
/**
-
* 序列化
-
*/
-
private static final long serialVersionUID = 1L;
-
/**
-
* request
-
*/
-
private HttpServletRequest request;
-
/**
-
* response
-
*/
-
private HttpSession session;
-
/**
-
* application
-
*/
-
private ServletContext application;
-
/**
-
* 控制器
-
*/
-
"unchecked")(
-
-
public String execute(){
-
System.out.println( "通过实现ServletRequestAware接口来访问request,session,application对象");
-
// 赋值
-
request.setAttribute( "requestKey", "requestValue");
-
session.setAttribute( "sessionKey", "sessionValue");
-
application.setAttribute( "applicationKey", "applicationValue");
-
return "success";
-
}
-
/*
-
* 实现ServletRequestAware接口中的方法
-
*/
-
-
public void setServletRequest(HttpServletRequest request) {
-
this.request = request;
-
this.session = request.getSession();
-
this.application = session.getServletContext();
-
}
-
}
上面的action配套的struts.xml及jsp页面
struts.xml
-
<struts>
-
<!-- 配置开发模式:修改不用重启服务器 -->
-
<constant name="struts.devMode" value="true"/>
-
<package name="" namespace="/login" extends="struts-default">
-
<action name="login*" class="com.wj.struts2.action.UserAction{1}">
-
<result name="success">/success.jsp </result>
-
<result name="failure">/failure.jsp </result>
-
</action>
-
</package>
-
</struts>
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@taglib uri="/struts-tags" prefix="s" %>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme() + "://"
-
+ request.getServerName() + ":" + request.getServerPort()
-
+ path + "/";
-
%>
-
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>Struts2_AccessWebElements </title>
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
-
<meta http-equiv="description" content="This is my page">
-
<!--
-
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-
-->
-
<mce:script language="javascript"> <!--
-
function sub(str){
-
document.form1.action = str;
-
document.form1.submit();
-
}
-
-
// --> </mce:script>
-
</head>
-
-
<body>
-
<form name="form1">
-
<div>
-
Struts2中访问web元素的四种方式 <br>
-
方式一: <input type="button" value="submit1" onclick="sub('<%=basePath%>login/login1')"> <br>
-
方式二: <input type="button" value="submit2" onclick="sub('<%=basePath%>login/login2')"> <br>
-
方式三: <input type="button" value="submit3" onclick="sub('<%=basePath%>login/login3')"> <br>
-
方式四: <input type="button" value="submit4" onclick="sub('<%=basePath%>login/login4')"> <br>
-
</div>
-
</form>
-
</body>
-
</html>
-
<%@ page language= "java" import= "java.util.*" pageEncoding= "UTF-8"%>
-
<% "/struts-tags" prefix= "s" %>uri=
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme() + "://"
-
+ request.getServerName() + ":" + request.getServerPort()
-
+ path + "/";
-
%>
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<base href= "<%=basePath%>">
-
<title>Struts2_AccessWebElements</title>
-
<meta http-equiv= "pragma" content= "no-cache">
-
<meta http-equiv= "cache-control" content= "no-cache">
-
<meta http-equiv= "expires" content= "0">
-
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3">
-
<meta http-equiv= "description" content= "This is my page">
-
<!--
-
<link rel= "stylesheet" type= "text/css" href= "styles.css" mce_href= "styles.css">
-
-->
-
</head>
-
-
<body>
-
requestKey---<s:property value= "#request.requestKey"/>|<%=request.getAttribute( "requestKey")%><br>
-
sessionKey---<s:property value= "#session.sessionKey"/>|<%=session.getAttribute( "sessionKey")%><br>
-
applicationKey---<s:property value= "#application.applicationKey"/>|<%=application.getAttribute( "applicationKey")%><br>
-
--------------------------------------------
-
<s:debug></s:debug>
-
</body>
-
</html>
前台jsp页面获取后台值的方式
<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%>
<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%>
<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%>