Struts 2 <s:hidden> hidden value example

Download It – Struts2-Hidden-Example.zip

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


<s:hidden name="url" value="http://www.mkyong.com" />

It will render as the following HTML code.


<input type="hidden" name="url" value="http://www.mkyong.com" />

Struts 2 <s:hidden> example

A page with a url hidden value, and display the hidden value after form is submitted.

1. Action

HiddenAction.java


package com.mkyong.common.action;

import com.opensymphony.xwork2.ActionSupport;

public class HiddenAction extends ActionSupport{

	private String url;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String execute() {
		return SUCCESS;
	}

}

2. View page

Struts 2 “s:hidden” tag to create a hidden value field.

hidden.jsp


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

<body>
<h1>Struts 2 - hidden value example</h1>

<s:form action="helloHidden" namespace="/">

	<h2>This page has a hidden value (view source): 
	<s:hidden name="url" value="http://www.mkyong.com" /></h2> 

	<s:submit value="submit" name="submit" />
	
</s:form>

</body>
</html>

welcome.jsp


<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>Struts 2 - hidden value example</h1>

<h2>
  The hidden value :
  <s:property value="url"/>
</h2> 

</body>
</html>

3. struts.xml

Link all together ~


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

4. Demo

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

Struts2 hidden value example
Struts2 hidden value example

Reference

  1. Struts 2 hidden field documentation

About the Author

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

Comments

Leave a Reply

avatar
newest oldest most voted
Mohamed Himmiche
Guest
Mohamed Himmiche

Hi,

When we can use hidden value.
For example : two actions (“list” and “listAdvanced”) wich forward to the same page “result.jsp” , i want to return to the calling page to modify some properties:
– My propleme is : i don’t know wich method to manage this, using scope interceptor or hidden value to pass the value of the calling page ?
Thanks

Sebastian
Guest
Sebastian

How can I access the Hidden Field in my Action?

Naveen
Guest
Naveen

Hi,

I am using Struts 2.0 version in my web application.
I have Query related to struts ‘redirect-action’ it works fine to me..

My code in struts.xml is:

redirectUrl
true
${param1}
${param2}

After redirect my browser shows url like this:

http://localhost:8080/HelloWorld/redirectUrl.view?param1=LOAD&param2=1

My Query is can I hide my url parameters in struts2….?

And what is difference between redirect and redirect-action result type.. ?

Thanks in Advance…

JOJO
Guest
JOJO

Did you get any solution this?…is it possible to hile the parameters in URL, Please let me know, I have the same requirement. Thanks

ssank
Guest
ssank

ne bokka ra

trackback
Struts 2 Tutorial

[…] 2 <s:textfield> textbox example.Password example Struts 2 <s:password> password example.Hidden value example Struts 2 <s:hidden> hidden value example.Textarea example Struts 2 <s:textarea> […]