利用Token解决重复提交(4)

7.编写注册提交处理Action,RegisterHandleAction.java:

package test.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import test.form.RegisterForm;

public class RegisterHandleAction extends Action {

@Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  ActionMessages errors = new ActionMessages();
  if(!isTokenValid(request)) {
   errors.add(ActionMessages.GLOBAL_MESSAGE,
     new ActionMessage("error.invalid.token"));
   saveErrors(request, errors);
   saveToken(request);
   return (new ActionForward(mapping.getInput()));
  } else {
   resetToken(request);
  }
  request.setAttribute("username", ((RegisterForm)form).getUsername());
  // insert the record to database
  // .....
  return mapping.findForward("success");
 }

}

8.添加错误信息提示的资源文件application.properties:

error.invalid.token=<i>不能重复提交表单!</i>

9.编写Struts的配置文件struts-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
      "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans>
      <form-bean type="test.form.RegisterForm" />
  </form-beans>

<global-forwards>
      <forward path="register.do" />
  </global-forwards>
 
  <action-mappings>
        <action path="/register"
            type="test.action.PrepareRegisterAction"
            scope="request" >
          <forward path="/WEB-INF/register.jsp" />
        </action>
           
        <action path="/registerHandle"
            type="test.action.RegisterHandleAction"
            scope="request"
            validate="true" input="/register.do">
          <forward path="/WEB-INF/success.jsp" />
        </action>
  </action-mappings>
 
  <message-resources parameter="application" /> 
</struts-config>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/9c2f5bd8b61133c2713707033e6928c4.html