Struts2中解耦的方式访问appliactionContext对象
2016-11-16 20:45
441 查看
1:jsp 超链接
<%@ page language="java" contentType="text/html;charset=UTF-8" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'userList.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<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">
</head>
<body>
<a href="<%=basePath%>/TestActionContext.action?name=解耦">Test ActionContext</a>
</body>
</html>
2:Action
package com.study;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
/**
* 与Servlet API解耦合的测试类
* @author Administrator
*
*/
public class TestAction {
public String execute()
{
//获取application对应的Map,并向其中添加一个属性
//ActionContext是Action的上下文对象,可以从中获取到所有Action需要的对象
ActionContext actionContext=ActionContext.getContext();
Map<String,Object> applicationMap=actionContext.getApplication();
applicationMap.put("aplicationKey", "applicationValue");
//session
//request
//获取请求参数对应的Map,并获取指定的参数值
return "success";
}
}
3:jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'userList.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<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">
</head>
<body>
application:${applicationScope.aplicationKey}
</body>
</html>
<%@ page language="java" contentType="text/html;charset=UTF-8" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'userList.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<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">
</head>
<body>
<a href="<%=basePath%>/TestActionContext.action?name=解耦">Test ActionContext</a>
</body>
</html>
2:Action
package com.study;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
/**
* 与Servlet API解耦合的测试类
* @author Administrator
*
*/
public class TestAction {
public String execute()
{
//获取application对应的Map,并向其中添加一个属性
//ActionContext是Action的上下文对象,可以从中获取到所有Action需要的对象
ActionContext actionContext=ActionContext.getContext();
Map<String,Object> applicationMap=actionContext.getApplication();
applicationMap.put("aplicationKey", "applicationValue");
//session
//request
//获取请求参数对应的Map,并获取指定的参数值
return "success";
}
}
3:jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'userList.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<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">
</head>
<body>
application:${applicationScope.aplicationKey}
</body>
</html>
相关文章推荐
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- Struts2_008_Servlet API耦合的访问方式值之ServletActionContext
- S5.1_Struts2_AccessWebElements action访问web对象的4种方式
- Struts2通过ActionContext解耦的方式获取WEB资源
- Struts2中访问Servlet中的API(ActionContext,ServletActionContext)两种方式
- Struts2 Action访问Web对象的四种方式
- struts2中ServletActionContext与ActionContext区别及获取Servlet对象方法
- struts2学习之访问Servlet对象的两种实现方式
- JAVA_WEB Struts2学习:Struts2解耦、耦合方式获取request、session、aplication对象
- struts2的Action访问Servlet的几种方式和全局result
- 解决Struts2的Action通过get方式访问乱码的问题
- STRUTS2 ACTION 访问WEB元素的四种方式
- Struts2的Action访问Servlet API的方式
- struts2学习笔记--ActionContext对象
- 在struts2 中通过ActionContext访问Session对象
- struts2中Action访问servlet的两种方式
- Struts2(6):使用ServletActionContext访问Servlet API
- struts2 lesson one: 的action特征,三种创建方法,action接收表单数据的三种访问,读取方式
- Struts2两种解耦访问WEB资源的方式
- struts2 ActionContext获取session对象有时为null