码迷,mamicode.com
首页 > Windows程序 > 详细

struts2学习笔记(2)---Action中訪问ServletAPI获取Map类型的Servlet元素

时间:2016-03-12 10:25:29      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

源码:

strust.xml

<?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>
	<constant name="struts.action.extension" value="action,do," />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />


	<package name="default" namespace="/" extends="struts-default">
		<default-action-ref name="default" />

		<action name="test1" class="org.Test.action.TestAction1"
			method="test">
			<result>/success.jsp</result>
		</action>
		
		<action name="test2" class="org.Test.action.TestAction2"
			method="test">
			<result>/success.jsp</result>
		</action>

		<action name="default">
			<result>/index.jsp</result>
		</action>
	</package>
</struts>


success.jsp

<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>成功</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">

</head>

<body>
	${requestScope.name}
	${sessionScope.name}
	${applicationScope.name}
	<br>
</body>
</html></span>



方法一:通过ActionContext来訪问

TestAction1.java

package org.Test.action;

import java.util.Map;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class TestAction1 extends ActionSupport {  
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	
	
	public TestAction1() {
		this.request = (Map)ActionContext.getContext().get("request");
		this.session = (Map)ActionContext.getContext().getSession();
		this.application = (Map)ActionContext.getContext().getApplication();
	}

	public String test() {
		System.out.println("TestAction1:test");
		this.request.put("name", "张三");
		this.session.put("name", "李四");
		this.application.put("name", "王五");
		return SUCCESS;
	}

}


执行结果:

技术分享

技术分享


说明:

1.TestAction1的

类中:定义Map类型的request,session,application对象

构造方法中:通过ActionContext获取这三个对象

test()方法中:用put方法设置属性

2.success.jsp:通过EL语言查看request,session,application对象的name属性的值



方法二:IOC(控制反转)。通过实现接口RequestAware,SessionAware, ApplicationAware来訪问

TestAction2.java

package org.Test.action;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction2 extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware {
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	
	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public String test() {
		System.out.println("TestAction2:test");
		this.request.put("name", "小明");
		this.session.put("name", "小华");
		this.application.put("name", "小王");
		return SUCCESS;
	}

}

执行结果:

技术分享技术分享


说明:

1.把三个set方法传过来的request,session.application值,赋给我们的request,session.application对象。即通过调用set方法来实例化这三个对象,而TestAction2本身并没有对三个对象进行实例化

struts2学习笔记(2)---Action中訪问ServletAPI获取Map类型的Servlet元素

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/5267886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!