Here is the sample code:
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="execActionAction" method="POST">
<s:textfield name="somestring"/>
<br>
<s:submit value="Enter"/>
<s:token />
</s:form>
</body>
</html>
result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Result</title>
</head>
<body>
<s:property value="somestring" />
<br>
</body>
</html>
Test.java
package com.Test;
import com.opensymphony.xwork2.ActionSupport;
public class Test extends ActionSupport{
private String somestring;
public String getSomestring() {
return somestring;
}
public void setSomestring(String somestring) {
this.somestring = somestring;
}
public String execute() throws Exception{
System.out.println("Printing Token-Session");
return SUCCESS;
}
}
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>
<include file="struts-default.xml" />
<package name="mypkgTest" extends="struts-default">
<action name="execActionAction" class="com.Test.Test">
<interceptor-ref name="basicStack" />
<interceptor-ref name="tokenSession" />
<result name="success">/result.jsp</result>
<result name="invalid.token">/index.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>myBlankProject</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
The result page displayed the string correctly.
In my Model (POJO) I've print another string on the console which is "Printing Token-Session". It is already there in the console as you can verify in the below screen shot.
Now, I've resubmit the form by pressing "Refresh" button on the browser. Now you can see that there is only one occurrence of the string "Printing Token Session" on the console even I've resubmitted the form and there will be an Warning stating that"Form token
So, this is the advantage of Token Session Interceptor, by which you can stop resubmitting of the form by the user.