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

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