SN | Objects & Description |
---|---|
1 | Temporary Objects There are various temporary objects which are created during execution of a page. For example the current iteration value for a collection being looped over in a JSP tag. |
2 | The Model Object If you are using model objects in your struts application, the current model object is placed before the action on the value stack |
3 | The Action Object This will be the current action object which is being executed. |
4 | Named Objects These objects include #application, #session, #request, #attr and #parameters and refer to the corresponding servlet scopes |
ActionContext.getContext().getValueStack()
SN | ValueStack Methods & Description |
---|---|
1 | Object findValue(String expr) Find a value by evaluating the given expression against the stack in the default search order. |
2 | CompoundRoot getRoot() Get the CompoundRoot which holds the objects pushed onto the stack. |
3 | Object peek() Get the object on the top of the stack without changing the stack. |
4 | Object pop() Get the object on the top of the stack and remove it from the stack. |
5 | void push(Object o) Put this object onto the top of the stack. |
6 | void set(String key, Object o) Sets an object on the stack with the given key so it is retrievable by findValue(key,...) |
7 | void setDefaultType(Class defaultType) Sets the default type to convert to if no type is provided when getting a value. |
8 | void setValue(String expr, Object value) Attempts to set a property on a bean in the stack with the given expression using the default search order. |
9 | int size() Get the number of objects in the stack. |
<s:property value="name"/>
<s:property value="#name"/>If you have an attribute in session called "login" you can retrieve it as follows:
<s:property value="#session.login"/>OGNL also supports dealing with collections - namely Map, List and Set. For example to display a dropdown list of colors, you could do:
<s:property name="color" list="{'red','yellow','green'}"/>
package com.dineshonjava.struts2.action; import java.util.HashMap; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ValueStack; /** * @author Dinesh Rajput * */ public class HelloWorldAction extends ActionSupport { private static final long serialVersionUID = 4956157388836635122L; 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")); stack.push(context); System.out.println("Size of the valueStack: " + stack.size()); return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }Actually, Struts 2 adds your action to the top of the valueStack when executed. So, the usual way to put stuff on the Value Stack is to add getters/setters for the values to your Action class and then use <s:property> tag to access the values. But I'm showing you how exactly ActionContext and ValueStack work in struts.
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Hello World</title> </head> <body> Entered value : <s:property value="name"/><br/> Value of key 1 : <s:property value="key1" /><br/> Value of key 2 : <s:property value="key2" /> <br/> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Hello World</title> </head> <body> <h1>Hello World From Struts2</h1> <form action="hello"> <label for="name">Please enter your name</label><br/> <input type="text" name="name"/> <input type="submit" value="Say Hello"/> </form> </body> </html>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.dineshonjava.struts2.action.HelloWorldAction" method="execute"> <result name="success">/success.jsp</result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts2ValueStack</display-name> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory. Finally, start Tomcat server and try to access
Labels: struts2