Struts2值栈的相关操作

来源:互联网 发布:java微商城源码下载 编辑:程序博客网 时间:2024/05/21 08:02
import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.util.CompoundRoot;import com.opensymphony.xwork2.util.ValueStack;public class ValueStackAction extends ActionSupport{public String testValueStack(){ValueStack valueStack = ActionContext.getContext().getValueStack();ValueStack valueStack2 = ServletActionContext.getContext().getValueStack();ValueStack valueStack3 = (ValueStack)ServletActionContext.getRequest().getAttribute("struts.valueStack");System.out.println(valueStack);System.out.println(valueStack2);System.out.println(valueStack3);return "";}/* * 把数据放入对象栈中的第一种方式 */public String addDataToObjectStack_1(){ValueStack valueStack = ActionContext.getContext().getValueStack();/** * 把字符串添加到了CompoundRoot的第一个位置,我们把第一个位置称为对象栈的栈顶 */valueStack.push("aaaaa");return "";}/* * 把数据放入对象栈中的第二种方式 */public String addDataToObjectStack_2(){ValueStack valueStack = ActionContext.getContext().getValueStack();/** * 把字符串添加到了对象栈中 */valueStack.getRoot().add("aaaa");return "";}/* * 把数据放入对象栈中的第三种方式 */public String addDataToObjectStack_3(){ValueStack valueStack = ActionContext.getContext().getValueStack();/** * 把字符串添加到了对象栈中 *    把一个map放入到了对象栈的栈顶,"aaa"作为key,"asfd"作为value */ActionContext.getContext().getValueStack().set("aaa", "asfd");return "";}/** * 从对象栈中把数据提取出来的方式 *    第一种方式 */public String addDataFromObjectStack_1(){ValueStack valueStack = ActionContext.getContext().getValueStack();/** * 提取栈顶的元素 */ActionContext.getContext().getValueStack().getRoot().get(0);return "";}/** * 从对象栈中把数据提取出来的方式 *    第二种方式 */public String addDataFromObjectStack_2(){ValueStack valueStack = ActionContext.getContext().getValueStack();/** * 提取栈顶的元素 */ActionContext.getContext().getValueStack().peek();return "";}/* * 把对象栈的栈顶的元素移除 */public String removeDataFromObjectStack_1(){ValueStack valueStack = ActionContext.getContext().getValueStack();/** * 移除栈顶的元素 */ActionContext.getContext().getValueStack().getRoot().remove(0);return "";}/* * 把对象栈的栈顶的元素移除 */public String removeDataFromObjectStack_2(){ValueStack valueStack = ActionContext.getContext().getValueStack();/** * 移除栈顶的元素 */ActionContext.getContext().getValueStack().pop();return "";}/** * 把一个key,value键值对放入到request域中 */public String putObjectToRequest(){ServletActionContext.getRequest().setAttribute("aaa", "aaa");ValueStack valueStack = ActionContext.getContext().getValueStack();return "";}/** * 把一个key,value键值对放入到application域中 */public String putObjectToApplication(){ServletActionContext.getServletContext().setAttribute("aaaa", "aaaa");ValueStack valueStack = ActionContext.getContext().getValueStack();return "";}/** * 把一个key,value直接放在map栈中 */public String putDataToMapStack_1(){ValueStack valueStack = ActionContext.getContext().getValueStack();ActionContext.getContext().put("aaa", "aaaa");return "";}}

1 0
原创粉丝点击