Struts2 set tag example. How to access struts2 variable in jsp scriptlet?

The struts2 <s:set> tag is used to assign a value / expression to a variable.  Suppose you want to edit the value of one field based on the other field’s value in  JSP.   For e.g. mobileNo field should be editable based on the userid. If the userid is admin, then the mobileNo should be editable otherwise  it should be read only. One way to do this  is to  use a variable and a scripting variable. i.e.  You can assign a  property value to a variable using <s:set> tag and the variable can be converted to scripting variable using  <jsp:useBean> tag. The same task can be done in struts 1.2  using  <bean:define> tag. Now let us see the code for using <s:set> tag.

package net.javaonline.action;

import com.opensymphony.xwork2.ActionSupport;

public class SetTagAction extends ActionSupport {

	String userRole="";
	String mobileNo="";

	public String getMobileNo() {
		return mobileNo;

	public void setMobileNo(String mobileNo) {
		this.mobileNo = mobileNo;

	public String getUserRole() {
		return userRole;

	public void setUserRole(String userRole) {
		this.userRole = userRole;

	public String execute() throws Exception {


		return SUCCESS;



<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Set Tag Example</title>
<s:form name="setTagForm" action="process.action" metod="post" >
<TABLE id="rtbl" width="350px" border="1">
  <s:set var="role" value="userRole" />
  <jsp:useBean id="role" type="java.lang.String" />
	  if (role.equals("admin")) {%>
  		Mobile Number (Editable) :	<s:textfield name="mobileNo"/>
  <%} else{ %>
  		Mobile Number (Read Only) :	<s:property value="mobileNo"/>



<?xml version="1.0" encoding="UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

	<constant name="struts.enable.DynamicMethodInvocation" 	value="false" />
	<constant name="struts.devMode" value="false" />

	<package name="default" extends="struts-default" namespace="/">

	<action name="setTag" class="net.javaonline.action.SetTagAction">
  		<result name="success">SetTagExample.jsp</result>


Run the program by calling :


if userid is admin

set tag example output1


if userid is not admin

set tag example output2


Reference : Struts2 Set Tag

Leave a Reply