本文共 8732 字,大约阅读时间需要 29 分钟。
struts.xml配置文件
< struts > < constant name = " struts.ui.theme " value = " simple " /> < package name = " person " namespace = " /test " extends = " struts-default " > < action name = " token " class = " com.ljq.action.PersonAction " > < interceptor - ref name = " defaultStack " /> < interceptor - ref name = " token " /> < result name = " invalid.token " >/ WEB - INF / page / error.jsp < result >/ WEB - INF / page / message.jsp < action name = " tokenSession " class = " com.ljq.action.PersonAction " > < interceptor - ref name = " defaultStack " /> < interceptor - ref name = " tokenSession " /> < result name = " invalid.token " >/ WEB - INF / page / error.jsp < result >/ WEB - INF / page / message.jsp
PersonAction类
package com.ljq.action; import java.util.ArrayList; import java.util.List; public class PersonAction { private String name; @SuppressWarnings( " unchecked " ) // 观看控制台 // 如果token生效则不会在控制台输出name的值,而会输出如下警告: 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; }}
index.jsp表单页面
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %> <% @ taglib uri = " /struts-tags " prefix = " s " %> < html > < head > < title > 防止表单重复提交 < meta http - equiv = " pragma " content = " no-cache " > < meta http - equiv = " cache-control " content = " no-cache " > < meta http - equiv = " expires " content = " 0 " > < body > < s:form action = " token " namespace = " /test " method = " post " > 姓名: < s:textfield name = " name " />< s:token > < input type = " submit " value = " 发送 " />
message.jsp返回成功页面
<% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %> <% @ taglib uri = " /struts-tags " prefix = " s " %> < html > < head > < title > My JSP ' index.jsp ' starting page < meta http - equiv = " pragma " content = " no-cache " > < meta http - equiv = " cache-control " content = " no-cache " > < meta http - equiv = " expires " content = " 0 " > < body > < s:property value = " name " />< br /> <%= new Date() %>
error.jsp表单重复提交提示页面
<% @ 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 + " / " ; %> < html > < head > < base href = " <%=basePath%> " > < title > My JSP ' error.jsp ' starting page < 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 " > < body > 您已经提交了表单,请不要重复提交。
转载地址:http://zcnni.baihongyu.com/