Practice Struts token to prevent retreating to repeated submission form

xiaoxiao2021-03-06  39

Using Google, find one or two related articles, practice success.

Built an action base class tokenaction, use the template method, as follows:

public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {ActionForward forward = null; if (isTokenValid (request, true)) {forward = doExecute (mapping, form, request, response);} else { SaveToken (Request); throw new tokenrepeatexception ("Error.Token");} Return Forward;

Public Abstract ActionForward Doexecute (ActionMApping Mapping, Actionform Form, HttpservletRequest Request, HttpservletResponse Response) THROWS Exception

Note: TokenRepeTexception is a custom exception, its parent class ApplicationException is declared in Struts-Config.xml to capture the Action. As (my error message resource is used by Spring, there is no Struts):

.....

Then, observe this exception handling class:

Public class business exceptionhandler extends ExceptionHandler {

public ActionForward execute (Exception exception, ExceptionConfig config, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {request.setAttribute ( "exception", exception); if (exception instanceof TokenRepeatException) {return mapping.findForward ( "tokenRepeat ");} Else // Display Error Info in Action Come from Return Mapping.getInputForward ();}}

There are several precautions: 1. Regarding the abnormal information Where is the display: Anomaly display certainly can't be on the form page, for example, after you have successfully updated a record, return the list of list, when the user returns repeatedly, Returning this list page and displays the prompt information is reasonable. It is best to define another Forward. I unified to the Forward name "tokenrepet", each need to control the Action that needs to be submitted must be defined. Finally, add abnormal display on the JSP page:


2. You must forward instead of directly enter the JSP page, because you have to call the SaveToken method to record the token word.

转载请注明原文地址:https://www.9cbs.com/read-70221.html

New Post(0)