Home Struts Struts Struts2.2.1 Struts2.2.1 handle multiple Submits in your application

Ask Questions?

View Latest Questions


 
 

Struts2.2.1 handle multiple Submits in your application
Posted on: February 3, 2011 at 12:00 AM
Advertisement
 In this tutorial, We will discuss about the handling the multiple submits in the application.

Struts2.2.1  handle multiple Submits in your application

 In this tutorial, We will discuss about the handling the multiple submits in the application.

In this example we enter the detail in the form and submit.if we click the submit button more than one time in the current token session then it give a error message otherwise it produce the output. In this example we use the Token Interceptor. the token tag used in the index.jsp file will generate a token for every session. and this token is aquired by only one Interceptor at a time and other can not aquired and on multiple submit generates error.

The following Example will shows how to handle the multiple Submits in the Struts2.2.1 --

Directory structure of the Example-

First we create a JSP file named index.jsp as follows.

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

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

<html>

<head>

<title>order details</title>

</head>

<body>

<div id="global" style="width: 350px">

<h3>Please enter your order details</h3>

<s:form action="Pay">

<s:token />

<s:textfield name="name" label="Name" />

<s:textfield name="address" label="Address" />

<s:textfield name="cellNo" label="CellNo" />

<s:textfield name="product" label="Name of Product" />

<s:textfield name="amount" label="Price" />

<s:textfield name="quantity" label="Quantity" />

<s:submit />

</s:form></div>

</body>

</html>

Here is the Thanks.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

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

<html>

<head>

<title>Thank you</title>

</head>

<body>

Thank you. We will ship your order within 3 days.<br>

Your detail are.<br>

Name :<s:property value="name"/><br>

Address :<s:property value="address"/><br>

CellNo :<s:property value="cellNo"/><br>

Product Name :<s:property value="product"/><br>

Quantity :<s:property value="quantity"/><br>

Amount :<s:property value="amount*quantity"/>

</body>

</html>

Here is the ERROR.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

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

<html>

<head>

<title>Thank you</title>

</head>

<body>

<s:actionerror />

</body>

</html>

Here is the  struts.xml

<?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.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.devMode" value="false" />

<package name="default" extends="struts-default" namespace="/">

<action name="Pay" class="roseindia.MultipleSubmitAction">

<interceptor-ref name="token" />

<interceptor-ref name="basicStack" />

<result name="invalid.token">/ERROR.jsp</result>

<result name="input">/index.jsp</result>

<result>/Thanks.jsp</result>

</action>

</package>

</struts>

The action class MultipleSubmitAction.java is as follows.

package roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class MultipleSubmitAction extends ActionSupport {

private String name;

private String address;

private String product;

private String cellNo;

private int quantity;

private int amount;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public String getProduct() {

return product;

}

public void setProduct(String product) {

this.product = product;

}

public String getCellNo() {

return cellNo;

}

public void setCellNo(String cellNo) {

this.cellNo = cellNo;

}

public int getQuantity() {

return quantity;

}

public void setQuantity(int quantity) {

this.quantity = quantity;

}

public int getAmount() {

return amount;

}

public void setAmount(int amount) {

this.amount = amount;

}

public String execute() throws Exception {

try {

Thread.sleep(4000);

} catch (InterruptedException e) {

}

return SUCCESS;

}

}

Here is the TokenInterceptor.properties file

struts.messages.invalid.token=You have submitted the form

the second time. Please contact the administrator.

This Program produces output on the basis of the Token interceptor, This  give the output as-

Output:-

 

When we click submit button more than one times .then output is shown below.

Download Select Source Code

Related Tags for Struts2.2.1 handle multiple Submits in your application:


DMCA.com