Practice Struts token to prevent retreating to repeated submission form

xiaoxiao2021-03-06  47

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 (MessageLocator.getMessage ( "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 the exception class: public class BusinessExceptionHandler 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 exception display on the JSP page:
< / Logic: Present>

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-64901.html

New Post(0)