The Token mechanisms to address with the Struts form to submit duplicate

sponsored links
Struts-Token (token) mechanism can be properly resolved to submit the form to repeat the question, the basic principle is: the server-side in dealing with requests arrive before the request will be included in the token value stored in the current user session in the Token value is compared to see if it matches. After processing the request, and in reply sent to the client before, it will generate a new token, which is passed to the client other than the addition will be saved in the user's session token to replace the old. So, if you just fall back to the submission of a user page and resubmit, then pass over the token the client and server-side token on the inconsistent, thus effectively prevent the occurrence of duplicate submission.

In fact, that is, when two points, first: You need to have the token in the request value, the value of the token in the request how to save, in fact, on the page, and we can usually save some of the information is the same, through hidden fields to save, save the form, such as: <input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="6aa35341f25184fd996c4c918255c3ae">, this value is TokenProcessor class generateToken () to obtain, is based on the current user's session id and the current time, long value to be calculated. Second: after the submission of the client, we have to judge according to the values contained in the request and the server is the same token, because the server each time you submit will generate a new Token, so, if it is repeated submission of the client Token value and Token server-side value will be inconsistent. Here's to insert a data in the database to illustrate how to prevent duplication of submission.

In the Action in the add method, we need to clear requirements Token value stored in the page, simply adding a statement: saveToken (request);, as follows:

public ActionForward add (ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

/ / In front of the processing is omitted saveToken (request);

return mapping.findForward ( "add");

) In Action of the insert method, we have according to the form of the Token value and the server side of the Token value, as follows:

public ActionForward insert (ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

if (isTokenValid (request, true)) (

/ / Form is not a duplicate submission / / Here is the code to save the data) else (

/ / Form submitted to repeat saveToken (request);

/ / Other processing code)

)

In fact, very simple to use, give the simplest, most need to use this example:

Repeat to submit general control of the database is mainly used in control operations, such as insert, update, delete and so on, due to update, delete are normally filled by id to operate (for example: updateXXXById, removeXXXById), so the significance of such operational control is not high (do not rule out isolated cases), repeat control also presented mainly in the insertion of the control.

Let me talk about what we are currently doing the project situation:

The current project is to use Struts + Spring + Ibatis, pages with jstl, Struts complex View layer, Spring in the Service layer provides transaction control, Ibatis is used instead of JDBC, to access all the pages are not direct access to the jsp, but a visit to Structs in Action, by the Action to Forward to a Jsp, all against the database operations, such as taking data or modify data, are in the Action which was completed, all of the Action generally inherit BaseDispatchAction, this is the establishment of their own class aims to do for all of the Action unified control, in the Struts layer, for a function, we are generally divided into two Action, an Action inside the function is called Struts validation functionality does not require the (common method names add, edit, remove, view, list) Another is the need to call the Struts validation capabilities (common method names insert, update).

Take the post on the forum for it, post on the forum first need to jump to a page, you can fill in the subject and content of the post, fill out, click "Submit", post on the published, so here, after two steps:

1, go to a new page, in Action where we are commonly referred to as add, for example:

public ActionForward add (ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception (

/ / This one is the output of debugging message that the code execution to this section of the log.debug ( ":: action - subject add");

/ / your code here

/ / Here to save the Token value saveToken (request);

/ / Jump to add page, Structs-config.xml inside the definition, for example, jump to subjectAdd.jsp

return mapping.findForward ( "add");

)

2, in completing the title and contents, choose to submit, will be submitted to the insert method, in the insert method of years to determine whether the repeated submitted.

public ActionForward insert (ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) (

if (isTokenValid (request, true)) (

/ / Form is not a duplicate submission / / Here is the code to save the data) else (

/ / Form submitted to repeat saveToken (request);

/ / Other processing code)

)

A little more detail below (note that all of the following code to make use of angle brackets):

1, you want to Posted, click on the "I want Posted" link inside the code can look like this:

I would like to <html:link action="subject.do?method=add"> Posted </ html: link>

subject.do and the method these struct-config.xml I will not say how to define, and click on the link, will implement the add method subject.do code as above said, the jump to subjectAdd.jsp page. The code page is probably as follows:

<html:form Action="subjectForm.do?method=insert">

<html:text Property="title" />

<html:textarea Property="content" />

<html:submit Property=" Published " />

<html:reset Property=" Refilling " />

<html:form>

If you add a method of Riga "saveToken (request);" This is one, then the resulting page in the subjectAdd.jsp will be more than a hidden field, similar to this <input type = "hidden" name = "org.apache . struts.taglib.html.TOKEN "value =" 6aa35341f25184fd996c4c918255c3ae ">,

2, click after the form submitted to the subjectForm.do inside the insert method, you insert method of years to form the data into a database, if not repeat the submission of the control, then every click of a browser Refresh button, will be the same in the database to insert a record, add the following code, you can control the user's repeated submitted.

if (isTokenValid (request, true)) (

/ / Form is not a duplicate submission / / Here is the code to save the data) else (

/ / Form submitted to repeat saveToken (request);

/ / Other processing code)

Note that you must use the add method inside saveToken (request), you will be able to determine where in the insert, otherwise the save operation is repeated each time you submit.

Keep in mind that, Struts in a time when every time you visit the Action will produce a token, stored in your Session which, if you're inside a function inside the Action, using saveToken (request);, then the token will be stored in this Action by the Forward to the jsp static pages generated inside.

If you're on your way to where the use of Action of the isTokenValid, then Struts will forward your request from your inside to get the token value, and then, and the value of Session where the token comparison, if the two are equal, they will not repeat the submission, If not equal, is to repeat the submission.

Action due to all of our projects are inherited from the BaseDispatchAction this class, so we are basically in this class which has done a form submitted in duplicate control, the default is to control the add method and insert methods, if the need to control the other way, the his hand to write the above code, otherwise, does not require hand-written, the control of the code is as follows:

public abstract class BaseDispatchAction extends BaseAction (

protected ActionForward perform (ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception (

String parameter = mapping.getParameter ();

String name = request.getParameter (parameter);

if (null == name) (/ / If you do not specify a method, then the default list

name = "list";

)

if ( "add". equals (name)) (

if ( "add". equals (name)) (

saveToken (request);

)

) Else if ( "insert". Equals (name)) (

if (! isTokenValid (request, true)) (

resetToken (request);

saveError (request, new ActionMessage ( "error.repeatSubmit"));

log.error ( "duplicate submission!");

return mapping.findForward ( "error");

)

)

return dispatchMethod2 (mapping, form, request, response, name);

)

)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of The Token mechanisms to address with the Struts form to submit duplicate

  • hibernate call stored procedure

    hibernate call stored procedure

  • Hibernate pessimistic locking mechanism for locking and optimistic locking

    hibernate lock mechanism 1. Pessimistic lock It refers to the modification of data by outsiders hold a conservative attitude. The assumption that at any time access to data, may also have another client to access the same data, in order to maintain t ...

  • hibernate using c3p0 connection pooling

    Private http://www.lifevv.com/tenyo/doc/20070605102040991.html c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is v ...

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries