Struts 2 <s:hidden> hidden value example

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
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.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