一、写跳转用的发帖链接页面,点击后,对应的action中会对该次提交加上一个Token令牌
二、Action中的forTopic方法给该提交加令牌Token
三、发帖内容填写页面publishTopic.jsp
四、提交后成功时显示的页面publishshow.jsp
五、后退浏览器重复提交后显示的页面error.jsp
六、struts-config.xml中对应的配置
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<html:base />
<title>forum.jsp</title>
</head>
<body>
<html:link action="/publish.do?status=forTopic">我要发帖</html:link>
</body>
</html:html>
二、Action中的forTopic方法给该提交加令牌Token
package com.test.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.test.struts.form.PublishForm;
public class PublishAction extends DispatchAction {
//这个方法给publishTopic-1.jsp的链接加上令牌
public ActionForward forTopic(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
PublishForm publishForm = (PublishForm) form;// TODO Auto-generated method stub
System.out.println("forTopic...");
this.saveToken(request);
return mapping.findForward("publishTopic");
}
//用于检查令牌是否过时,并做出相应处理
public ActionForward toTopic(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
PublishForm publishForm = (PublishForm) form;// TODO Auto-generated method stub
if(isTokenValid(request)){ //如果两个Token相匹配的话
System.out.println("toTopic...");
String title = request.getParameter("title");
String content = request.getParameter("content");
request.setAttribute("title", title);
request.setAttribute("content", content);
this.resetToken(request);
return mapping.findForward("publishshow");
}else{
System.out.println("for resubmit error...");
return mapping.findForward("error");
}
}
}
三、发帖内容填写页面publishTopic.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>publishTopic.jsp</title>
</head>
<body>
<html:form action="/publish.do?status=toTopic" method="post">
<input type="text" name="title"/><br>
<input type="text" name="content"/><br>
<input type="submit" value="发表"/>
<input type="reset" value="重置"/>
</html:form>
<h1>good!!</h1>
</body>
</html:html>
四、提交后成功时显示的页面publishshow.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>publishshow.jsp</title>
</head>
<body>
<h1>发表后的文章信息:</h1>
<input type="text" value="<%=request.getAttribute("title") %>"/><br>
<input type="text" value="<%=request.getAttribute("content") %>"/>
</body>
</html:html>
五、后退浏览器重复提交后显示的页面error.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>error.jsp</title>
</head>
<body>
<h1>重复提交了!!!</h1>
</body>
</html:html>
六、struts-config.xml中对应的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="publishForm" type="com.test.struts.form.PublishForm" />
<form-bean name="tokenActionForm" type="com.test.struts.form.TokenActionForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="publishForm"
input="/error.jsp"
name="publishForm"
parameter="status"
path="/publish"
scope="request"
type="com.test.struts.action.PublishAction">
<forward name="publishTopic" path="/publishTopic.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
<forward name="publishshow" path="/publishshow.jsp"></forward>
</action>
<action
attribute="tokenActionForm"
parameter="status"
input="/error.jsp"
path="/tokenAction"
scope="request"
name="tokenActionForm"
type="com.test.struts.action.TokenActionAction">
<forward name="strutsToken1" path="/strutsToken1.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.test.struts.ApplicationResources" />
</struts-config>