Struts2中通过实现Aware接口解耦servlet API,可以多个方法共享一个application,session,request,parameter对象
2016-11-16 22:16
429 查看
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=解耦对象&name=解耦参数&user=admin">Test ActionContext</a><br/>
<a href="<%=basePath%>TestAwareAction.action?name1=解耦对象&name1=解耦参数&user1=admin">Test ActionContext</a>
<%
application.setAttribute("date", new Date());
session.setAttribute("date1", new Date());
request.setAttribute("date2", new Date());
%>
</body>
</html>
2:Action
package com.study;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
public class TestAwareAction implements ApplicationAware{
private Map<String,Object> application;
public String execute()
{
//1:向application中添加一个属性,applicationKey2->applicationValue2
application.put("applicationKey2","applicationValue2");
System.out.print(application.get("date"));
//2:从application中读取一个属性date,并打印
return "success";
}
@Override
public void setApplication(Map<String, Object> application) {
this.application=application;
}
}
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>
<h4>Test Aware</h4>
application:${applicationScope.applicationKey2}<br/>
session:${sessionScope.sessionKey2}<br/>
request:${requestScope.requestKey2}<br/>
</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=解耦对象&name=解耦参数&user=admin">Test ActionContext</a><br/>
<a href="<%=basePath%>TestAwareAction.action?name1=解耦对象&name1=解耦参数&user1=admin">Test ActionContext</a>
<%
application.setAttribute("date", new Date());
session.setAttribute("date1", new Date());
request.setAttribute("date2", new Date());
%>
</body>
</html>
2:Action
package com.study;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
public class TestAwareAction implements ApplicationAware{
private Map<String,Object> application;
public String execute()
{
//1:向application中添加一个属性,applicationKey2->applicationValue2
application.put("applicationKey2","applicationValue2");
System.out.print(application.get("date"));
//2:从application中读取一个属性date,并打印
return "success";
}
@Override
public void setApplication(Map<String, Object> application) {
this.application=application;
}
}
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>
<h4>Test Aware</h4>
application:${applicationScope.applicationKey2}<br/>
session:${sessionScope.sessionKey2}<br/>
request:${requestScope.requestKey2}<br/>
</body>
</html>
相关文章推荐
- struts2 获取"作用域"对象:Request Session Application 与 Aware 接口的使用
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- struts2 获取"作用域"对象Request Session Application和Aware 接口
- struts2 action中获取request session application的方法
- struts2标签获取parameter,request,session,application中的值方法 value="#request.yearEnd"
- struts2:lesson three:Annotation配置,实现request,session.application的四种方式,Result类型
- 如果一个类通过继承Thread来实现多线程的话,则不适合多个线程共享资源,而通过实现Runnable就可以做到这一点
- struts2 action中获取request session application的方法
- struts2 action中获取request session application的方法
- Struts2的Action中获得request、session和application的几种方法
- struts2 action中获取request session application的方法
- struts2系列十之4中获取使用request,session,application的方法和页面显示的三种方法【后期ognl算第四种】
- 通过接口注入获取Request,session,application
- flex 中通过 RO 调用一个java写的Bean的时候获得IP地址的方法 .也就是获得session 和 request的方法
- 一个类可以实现多个有相同方法的接口
- Struts2异常!!!(原因LoginAction没有实现相应接口:SessionAware)
- Struts2获得Application,Session,Request的四种方法
- Struts2中request,session,application三种请求方法
- Struts2中操作request,session,application的方法
- Struts2的Action中获得request、session和application的几种方法