参考各位老大的经验,测试代码如下:
input.jsp:
[quote]<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=GBK" %>
<html:html>
<head><title>Test</title></head>
<body>
<html:form action="/testAction.do" method="post">
<br>
<html:text property="name"/>
<br>
<html:text property="pass"/>
<br>
<html:submit property="submit" value="Submit"/><br>
</html:form>
</body>
</html:html>
[/quote]
success.jsp:
[quote]
<%@ page contentType="text/html; charset=GBK" %>
<html><head><title>success</title></head>
<body bgcolor="#ffffff">
success!
</body>
</html>
[/quote]
TestForm:
[quote]
package test;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class TestForm extends ActionForm {
private String name;
private String pass;
public void setPass(String pass) {
this.pass = pass;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getPass() {
return pass;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/** @todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
[/quote]
TestAction:
[quote]
package test;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.log4j.Logger;
public class TestAction extends Action {
private static final transient Logger log = Logger.getLogger(TestAction.class);
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestForm testForm = (TestForm) form;
if (testForm.getName() == null) {
saveToken(request);
return mapping.getInputForward();
}
if (!isTokenValid(request)) {
saveToken(request);
log.debug("不能重复提交!");
return mapping.getInputForward();
}
log.debug("添加成功!");
resetToken(request);
return mapping.findForward("success");
}
}
[/quote]
struts-config.xml:
[quote]
<?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>
<form-beans>
<form-bean name="testActionForm" type="test.TestForm"/>
</form-beans>
<action-mappings>
<action name="testActionForm" path="/testAction" type="test.TestAction" scope="request" input="/index.jsp" validate="false">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
</struts-config>
[/quote]
编写完上边的代码后:[url]http://localhost:8080/test/testAction.do[/url]可以进行测试!
input.jsp:
[quote]<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=GBK" %>
<html:html>
<head><title>Test</title></head>
<body>
<html:form action="/testAction.do" method="post">
<br>
<html:text property="name"/>
<br>
<html:text property="pass"/>
<br>
<html:submit property="submit" value="Submit"/><br>
</html:form>
</body>
</html:html>
[/quote]
success.jsp:
[quote]
<%@ page contentType="text/html; charset=GBK" %>
<html><head><title>success</title></head>
<body bgcolor="#ffffff">
success!
</body>
</html>
[/quote]
TestForm:
[quote]
package test;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class TestForm extends ActionForm {
private String name;
private String pass;
public void setPass(String pass) {
this.pass = pass;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getPass() {
return pass;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/** @todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
[/quote]
TestAction:
[quote]
package test;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.log4j.Logger;
public class TestAction extends Action {
private static final transient Logger log = Logger.getLogger(TestAction.class);
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestForm testForm = (TestForm) form;
if (testForm.getName() == null) {
saveToken(request);
return mapping.getInputForward();
}
if (!isTokenValid(request)) {
saveToken(request);
log.debug("不能重复提交!");
return mapping.getInputForward();
}
log.debug("添加成功!");
resetToken(request);
return mapping.findForward("success");
}
}
[/quote]
struts-config.xml:
[quote]
<?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>
<form-beans>
<form-bean name="testActionForm" type="test.TestForm"/>
</form-beans>
<action-mappings>
<action name="testActionForm" path="/testAction" type="test.TestAction" scope="request" input="/index.jsp" validate="false">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
</struts-config>
[/quote]
编写完上边的代码后:[url]http://localhost:8080/test/testAction.do[/url]可以进行测试!