May 15, 2021 Struts2
Serial number | Objects and descriptions |
---|---|
1 |
Temporary object
In practice, there are various temporary objects created during page execution. F or example, the current iterative value of the JSP label loop collection. |
2 |
The Model object
If you use the Model object in a struts application, the current Model object is placed before the action on the value stack. |
3 |
Action object
This refers to the current action object being executed. |
4 |
The named object
The Sse Objects include The S Application, #session, #Request, #attr, and #parameters, and the referenced servlet scope. |
The value stack can be accessed by providing labels for JSP, Velocity, or Freemarker. I n a separate section, we'll learn about the various labels used to get and set the struts2 value stack. Y ou can get the value stack object in action, as follows:
ActionContext.getContext().getValueStack()
Once you have a value stack object, you can manipulate it using the following methods:
Serial number | Value stack methods and instructions |
---|---|
1 |
Object findValue(String expr)
Find values by evaluating the given expression on the value stack in the default search order. |
2 |
CompoundRoot getRoot()
Gets The CompoundRoot that pushes the object into the value stack. |
3 |
Object peek()
Gets the object at the top of the value stack without changing the value stack. |
4 |
Object pop()
Gets the object at the top of the value stack and removes it from the value stack. |
5 |
void push(Object o)
Place the object at the top of the value stack. |
6 |
void set(String key,Object o)
Use a given key to set an object on the value stack so that it can pass through the ,... S earch. |
7 |
void setDefaultType(Class defaultType)
Sets the default type to convert when you get the value. |
8 |
void setValue(String expr,Object value)
Try setting properties on the bean of the value stack using an expression given by the default search order. |
9 |
int size()
Gets the number of objects in the value stack. |
OGNL (Object-Graph Navigation Language, object diagram navigation language) is a powerful expression language for referencing and operating data on the value stack, as well as for data transfer and type conversion.
Application - Application scope variable
Session - Session scope variable
Root/Value Stack - All action variables are stored here
Request - Request scope variables
Argument - Request parameters
Properties - Properties stored in pages, requests, sessions, and application scopes
It is important to understand that Action objects are always available in the value stack, so if your Action objects have x and y properties, you can use them at any time.
Objects in ActionContext are referenced using the hashtag, but objects in the value stack can be
referenced directly, for example, if the employee
is a property of the action class, you can refer to them as follows:
<s:property value="name"/>
Alternative
<s:property value="#name"/>
If you have a property called "login" in the session, you can retrieve it as follows:
<s:property value="#session.login"/>
OGNL also supports processing collections - namely Map, List, and Set. F or example, to display a down-and-down list of colors, you can do the following:
<s:select name="color" list="{'red','yellow','green'}" />
OGNL expressions intelligently interpret "red," "yellow," and "green" as colors, and build a list based on this.
Let's refer to the action class used below to access the value stack, and then ie. T he JSP view page is set to use OGNL to access several keys.
package cn.w3cschool.struts2; 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 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; } }
In fact, Struts 2 adds action to the top of the value stack when executed. S o, usually the way to place something on the value stack is to add the getters/setters value to the Action class, and then access the values using the .lt;s:property> label. W e've shown earlier how ActionContext and the value stack work in struts.
Create the following jsp file HelloWorld .jsp in your Eclipse project's WebContent folder, and if action returns to success, the view will be displayed:
<%@ 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>
We also need to create an index file in the Webcontent .jsp, which reads as follows:
<%@ 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>
Here's what the struts .xml file is about:
<?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="cn.w3cschool.struts2.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
Here's .xml the web file:
<?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>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> </web-app>
Right-click on the project name, and then click "Export" and "WAR File" to create a WAR file. T he WAR file is then deployed in Tomcat's webapps directory. F inally, start the Tomcat server and try to access the URL http://localhost:8080/HelloWorldStruts2/index.jsp. T he following interface will be displayed:
Now enter any word in a given text box, and then click the Say Hello button to perform the defined action. I f you look at the generated logs, you can see the following text at the bottom:
Size of the valueStack: 3
This means that it will show any values you enter and the key1 and key2 values we put on the value stack.