0
点赞
收藏
分享

微信扫一扫

valueStack方法(存取)


setValue方法

该方法在对象存在时 会重置,不存在时会生成新对象

  1. 提供getset方法
  2. 向valueStack存储数:valueStack.setValue(“username”,“LIANG”);
    向contextMap中存储数据:valueStack.setValue("#username",“LIANG”);

package org.ccit.com.web.action;

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

/**
* @program: struts2_06
* @description
* @author: LIANG
* @create: 2021-02-04 15:31
**/
public class valueStack extends ActionSupport {
//需要提供getset方法
private String username;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String execute() throws Exception {
//获取值栈
ValueStack valueStack = ActionContext.getContext().getValueStack();
//向valueStack存储数据
valueStack.setValue("username","LIANG");
//向contextMap中存储数据
valueStack.setValue("#username","LIANG");
return SUCCESS;
}
}

使用<s:debug>查看

valueStack方法(存取)_取值


valueStack方法(存取)_struts2_02

set方法

//set方法 以hashMap对象存入valueStack
valueStack.set("Student",new Student("LIANG","123456"));
//以封装对象压栈
valueStack.push(new Student("LIANG","123456"));

valueStack方法(存取)_struts2_03


valueStack方法(存取)_struts2_04

findValue方法

<%
Object obj = ActionContext.getContext().getValueStack().findValue("user.username");
out.print(obj);
%>

jsp页面显示

从valueStack中取值

<s:property value="username"></s:property>
<s:property value="[1].username"></s:property>
<s:property value="Student.username"></s:property>

两个username 用下表取值

由set方法存入valueStack中的数据 使用key.属性取值

valueStack方法(存取)_struts2_05


从contextMap中取值 加#号 如果没有对应数据 会从valueStack中找数据

<s:property value="#username"></s:property>

注:struts中EL取值顺序的改变

默认情况下EL查找顺序
pageContext(page) > request > session > servletContext(application)
struts中EL表达式的查找顺序
page > request > valueStack > contextMap > seeeion > application


举报

相关推荐

0 条评论