日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

struts2之防止表單重復(fù)提交

 dawn001 2014-05-24

struts.xml配置文件

復(fù)制代碼
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts./dtds/struts-2.0.dtd">

<struts>
<!-- 默認(rèn)的視圖主題 -->
<constant name="struts.ui.theme" value="simple" />

<!-- struts2在防止表單重復(fù)提交的攔截中有2個(gè),分別是:token,tokenSession。tokenSession繼承token而來(lái)。
通常情況下,使用tokenSession客戶端感覺會(huì)比較友好。
-->
<!-- 如果重復(fù)提交,會(huì)跳轉(zhuǎn)到error.jsp頁(yè)面 -->
<package name="person" namespace="/test" extends="struts-default">
<action name="token" class="com.ljq.action.PersonAction">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="token" />
<!-- 如果重復(fù)提交,跳轉(zhuǎn)到error.jsp頁(yè)面 -->
<result name="invalid.token">/WEB-INF/page/error.jsp</result>
<result>/WEB-INF/page/message.jsp</result>
</action>
<action name="tokenSession" class="com.ljq.action.PersonAction">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="tokenSession" />
<!-- 如果重復(fù)提交,不會(huì)跳轉(zhuǎn)到error.jsp頁(yè)面 -->
<result name="invalid.token">/WEB-INF/page/error.jsp</result>
<result>/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
復(fù)制代碼

      

PersonAction類

復(fù)制代碼
package com.ljq.action;


import java.util.ArrayList;
import java.util.List;

public class PersonAction {

private String name;

@SuppressWarnings(
"unchecked")
//觀看控制臺(tái)
//如果token生效則不會(huì)在控制臺(tái)輸出name的值,而會(huì)輸出如下警告: 2011-3-14 20:45:32 com.opensymphony.xwork2.util.logging.commons.CommonsLogger
//warn 警告: Form token EDZ4S96RNDN5VD8B1CQTK6FTHIJUPC66 does not match the session token null.
public String execute() {
List ls
= new ArrayList();
ls.add(name);
for (int i = 0; i < ls.size(); i++) {
System.out.println(ls.get(i));
}
return "success";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
復(fù)制代碼

      

index.jsp表單頁(yè)面

復(fù)制代碼
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>防止表單重復(fù)提交</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>

<body>
<!-- 防止表單重復(fù)提交,記得在form表單里填上<s:token></s:token> -->
<!-- action="token"、action="tokenSession" -->
<s:form action="token" namespace="/test" method="post">
姓名:
<s:textfield name="name"/><s:token></s:token>
<input type="submit" value="發(fā)送"/>
</s:form>
</body>
</html>
復(fù)制代碼

      

message.jsp返回成功頁(yè)面

復(fù)制代碼
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>

<body>
<s:property value="name"/><br/>
<%=new Date() %>
</body>
</html>
復(fù)制代碼

      

error.jsp表單重復(fù)提交提示頁(yè)面

復(fù)制代碼
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path
= request.getContextPath();
String basePath
= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'error.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
您已經(jīng)提交了表單,請(qǐng)不要重復(fù)提交。
</body>
</html>
復(fù)制代碼

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多