Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The Set Tag
2.1.
Action
2.2.
Example
3.
Frequently Asked Questions
3.1.
What is Struts?
3.2.
What is Web Framework?
3.3.
What is API?
3.4.
What purpose does a set tag serve?
3.5.
What exactly are action tag struts?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

The Set Tag

Introduction

Sometimes while using the Struts2 Java framework, one might wonder how to assign value to a variable in the specified scope. The answer is using the set tag.

So, let us learn about the set tag.

Introduction

The Set Tag

The Set Tag in Struts 2 is used to give a variable a value in a scope that has been defined. The scope can be application, session, request, page, or action where the action is the default scope. Here value means any hard-coded String, a property value, or simply anything you can reference.

Syntax: <s:set var="variable_Name" value="variable_Value" />

Action

Action class with - msg property

SetTagAct.java

package com.indra.common.action;
import com.opensymphony.xwork2.ActionSupport;
 
	public class SetTagAct extends ActionSupport{
	private String msg = "Struts 2 is a framework";
	public String getMsg() {
		return msg;
		}
	public String execute() throws Exception {
		return SUCCESS;
		}
	}

Example

In the following example, you will learn how to use the set tag.

The set tag

set.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
	<body>
		<h1>Struts2 Set Tag Example</h1>
		<h2>1. &lt;s:set var="variable_Msg" value="msg" /&gt;</h2>

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

		<h2>2. &lt;s:set var="variable_URL" value="%{'https://www.codingninjas.com/'}" /&gt;</h2> 

	<s:set var="variable_URL" value="%{'https://www.codingninjas.com/'}" />
	<s:property value="variable_URL" />

	</body>
</html>

 

Explanation -

  1. <s:set var="variable_Msg" value="msg" />
    It calls the getMsg() method of action and assigns the returned value to a variable named "variable_Msg".
  2. <s:set var="variable_URL" value="%{'https://www.codingninjas.com/'}" />
    Hard-coded a string and assigned it to a variable named "variable_URL".

struts.xml

<?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="setTagAct" 
			class="com.indra.common.action.SetTagAct" >
			<result name="success">pages/set.jsp</result>
		</action>

	</package>
</struts>


Output - 

Output

Frequently Asked Questions

What is Struts?

Struts is a free, open-source framework for web applications written in Java. Apache Software Foundation maintains this framework. The Action, Interceptor, and Result pages are the three main Struts2 building blocks. Action classes can be created in a variety of methods with Struts2 and then configured using Struts.

What is Web Framework?

It is a software framework, also known as a web application framework (WAF), which helps in the creation of web applications, including web APIs, web services, and web resources.

What is API?

API (Application Programming Interface) is a means of communication between two or more computer programs. It is a kind of software interface that provides a service to other software programs.

What purpose does a set tag serve?

The objects would be kept in a static map with strongly referenced values by setTag(int, Object). This implies that the context, which points to essentially everything else, will leak if the object contains any references referring back to it.

What exactly are action tag struts?

Using the action tag, programmers can carry out actions from the view page. By naming the activity, they can achieve this. To render the result in the view directly, they can set the "executeResult" option to "true."

Conclusion

In this article, we discussed the Set Tag. We learned how the set tag in Struts2 is used to give a variable a value in a specified scope.

See the official website for documentation and other information about Struts. To learn about various Java frameworks, click here.

Nevertheless, you may consider our paid courses to give your career an edge over others.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass