Struts 2 set tag example

Download It – Struts2-Set-Tag-Example.zip

Struts 2 “set” tag is used to assign a value to a variable in a specified scope (application, session, request, page, or action), the action is the default scope. See a complete “set” tag example :

The “value” means any hard-coded String, property value or just anything you can reference.

1. Action

Action class with a “msg” property.

SetTagAction.java


package com.mkyong.common.action;

import com.opensymphony.xwork2.ActionSupport;
 
public class SetTagAction extends ActionSupport{

	private String msg = "Struts 2 is a funny framework";
	
	public String getMsg() {
		return msg;
	}

	public String execute() throws Exception {
		
		return SUCCESS;
	}
}

2. set tag example

It shows the use of “set” tag.

set.jsp


<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
 
<body>
<h1>Struts 2 set tag example</h1>
<div><div class="box-ads">
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- mkyong-in-article-responsive-1st-h2 -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-2836379775501347"
     data-ad-slot="2195512879"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div></div><h2>1. <s:set var="varMsg" value="msg" /></h2>

<s:set var="varMsg" value="msg" />
<s:property value="varMsg" />

<h2>2. <s:set var="varUrl" value="%{'http://www.mkyong.com'}" /></h2> 

<s:set var="varUrl" value="%{'http://www.mkyong.com'}" />
<s:property value="varUrl" />


</body>
</html>

How it work?

1. <s:set var=”varMsg” value=”msg” />
Call the action’s getMsg() method and assign the returned value to a variable named “varMsg“.

2. <s:set var=”varUrl” value=”%{‘http://www.mkyong.com’}” />
Hard coded a string and assign it to a variable named “varUrl“.

Assign value to a variable, not property value.

For example,


public class SetTagAction extends ActionSupport{

	private String msg;
	
	public String setMsg(String msg) {
		this.msg = msg;
	}
	...

<s:set var="msg" value="%{'this is a message'}" />

Many Struts 2 developers thought that the “set” tag var=”msg” will assign the value to the associated action class via setMsg() method.

This is wrong, the set tag will not call the setMsg() method, it will only assign the “value” to a variable named “msg“, not the action’s property value.

3. 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="setTagAction" 
			class="com.mkyong.common.action.SetTagAction" >
			<result name="success">pages/set.jsp</result>
		</action>
		
	</package>
</struts>

5. Demo

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

Output

Struts 2 set tag example

Reference

  1. Struts 2 set tag documentation

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
ManishAnant ChoubeyAlexandremayurElaine Recent comment authors
newest oldest most voted
Anant Choubey
Guest
Anant Choubey

Hi,

I have a question. What if i don’t want to hard code the JSP name in the struts.xml? Suppose, i want to rename all the JSP’s used in the struts.xml. Is there a way in which i store the jsp name in some file and then refer that file in the struts.xml.

thanks.

Alexandre
Guest
Alexandre

Thank you very much for your clear and helpful explainations !

mayur
Guest
mayur

Hi,
I am trying to write same code for int variable.
but that’s not working for me. it works fine with string but not with Int variable

Manish
Guest
Manish

Its works if u do like this

Elaine
Guest
Elaine

I would like to ask is there any where to set a value from a s property to s set? I got a Array list which contains value like this [2,3,4] when I use s iterator to loop it, i use s property can get the value in the list. <s:iterator value="list"> <s:property/> </s:iterator> but I would like to set the value I get from the list to s set so that I can use for s if. I would like to do some comparison like this. <s:if test='%{map["WEEK"] eq <s:property/>}'></s:if> I know it is impossible to put a s… Read more »

Mike
Guest
Mike

How set something like

<s:set name="ipAdd" value="%{}”