转自http://blog.csdn.net/developer_java/archive/2010/09/14/5882976.aspx
Struts2中访问web元素的四种方式(访问requst、session、application)
四种方式:
1. 通过ActionContext来访问request,session,application对象
2. 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象
3. 通过ServletActionContext来访问request,session,application对象
4. 通过实现ServletRequestAware接口来访问request,session,application对象
演示代码:
方式一:
view plaincopy to clipboardprint?
public class UserAction1 extends ActionSupport{
private static final long serialVersionUID = 1L;
private Map <String,Object>request;
private Map <String,Object>session;
private Map <String,Object>application;
@SuppressWarnings("unchecked")
@Override
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";
}
}
public class UserAction1 extends ActionSupport{
private static final long serialVersionUID = 1L;
private Map <String,Object>request;
private Map <String,Object>session;
private Map <String,Object>application;
@SuppressWarnings("unchecked")
@Override
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";
}
}
方式二:
view plaincopy to clipboardprint?
public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private static final long serialVersionUID = 1L;
private Map <String,Object>request;
private Map <String,Object>session;
private Map <String,Object>application;
@SuppressWarnings("unchecked")
@Override
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";
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private static final long serialVersionUID = 1L;
private Map <String,Object>request;
private Map <String,Object>session;
private Map <String,Object>application;
@SuppressWarnings("unchecked")
@Override
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";
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
方式三:
view plaincopy to clipboardprint?
public class UserAction3 extends ActionSupport{
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@SuppressWarnings("unchecked")
@Override
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";
}
}
public class UserAction3 extends ActionSupport{
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@SuppressWarnings("unchecked")
@Override
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";
}
}
方式四:
view plaincopy to clipboardprint?
public class UserAction4 extends ActionSupport implements ServletRequestAware{
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println("通过实现ServletRequestAware接口来访问request,session,application对象");
// 赋值
request.setAttribute("requestKey", "requestValue");
session.setAttribute("sessionKey", "sessionValue");
application.setAttribute("applicationKey", "applicationValue");
return "success";
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}
public class UserAction4 extends ActionSupport implements ServletRequestAware{
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println("通过实现ServletRequestAware接口来访问request,session,application对象");
// 赋值
request.setAttribute("requestKey", "requestValue");
session.setAttribute("sessionKey", "sessionValue");
application.setAttribute("applicationKey", "applicationValue");
return "success";
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}
上面的action配套的struts.xml及jsp页面
struts.xml
view plaincopy to clipboardprint?
<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>
<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>
index.jsp
view plaincopy to clipboardprint?
<%@ 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 + "/";
%>
<!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">
-->
<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" οnclick="sub('<%=basePath%>login/login1')"><br>
方式二:<input type="button" value="submit2" οnclick="sub('<%=basePath%>login/login2')"><br>
方式三:<input type="button" value="submit3" οnclick="sub('<%=basePath%>login/login3')"><br>
方式四:<input type="button" value="submit4" οnclick="sub('<%=basePath%>login/login4')"><br>
</div>
</form>
</body>
</html>
<%@ 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 + "/";
%>
<!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">
-->
<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" οnclick="sub('<%=basePath%>login/login1')"><br>
方式二:<input type="button" value="submit2" οnclick="sub('<%=basePath%>login/login2')"><br>
方式三:<input type="button" value="submit3" οnclick="sub('<%=basePath%>login/login3')"><br>
方式四:<input type="button" value="submit4" οnclick="sub('<%=basePath%>login/login4')"><br>
</div>
</form>
</body>
</html>
success.jsp
view plaincopy to clipboardprint?
<%@ 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 + "/";
%>
<!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>
<%@ 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 + "/";
%>
<!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")%>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/developer_java/archive/2010/09/14/5882976.aspx