Struts 2 “param” tag is used to parametrize other tags. However, when you declared the “param” tag, the “parameter value” can be define in two ways :

  1. value” attribute.
  2. Text between the start and end of “param” tag.

For example,

<param name="fruit">Banana</param>  {Case 1}
<param name="fruit" value="Banana"/> {Case 2}
In Struts 2 , both behaves a totally different meaning. In “Case 1″, the value is considered as a java.lang.String object; While “Case 2″, the value is considered as a java.lang.Object object.

The above statement is best illustrated using some examples :

Example 1

Set a “String” value into a bean property via “param” tag, you have to declared like this

<s:bean name="com.mkyong.common.Person" var="personBean">
	<s:param name="nickName">mkyong</s:param>
</s:bean>
Not

<s:bean name="com.mkyong.common.Person" var="personBean">
	<s:param name="nickName" value="mkyong"></s:param>
</s:bean>

If you declared the “String” value inside the “value” attribute, Struts 2 will just ignore it.

Example 2

Set a java.lang.Object object into bean property via “param” tag, declared like this

<s:bean name="com.mkyong.common.Fruit" var="fruitBean">
	<s:param name="fruitName">Banana</s:param>
</s:bean>
 
<s:bean name="com.mkyong.common.Person" var="personBean">
	<s:param name="favorFruit" value="#fruitBean"></s:param>
</s:bean>

Example 3

For primitive type, you are freely to declared it anywhere, it just work like a charm.

<s:bean name="com.mkyong.common.Person" var="personBean">
	<s:param name="age" value="99"></s:param>
</s:bean>
// or
<s:bean name="com.mkyong.common.Person" var="personBean">
	<s:param name="age">99</s:param>
</s:bean>
The “param” tag is not only available for the bean tag, it’s apply to almost all of the other tags that need parametrize.
Struts 2, “param” tag is a really confusing tag, many new Struts 2 developers are fall into this silly trap, and wonder why a simple “String” setter method is not work. Hope the Struts 2 team can design more user-friendly tag in future.

Struts 2 param tag example

A complete Struts 2 “param” tag example.

1. Action

An Action class to forward the request.

ParamTagAction.java

package com.mkyong.common.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class ParamTagAction extends ActionSupport{
 
	public String execute() {
		return SUCCESS;
	}
 
}

2. Bean

Two beans, initialize with “param” tag later.

Person.java

package com.mkyong.common;
 
public class Person{
 
	private String nickName;
	private int age;
	private Fruit favorFruit;
 
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Fruit getFavorFruit() {
		return favorFruit;
	}
	public void setFavorFruit(Fruit favorFruit) {
		this.favorFruit = favorFruit;
	}
 
	public String getFruitName(){
		return this.favorFruit.getFruitName();
	}
 
}

Fruit.java

package com.mkyong.common;
 
public class Fruit{
 
	private String fruitName;
 
	public String getFruitName() {
		return fruitName;
	}
 
	public void setFruitName(String fruitName) {
		this.fruitName = fruitName;
	}
 
}

3. param tag example

A JSP page to show the use of “param” tag.

param.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
 <html>
<head>
</head>
 
<body>
<h1>Struts 2 param tag example</h1>
 
<s:bean name="com.mkyong.common.Fruit" var="fruitBean">
	<s:param name="fruitName">Banana</s:param>
</s:bean>
 
<s:bean name="com.mkyong.common.Person" var="personBean">
	<s:param name="nickName">ah pig ah dog</s:param>
	<s:param name="age">99</s:param>
	<s:param name="favorFruit" value="#fruitBean"></s:param>
</s:bean>
 
<h3>PersonBean</h3>
<ol>
<li>NickName property : <s:property value="#personBean.nickName" /></li>
<li>Age property : <s:property value="#personBean.age" /></li>
<li>Fruit property : <s:property value="#personBean.fruitName" /></li>
</ol>
</body>
</html>

4. struts.xml

Link it ~

<?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="default" namespace="/" extends="struts-default">
 
		<action name="paramTagAction" 
			class="com.mkyong.common.action.ParamTagAction" >
			<result name="success">pages/param.jsp</result>
		</action>
 
	</package>
</struts>

5. Demo

http://localhost:8080/Struts2Example/paramTagAction.action

Output

Struts 2 param tag example

Reference

  1. Struts 2 param tag documentation
Note : You can find more similar articles at - Struts 2.x Tutorials