ContextMap存取数据
1 往ContextMap存入数据
1.1 往ContextMap存入数据
package com.yiidian.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author 一点教程(yiidian.com)
*/
public class Demo1Action extends ActionSupport {
@Override
public String execute() throws Exception {
// 获取ActionContext
ActionContext context = ActionContext.getContext();
// 1.存放ContextMap数据
context.put("hello", "一点教程网-技术创造梦想,专注IT教程");
// 2.往ContextMap的request存入数据
HttpServletRequest request = (HttpServletRequest) context.get(StrutsStatics.HTTP_REQUEST);
request.setAttribute("rerquestMap", "requestMap的数据");
// 3.往ContextMap的session存入数据
context.getSession().put("sessionMap", "sessionMap的数据");
// 4.往ContextMap的application存入数据
context.getApplication().put("applicationMap", "applicationMap的数据");
return SUCCESS;
}
}
1.2 struts.xml配置Action
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="base" extends="struts-default" namespace="/">
<action name="demo1" class="com.yiidian.action.Demo1Action">
<result>/succ.jsp</result>
</action>
</package>
</struts>
1.3 使用debug标签查看数据
2 从ContextMap取出数据
2.1 取出ContextMap数据
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>成功页面</title>
</head>
<body>
<s:debug></s:debug>
<hr/>
<%--取出contextMap的数据 --%>
<!-- 取出ContextMap对象的数据-->
<s:property value="#hello" />
<br />
<!-- 取出ContextMap的request属性-->
<s:property value="#request.rerquestMap" />
<br />
<!-- 取出ContextMap的session属性 -->
<s:property value="#session.sessionMap" />
<br />
<!-- 取出ContextMap的application属性 -->
<s:property value="#application.applicationMap" />
<br />
<hr />
<!-- 通过ContextMap的attr属性,也可以取出域对象的数据 -->
<s:property value="#attr.rerquestMap" />
<br />
<s:property value="#attr.sessionMap" />
<br />
<s:property value="#attr.applicationMap" />
<br />
</body>
</html>
2.2 运行效果
热门文章
优秀文章