您的位置:首页 > 移动开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐