Struts2.2.1 handle multiple Submits in your application
Posted on: February 3, 2011 at 12:00 AM
 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 --ADS_TO_REPLACE_1

Directory structure of the Example-

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

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

pageEncoding="UTF-8"%>

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

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

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

<html>ADS_TO_REPLACE_4

<head>

<title>order details</title>

</head>ADS_TO_REPLACE_5

<body>

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

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

<s:form action="Pay">

<s:token />

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

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

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

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

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

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

<s:submit />ADS_TO_REPLACE_9

</s:form></div>

</body>

</html>

Here is the Thanks.jspADS_TO_REPLACE_10

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

pageEncoding="UTF-8"%>

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

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

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

<html>ADS_TO_REPLACE_12

<head>

<title>Thank you</title>

</head>ADS_TO_REPLACE_13

<body>

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

Your detail are.<br>ADS_TO_REPLACE_14

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

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

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

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

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

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

</body>

</html>

Here is the ERROR.jsp

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

pageEncoding="UTF-8"%>

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

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

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

<html>

<head>ADS_TO_REPLACE_19

<title>Thank you</title>

</head>

<body>ADS_TO_REPLACE_20

<s:actionerror />

</body>

</html>

Here is the  struts.xmlADS_TO_REPLACE_21

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"ADS_TO_REPLACE_22

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />ADS_TO_REPLACE_23

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

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

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

<interceptor-ref name="token" />

<interceptor-ref name="basicStack" />

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

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

<result>/Thanks.jsp</result>

</action>ADS_TO_REPLACE_26

</package>

</struts>

The action class MultipleSubmitAction.java is as follows.

package roseindia;ADS_TO_REPLACE_27

import com.opensymphony.xwork2.ActionSupport;

public class MultipleSubmitAction extends ActionSupport {

private String name;ADS_TO_REPLACE_28

private String address;

private String product;

private String cellNo;ADS_TO_REPLACE_29

private int quantity;

private int amount;

public String getName() {ADS_TO_REPLACE_30

return name;

}

public void setName(String name) {ADS_TO_REPLACE_31

this.name = name;

}

public String getAddress() {ADS_TO_REPLACE_32

return address;

}

public void setAddress(String address) {ADS_TO_REPLACE_33

this.address = address;

}

public String getProduct() {ADS_TO_REPLACE_34

return product;

}

public void setProduct(String product) {ADS_TO_REPLACE_35

this.product = product;

}

public String getCellNo() {ADS_TO_REPLACE_36

return cellNo;

}

public void setCellNo(String cellNo) {ADS_TO_REPLACE_37

this.cellNo = cellNo;

}

public int getQuantity() {ADS_TO_REPLACE_38

return quantity;

}

public void setQuantity(int quantity) {ADS_TO_REPLACE_39

this.quantity = quantity;

}

public int getAmount() {ADS_TO_REPLACE_40

return amount;

}

public void setAmount(int amount) {ADS_TO_REPLACE_41

this.amount = amount;

}

public String execute() throws Exception {ADS_TO_REPLACE_42

try {

Thread.sleep(4000);

} catch (InterruptedException e) {ADS_TO_REPLACE_43

}

return SUCCESS;

}ADS_TO_REPLACE_44

}

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- ADS_TO_REPLACE_45

Output:-

 

ADS_TO_REPLACE_46

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

Download Select Source CodeADS_TO_REPLACE_47

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

Advertisements

Ads

Ads

 
Advertisement null

Ads