Valuestack in struts2 pdf download.
In struts 2 ValueStack represents a stack that contains the references of application specific objects. When a request come an object of ValueStack is created first and after it object of Action, Model or any other java resources are created. The references of all these objects are maintained in ValueStack.
Note: The objects in the ValueStack are available to the response on UI pages. ValueStack uses OGNL for this process.
Main parts of ValueStack:
- Object Stack: It is used to contain the references of the Objects.
- Context Map: It contains the maps like Request Parameters, Request Attributes, Session Attributes and Application Attributes.
Commonly used methods of ValueStack interface:
1. findValue(String expr):Find a value by evaluating the specified expression.
Syntax:
public Object findValue(String expr)
2. findString(String expr):Find a string by evaluating the specified expression.
Syntax:
public String findString(String expr)
3. peek(): It gives the object which is on the top of the stack and not remove it from the stack.
Syntax:
public Object peek()
4. pop(): It gives the object which is on the top of the stack and remove it from the stack.
Syntax:
public Object pop()
5. push(Object o):Put the specified object on the top of the stack.
Syntax:
public void push(Object o)
6. set(String key, Object value):Sets an object on the stack with the specified key. It can retrieve using findValue(key).
Syntax:
public void set(String key, Object value)
7. size(): It gives the no. of objects in the stack.
Syntax:
public int size()
Example:
login.jsp
<%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <title>Struts 2 ValueStack example</title> </head> <body> <h3>This is a ValueStack example.</h3> <s:form action="welcome"> <s:textfield name="userName" label="UserName" /> <s:submit value="Hello" align="center"/> </s:form> </body> </html> |
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng. filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app> |
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="user" extends="struts-default"> <action name="welcome" class="com.w3schools.action.Login"> <result name="success">/welcome.jsp</result> </action> </package> </struts> |
Login.java
import java.util.HashMap; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.util.ValueStack; /** * This class is used as an action class. * @author w3schools */ public class Login { //data members private String userName; //business logic public String execute(){ ValueStack stack = ActionContext.getContext().getValueStack(); Map<String, Object> context = new HashMap<String, Object>(); context.put("value1", new String("First value")); context.put("value2", new String("Second value")); stack.push(context); return "success"; } //getter setters public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } } |
welcome.jsp
<%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <title>Struts 2 ValueStack example</title> </head> <body> <h3>This is a ValueStack example.</h3> <strong><em>UserName: <s:property value="userName" /></em></strong> <strong><em>Value1: <s:property value="value1" /></em></strong> <strong><em>Value2: <s:property value="value2" /></em></strong> </body> </html> |
Output:
Enter username.
Click on Hello button.
Download this example.
Next Topic: Interceptors in struts 2 with example.
Previous Topic: OGNL in struts 2 with example.