How to pass parameters from struts.xml to Struts action class
- Details
- Written by Nam Ha Minh
- Last Updated on 31 July 2019   |   Print Email
In this Struts tutorial, I will share with you how to read values of parameters configured in the Struts configuration file from within a Struts action class.
In Struts, the staticParams interceptor enables us configuring parameters for an action class in struts.xml configuration file, for example:
<action name="actionName" class="net.codejava.struts.MyAction"> <param name="param1">Value 1</param> <param name="param2">Value 2</param> <!-- results names go here... --> </action>
As we can see, each <param> element specifies key and value for a parameter - very straightforward. In the action class, there are two ways for reading these parameters: using JavaBean-style methods and using a Map object named params.
1. Reading parameters using JavaBean-style methods:
In this method, we have to declare properties whose names equal to key names of the parameters in struts.xml file. Suppose we specify parameters for an action as follows:
<action name="testMethodParam" class="net.codejava.struts.MethodParamAction"> <param name="location">/UploadFiles</param> <result name="success">/MethodParamResult.jsp</result> </action>
Then write the action class like this:
package net.codejava.struts; import com.opensymphony.xwork2.ActionSupport; public class MethodParamAction extends ActionSupport { private String location; public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public String execute() { System.out.println("location = " + location); return SUCCESS; } }
And here is the result JSP page:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Result</title> </head> <body> <center> <h3>Upload location: ${location}</h3> </center> </body> </html>
Output when running: http://localhost:8080/Struts2Parameters/testMethodParam
This method is suitable when we have just a few parameters for the action.
2. Reading parameters using a Map object
In this method, we have to do the following:
- Declare a property named params of type Map<String, String> - this map will hold all the parameters passed to the action class from the struts.xml file.
- Make the action class implementing the com.opensymphony.xwork2.config.entities.Parameterizable interface and implement its methods: setParams(), getParams() and addParam().
Let’s see an example. Suppose we have an action declared in struts.xml file as follows:
<action name="testMapParam" class="net.codejava.struts.MapParamAction"> <param name="location">/UploadFiles</param> <param name="fileTypes">jpg,png,zip,doc,pdf</param> <param name="maxSize">10240</param> <result name="success">/MapParamResult.jsp</result> </action>
It’s easy to spot the three parameters are: location, fileTypes and maxSize. Now we implement the action class as follows:
package net.codejava.struts; import java.util.HashMap; import java.util.Map; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.config.entities.Parameterizable; public class MapParamAction extends ActionSupport implements Parameterizable { private Map<String, String> params = new HashMap<String, String>(); @Override public void addParam(String key, String value) { this.params.put(key, value); } @Override public Map<String, String> getParams() { return this.params; } @Override public void setParams(Map<String, String> params) { this.params = params; } public String execute() { System.out.println("param1 = " + this.params.get("param1")); System.out.println("param2 = " + this.params.get("param2")); System.out.println("param3 = " + this.params.get("param3")); return SUCCESS; } }
As we can see, reading a parameter’s value is as simple as reading a map element:
String value = params.get("key");
Here is the result JSP page:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Map Param Result</title> </head> <body> <center> <h3>Location = ${params.location}</h3> <h3>File Types = ${params.fileTypes}</h3> <h3>Max Size = ${params.maxSize}</h3> </center> </body> </html>
Output when running: http://localhost:8080/Struts2Parameters/testMapParam
This method looks a bit more complex then the JavaBean-style one, however it would be useful in case we have many parameters, as reading parameters from a map is easier than having many getter and setter methods.
NOTE: The staticParams interceptor is already included in the Strut’s default stack, so if our package extends from the struts-default package, there is no need to include this interceptor explicitly.
Related Struts Tutorials:
- How to configure Struts framework in web.xml
- Reading parameters from web.xml in Struts action class
- Splitting (modularizing) Struts configuration file
- How to access HttpServletRequest and HttpServletResponse within Struts2 action class
Other Struts Tutorials:
- Introduction to Struts 2 framework
- Struts beginner tutorial (Eclipse + Tomcat + XML)
- Struts Beginner Tutorial with Annotations
- Struts beginner tutorial with Convention Plugin (zero-configuration)
- How to handle exceptions in Struts
- Send e-mail with attachments in Struts
- Struts File Upload Tutorial
- Struts - Spring - Hibernate Integration Tutorial
Comments