3.Struts2_通过ActionContext获取Web资源

1.在Action中访问web资源:

1)什么是web资源?

HttpServletRequest,HttpSession,ServletContext等原生的Servlet  API。

2)为什么获取web资源?

b/s构架的应用的Controller中,必然需要访问web资源:向域对象中读写属性,读写Cookie,获取realPath...

3)如何访问?

 和Servlet API解耦的方式:只能访问有限的Servlet API对象,且只能访问其有限的方法。(读取请求参数,读写域对象属性,使session失效)

>使用ActionContext 

>实现XxxAware接口

耦合方式:访问更多Servlet API ,可调用原生方法。

>使用ServletActionContext

>实现ServletXxxAware 接口

下面使用ActionContext来获取:

Struts2对HttpServletRequest,HttpSession,ServletContext进行了封装,构造了3个Map对象代替原生。在Struts2

中需要操作Map对象来进行操作。


</pre><pre name="code" class="java"><span style="font-weight: normal;">package com.hcx.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

public class TestActionContextAction {

	public String execute(){
		//获取ActionContext对象,可获取Action一切信息
		 ActionContext actionContext = ActionContext.getContext();
		//1.获取application对应的Map,并向其中添加一个属性
		Map<String,Object> applicationMap =  actionContext.getApplication();
		applicationMap.put("applicationKey", "applicationValue");
		//获取属性
		Object date = applicationMap.get("date");
		System.out.println("date:"+date);
		//2、session
		Map<String,Object> sessionMap = actionContext.getSession();
		sessionMap.put("sessionKey", "sessionValue");
		SessionMap map=(SessionMap) sessionMap;
		map.invalidate();
		//3. request
		//ActionContext中没有提供getRequest的方法,需要手工调用get()方法,传入request字符串
		Map<String,Object> requestMap= (Map<String, Object>) actionContext.get("request");
		requestMap.put("requestKey", "requestValue");
		//4.获取请求参数对应的Map,并获取指定的参数值
		//key:请求参数名字
		//value:骑牛参数对应值的字符串数组
		//注意:该map只能读,不能写入数据,如果写入不报错,单也不起作用
		Map<String,Object> parameters=actionContext.getParameters();
		System.out.println(((String[])parameters.get("name"))[0]);
		
		parameters.put("age", "100");
		return "success";
	}
}</span>



<span style="font-weight: normal;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
		<package name="default" namespace="/" extends="struts-default">
		<action name="TestActionContext" 
			class="com.hcx.action.TestActionContextAction"
		>
		<result>/test-actionContext.jsp</result>
		</action>
		</package>
	
</struts>
</span>


<span style="font-weight: normal;"><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'test-actionContext.jsp' starting page</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">
	-->

  </head>
  
  <body>
   	<h4>Test ActionContext Page</h4>
   	application: ${applicationScope.applicationKey }
   	<br><br>
   	session: ${sessionScope.sessionKey }
   		<br><br>
   	request: ${requestScope.requestKey }
  </body>
</html></span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值