Struts 2 pass hidden value
If you want to pass hidden value to the server including value form list please see example below:
- Project structure:
- index.jsp page:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Struts 2 pass hidden value</title> <s:head /> </head> <body> <h3 style="color: green"> <s:text name="Struts 2 pass hidden value" /> </h3> <s:if test="hasActionErrors()"> <div id="fieldErrors"> <s:actionerror /> </div> </s:if> <s:form action="hiddenValueAction" namespace="/" method="post" name="strutsForm"> <table cellspacing="0" cellpadding="0" border="1" width="500"> <tr> <td><h3 align="center">Struts 2 pass hidden value</h3></td> </tr> <tr> <td><div style="height: 200px; overflow: auto;"> <table cellspacing="0" cellpadding="0" border="0" width="100%"> <tr> <td><h3>First Name</h3></td> <td><h3>Last Name</h3></td> <td><h3>Location</h3></td> </tr> <s:iterator status="stat" value="personsList"> <tr> <!-- Hidden value from list--> <td><s:property value="firstName" /><s:hidden name="personsList.firstName" value="%{firstName}"/></td> <td><s:property value="lastName" /><s:hidden name="personsList.lastName" value="%{lastName}"/></td> <td><s:property value="location" /><s:hidden name="personsList.location" value="%{location}"/></td> </tr> </s:iterator> <!-- Hidden value --> <s:hidden name="hiddenValue" value="hiddenValueTest" /> </table> </div></td> </tr> </table> </br><s:submit align="left"/> </s:form> </body> </html>
- HiddenValueAction.java:
package com.javahonk.action; import java.util.ArrayList; import java.util.List; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; @Results({ @Result(name = "error", location = "/index.jsp"), @Result(name = "input", location = "/index.jsp") }) public class HiddenValueAction extends ActionSupport { private static final long serialVersionUID = 1L; private List<Person> personsList = new ArrayList<Person>(); private String hiddenValue; @Override @Action(value = "/hiddenValueAction") public String execute() throws Exception { for (int i = 0; i < 5; i++) { Person person = new Person(); person.setFirstName("Java"+i); person.setLastName("Honk"); person.setLocation("NY"); person.setCheckbox(String.valueOf(i)); personsList.add(person); } return ActionSupport.INPUT; } public List<Person> getPersonsList() { return personsList; } public void setPersonsList(List<Person> personsList) { this.personsList = personsList; } public String getHiddenValue() { return hiddenValue; } public void setHiddenValue(String hiddenValue) { this.hiddenValue = hiddenValue; } }
- Web page output:
For more information please read this tutorial
Download Project: Struts2HiddenValue