标签:
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>
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属性的值
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