ValueStack值栈存取数据

1 往ValueStack存入数据

package com.yiidian.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author 一点教程(yiidian.com)
 */
public class Demo2Action extends ActionSupport {
	@Override
	public String execute() throws Exception {
		// 往值栈保存数据
		ActionContext ac = ActionContext.getContext();
		ValueStack vs = ac.getValueStack();
		// 1.字符串
		vs.push("小苍");
		// 2.对象
		vs.push(new User("小泽", "女"));
		return SUCCESS;
	}

}

User类:

package com.yiidian.action;
/**
 * @author 一点教程(yiidian.com)
 */
public class User {
	private String name;
	private String gender;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public User(String name, String gender) {
		super();
		this.name = name;
		this.gender = gender;
	}
	public User() {
		super();
	}
	
}

2 struts.xml配置Action

<action name="demo2" class="com.yiidian.action.Demo2Action">
	<result>/succ.jsp</result>
</action>

3 使用debug标签查看数据

4 在JSP页面取出ValueStack数据

注意:获取值栈不需要带#号

<%@ 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/>
	
	<%--
 		获取值栈不需要带#号
 		获取值栈的规则:
 			默认情况下,都是从栈顶元素往后查找,一直查找到最后一个元素为止
 	 --%>
 	<s:property value="[0].top"/><br/>
 	<s:property value="[1].top"/>
 	
 	<hr/>
 	<s:property value="name"/> - <s:property value="[0].top.gender"/>
</body>
</html>

效果为:

 

源码下载:https://pan.baidu.com/s/13WrVzRgr9dQmH4DZdbmHoA

热门文章

优秀文章