配置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>
<package name="stack" namespace="/stack" extends="struts-default">
<action name="stack_*" class="star.july.c_valuestack.StackAction" method="{1}">
<result name="success">/index.jsp</result>
<result name="input">/add.jsp</result>
</action>
</package>
</struts>
StackAction:
package star.july.c_valuestack;
import java.util.Map;
import star.july.b_validation.Student;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class StackAction extends ActionSupport{
Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String stack(){
String name = student.getName();
ActionContext ac = ActionContext.getContext();
//使用对象栈存储对象
//获取值栈,用list的栈方式存取
ValueStack vs = ac.getValueStack();
//将对象放入栈中,压栈
vs.push(student);
//弹栈
// vs.pop();
//使用映射栈存取对象
//添加自己的值和栈
Map map = ac.getContextMap();
map.put("map", "自定义");
//获取request
Map rp = (Map)ac.get("request");
rp.put("rp", "请求");
//获取session
Map sp = ac.getSession();
sp.put("sp", "会话");
//获取application并赋值
Map ap = ac.getApplication();
ap.put("ap", "应用");
System.out.println(student);
return SUCCESS;
}
}
输入值:
<body>
<s:fielderror></s:fielderror>
<form action="<c:url value='/stack/stack_stack'/>" method="post">
用户名:<input type="text" name="student.name"><br>
<input type="submit" value="提交"/>
</form>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 'index.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>
<!-- 对象栈的取方式,用[] -->
<!-- 取出方式,从下表开始一直取到结束为止 -->
对象栈的取方式,用[]
<s:property value="[1]"/><br>
<s:property value="[0]"/><br><br><br>
<!-- 映射栈的取值方式,前面加# -->
映射栈的取值方式,前面加#<br>
<s:property value="#map"/><br>
<s:property value="#request.rp"/><br>
<s:property value="#session.sp"/><br>
<s:property value="#application.ap"/>
<!-- 调试值栈,看值 -->
<s:debug></s:debug>
</body>
</html>