Download this Struts hidden value example – Struts-HiddenValue-Example.zip

In this Struts example, you will learn how to create a HTML hidden field with Struts <html:hidden> tag.

1. Folder Structure

This is the final project structure created by Maven. Please create the corresponding folders.

Struts-hidden-value-folder

2. Action class

Create an Action class, do nothing but forward the request.

HtmlHiddenAction.java

package com.mkyong.common.action;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
 
public class HtmlHiddenAction extends Action{
 
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
 
		return mapping.findForward("success");
	}
 
}

3. Properties file

Create a properties file, and declare the error and label messages.

Common.properties

#error message
error.common.html.hidden.name.required = Hidden value "Name" is required.
 
#label message
label.common.html.hidden.button.submit = Submit
label.common.html.hidden.button.reset = Reset

4. ActionForm

Create a ActionForm, contains a name variable and form validation – validate().

HtmlHiddenForm.java

package com.mkyong.common.form;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
 
public class HtmlHiddenForm extends ActionForm{
 
	String name;
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
 
	    ActionErrors errors = new ActionErrors();
 
	    if( getName() == null || ("".equals(getName()))) {
	       errors.add("common.name.err",
                  new ActionMessage("error.common.html.hidden.name.required"));
	    }
 
	    return errors;
	}
 
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// reset properties
		name = "";
	}
 
}

5. JSP Page

Use the Struts’s HTML tag <html:hidden> to create a HTML hidden value.

hidden.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
</head>
<body>
<h1>Struts html:hidden example</h1>
 
<html:form action="/Hidden">
 
<html:messages id="err_name" property="common.name.err">
<div style="color:red">
	<bean:write name="err_name" />
</div>
</html:messages>
 
<div style="padding:16px">
	<html:hidden property="name" value="This is mkyong.com" />
	A hidden field, please view it from source file.
</div>
 
<div style="padding:16px">
	<div style="float:left;padding-right:8px;">
		<html:submit>
                  <bean:message key="label.common.html.hidden.button.submit" />
                </html:submit>
	</div>
	<html:reset>
            <bean:message key="label.common.html.hidden.button.reset" />
        </html:reset>
</div>
 
</html:form>
 
</body>
</html>

Get the hidden value from ActionForm and display it

display.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
</head>
<body>
<h1>
	Hidden value : <bean:write name="htmlHiddenForm" property="name" />
</h1>
</body>
</html>

6. struts-config.xml

Create a Struts configuration file and link all together.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
 
	<form-beans>
		<form-bean
			name="htmlHiddenForm"
			type="com.mkyong.common.form.HtmlHiddenForm"/>
 
	</form-beans>
 
	<action-mappings>
 
	    <action
			path="/HiddenPage"
			type="org.apache.struts.actions.ForwardAction"
			parameter="/pages/hidden.jsp"/>
 
		<action
			path="/Hidden"
			type="com.mkyong.common.action.HtmlHiddenAction"
			name="htmlHiddenForm"
			validate="true"
			input="/pages/hidden.jsp"
			>	
 
			<forward name="success" path="/pages/display.jsp"/>
		</action>
	</action-mappings>
 
	<message-resources
		parameter="com.mkyong.common.properties.Common" />
 
</struts-config>

7. web.xml

Final step, create a web.xml for the Strut framework integration.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Maven Struts Examples</display-name>
 
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
         /WEB-INF/struts-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping>
 
</web-app>

Access it

http://localhost:8080/StrutsExample/HiddenPage.do

Struts-hidden-value-example1

hidden.jsp HTML source code.

<html>
<head>
</head>
<body>
<h1>Struts html:hidden example</h1>
 
<form name="htmlHiddenForm" method="post" action="/StrutsExample/Hidden.do">
 
<div style="padding:16px">
	<input type="hidden" name="name" value="This is mkyong.com">
 
	A hidden field, please view it from source file.
</div>
 
<div style="padding:16px">
	<div style="float:left;padding-right:8px;">
		<input type="submit" value="Submit">
	</div>
	<input type="reset" value="Reset">
</div>
 
</form>
 
</body>
</html>

Pressed the submit button, it will forward to

http://localhost:8080/StrutsExample/Hidden.do

and display the hidden value.

Struts-hidden-value-example2
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Struts 1.x Tutorials