This is my code below, when I execute it shows me the size 3, but when I pop the object out I am getting only 2 objects.
import java.util.*;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String name;
public String execute() throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
Map<String, Object> context = new HashMap<String, Object>();
context.put("key1", new String("This is key1"));
context.put("key2", new String("This is key2"));
context.put("key3", new String("This is key3"));
stack.push(context);
System.out.println("Size of the valueStack: " + stack.size());
for (int i = 0; i < stack.size(); i++) {
System.out.println(i + ": " + stack.pop().toString());
}
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Please, explain me whether I am doing it wrong?
Wnd I want to know that what are the objects stored in ValueStack and how can I retrieve those objects?
You have mistreated the
contextand a map.First, you got an action
contextandvalueStack.Then you created a map called
contextand pushed it to the stack.Then you have started iterate over the stack, but the stack is a different object that has
contextpushed over.To get your context back from the stack you need to
pop()orpeek()it from thevalueStack. Then you can iterate it as a map.The code: