Pages Navigation Menu

Coding is much easier than you think

Struts 2 <s:hidden> example

Struts 2 <s:hidden> example

 


 
In Struts 2 , you can use the <s:hidden> tag to create a HTML hidden field.

 

<s:hidden name="hiddenValue" value="simplecodestuffs" />

It will render as the following HTML code.

<input type="hidden" name="hiddenValue" value="simplecodestuffs" />

 

Struts 2 <s:hidden> example

 
A page with a url hidden value, and display the hidden value after form is submitted.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

1. Folder Structure :

 

Folder Structurehidden

 

2. Action class

 

HiddenAction.java

 

package com.simplecode.action;

import com.opensymphony.xwork2.ActionSupport;

public class HiddenAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String hiddenValue;

	public String getHiddenValue() {
		return hiddenValue;
	}

	public void setHiddenValue(String hiddenValue) {
		this.hiddenValue = hiddenValue;
	}

	public String execute() {
		return SUCCESS;
	}

}

 

3. JSP View page

 
Struts 2 €œs:hidden€ tag to create a hidden value field.

hidden.jsp

 

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Hidden</title>
</head>
<body>
	<h3>Struts 2 - &lt;s:hidden&gt; tag example</h3>
	This page has a hidden value (view source):
	<s:form action="Hidden">
		<h4>

			<s:hidden name="hiddenValue" value="simplecodestuffs" />
			<s:submit key="project.submit" name="submit" />
		</h4>
	</s:form>
</body>
</html>

 

success.jsp
 

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome Page</title>
</head>

<body>
	<h3>Struts 2 - &lt;s:hidden&gt; tag example</h3>

	The hidden value :
	<s:property value="hiddenValue" />

</body>
</html>

 

4. 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.custom.i18n.resources" value="project" />
	<package name="default" extends="struts-default" namespace="/jsp">
		<action name="Hiddenjsp">
			<result>/jsp/hidden.jsp</result>
		</action>
		<action name="Hidden" class="com.simplecode.action.HiddenAction">
			<result name="success">/jsp/success.jsp</result>
			<result name="input">/jsp/hidden.jsp</result>
		</action>
	</package>
</struts>

 

5. Demo

 
http://localhost:8089/Struts2_hidden/jsp/Hiddenjsp
 

s:hidden

 
Output :
 
s:hidden box

 

 

Reference

  1. Struts 2 hidden field documentation

 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

  • Shashikant Mourya

    Hello,
    If I have multiple submit buttons (with call to different action methods) or multiple forms and I want to pass a same hidden value to all, do I need to use multiple tags or Can I use a common tag for all buttons??

    • http://www.simplecodestuffs.com Mohaideen Jamil

      Common tag is enough