Struts 2 Processing form submit every type tag

Struts 2 Processing form submit every type tag

This demo you will see how we do process all form submitted value and send result back to the client with entered form value. Please follow below steps:

  • Create maven project name: Struts2FormAllTypeDataSubmit
  • Final project structure:

Struts 2 Processing form submit every type tag

  • pom.xml:
<dependencies>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.16.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-convention-plugin</artifactId>
			<version>2.3.16.3</version>
		</dependency>
</dependencies>
  •  input.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<?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 Processing form submit all type tag</title>
<s:head />
</head>

<body>
	<h3 style="color: green">
		<s:text name="Struts 2 Processing form submit all type tag" />
	</h3>
	<s:form action="hiddenValueAction" namespace="/" method="post"
		name="strutsForm">
		
		<s:textfield label="First Name" name="firstName"/><s:hidden name=""/>
		<s:textfield label="Last Name" name="lastName"/>
		<s:password name="password" label="Password"/>
		<s:radio list="radioButtonList" name="selectedRadionButton" id="radionButton" label="State"/>
		<s:textarea label="Text Area" name="textArea"/>
		<s:checkbox name="checkbox" label="Tick check box" labelposition="left"/>
		<s:combobox list="comboBoxList" name="combobox" label="Combo box" />	
		<s:select list="selectList" name="selectedValue" label="Select state List"/>
		<tr><td><p><s:reset name="Reset" theme="simple"/><s:submit theme="simple"/></p></td></tr>
	</s:form>
</body>
</html>
  •  success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<?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 Processing form submit all type tag</title>
<s:head />
</head>

<body>
	<h3 style="color: green">
		<s:text name="Your submitted value" />
	</h3>
	<p>First Name: <s:property value="firstName" /></p>
	<p>Last Name: <s:property value="lastName" /></p>
	<p>Password: <s:property value="password" /></p>
	<p>Selected Radio button: <s:property value="selectedRadionButton" /></p>
	<p>Text Area value: <s:property value="textArea" /></p>
	<p>Check box value: <s:property value="checkbox" /></p>
	<p>Combo box value: <s:property value="combobox" /></p>	
	<p>Select list value: <s:property value="selectedValue" /></p>
</body>
</html>
  •  AllFromValueAction.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 = "/input.jsp"),
		@Result(name = "success", location = "/success.jsp"),
		@Result(name = "input", location = "/input.jsp") })
public class AllFromValueAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String firstName;
	private String lastName;
	private String password;
	private String checkbox;
	private String combobox;
	private String textArea;
	private String selectedRadionButton;
	private String selectedValue;
	private List<String> radioButtonList = new ArrayList<String>();
	private List<String> comboBoxList = new ArrayList<String>();
	private List<String> selectList = new ArrayList<String>();

	public AllFromValueAction() {

		radioButtonList.add("NY");
		radioButtonList.add("NJ");
		radioButtonList.add("MA");
		comboBoxList = radioButtonList;
		selectList = radioButtonList;
		setRadioButtonList(radioButtonList);
		setComboBoxList(comboBoxList);
		setSelectList(selectList);
	}

	@Override
	@Action(value = "/allFromValueAction")
	public String execute() throws Exception {
		return ActionSupport.SUCCESS;
	}

	@Action(value = "/displayAction")
	public String displayPage() {
		return ActionSupport.INPUT;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getTextArea() {
		return textArea;
	}

	public void setTextArea(String textArea) {
		this.textArea = textArea;
	}

	public List<String> getRadioButtonList() {
		return radioButtonList;
	}

	public void setRadioButtonList(List<String> radioButtonList) {
		this.radioButtonList = radioButtonList;
	}

	public List<String> getComboBoxList() {
		return comboBoxList;
	}

	public void setComboBoxList(List<String> comboBoxList) {
		this.comboBoxList = comboBoxList;
	}

	public List<String> getSelectList() {
		return selectList;
	}

	public void setSelectList(List<String> selectList) {
		this.selectList = selectList;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(String checkbox) {
		this.checkbox = checkbox;
	}

	public String getCombobox() {
		return combobox;
	}

	public void setCombobox(String combobox) {
		this.combobox = combobox;
	}

	public String getSelectedRadionButton() {
		return selectedRadionButton;
	}

	public void setSelectedRadionButton(String selectedRadionButton) {
		this.selectedRadionButton = selectedRadionButton;
	}

	public String getSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

}

 

  • Run this demo in tomcat server. If you didn’t configure tomcat set up in eclipse yet please use this tutorial: Configure and Run Tomcat server in eclipse. Now right click project –>Run As –> Run on server you will see below input form page:

Struts 2 Processing form submit every type tag

 

  • Enter value on the form:

19

  • Click submit button on the form . You will see entered form data as below:

Struts 2 Processing form submit every type tag

 

download Download Project:  Struts2FormAllTypeDataSubmit

For more information please read this struts 2 site

Leave a Reply

Your email address will not be published. Required fields are marked *