• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

in struts how can i use Token to avoid submit again???

hi,experts,i use struts

in myAction:

                         mymethod(){
                                insert some date to my database....
            request.setAttribute("mydata",Myclass.getValue());
                          }
            return (mapping.findForward("success"));

and in struts-config:
                         <forward name="success"        path="/printItem.jsp"/>

and in printItem.jsp:
             <html>
                    <body>
                             ${mydata}
        .....
                    </body>
                       
            </html>

when i press F5 and reflush the printItem.jsp but it will do mymethod(),how can i avoid submit again? and when press F5 only to reflush printItem.jsp but not do mymethod();


thanks!
0
hgbdelphi
Asked:
hgbdelphi
1 Solution
 
hgbdelphiAuthor Commented:
i not use struts's Tag,and i use JSTL Tag!

  my form like:

<form action="/track/printItem.do?key=${param.key}" name="editItemForm" method="post">

</form>


not <html:form></html>
0
 
hgbdelphiAuthor Commented:
in struts-config,if i set redirect="true"
                         <forward name="success"        path="/printItem.jsp" redirect="true"/>

i can not get request.getAttribute("mydata");


0
 
aozarovCommented:
>> how can i avoid submit again?
http://www.onjava.com/lpt/a/3350
Basically you need to "remember" that it was submited. There are several tricks (like associate a generated key with each form/request)
then the users session can see if such key was already seen.

>> and when press F5 only to reflush printItem.jsp but not do mymethod();
>>   <forward name="success"        path="/printItem.jsp" redirect="true"/>
because you do forwared redirect="true" the refresh will go directly to the jsp and not to
the action (which actually populates the mydata attribute). In that case you want to change it to redirect="false".
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
aozarovCommented:
Typo: then the users session can see if such key was already seen.
Correct: then you can use the session if such key was already seen.
0
 
petmagdyCommented:
Hi hgbdelphi,

take a look at this article:

http://www.theserverside.com/articles/article.tss?l=RedirectAfterPost

Cheers!
0
 
hgbdelphiAuthor Commented:
i want to request.getAttribute("mydata") and not do mymethod() method when press F5

can experts give me some code.
0
 
aozarovCommented:
>> i want to request.getAttribute("mydata") and not do mymethod() method when press F5
your "mymethod()" is responsible for putting the "mydata" value in the request.
How do you want to get the value without putting it?
You can put it in the session instead of the request and then the next time it will still be available.
In that case you will get the value this way: session.getAttribute("mydata")
0
 
hgbdelphiAuthor Commented:
sorry,i not say clear

in myAction:

    public ActionForward execute(...){

                     mymethod(){
                                insert some date to my database....
                                request.setAttribute("mydata",Myclass.getValue());
                          }
                         return (mapping.findForward("success"));

}

and in struts-config:
                         <forward name="success"        path="/printItem.jsp"/>

when i do this request from web,it will do execute(....)method,and will go " printItem.jsp" jsp page,but the address text is always "printItem.do",so when i press F5,it will submit again(execute execute(....)method),and will do mymethod()again.
if set
 <forward name="success"        path="/printItem.jsp" redirect="true"/>
after do mymethod() method,it will location "printItem.jsp" and the ie's address text is "printItem.jsp" ,so when i press F5,it will not do public ActionForward execute(...){} method,but i can not request.getAttribute("mydata");

how can i avoid "Handling Multiple Submits" with not use <html:form></html> and Token??
0
 
aozarovCommented:
Put the data for mydata in the session (instead of hte request):
In the Action change: request.getSession().setAttribute("mydata",Myclass.getValue());
In the Jsp change: session.getAttribute("mydata")
and keep <forward name="success"  path="/printItem.jsp" redirect="true"/>
This way every refresh will go directly to printItem.jsp (instead of going to the action) and because mydata
is in the HttpSession it should be found by the jsp.
0
 
hgbdelphiAuthor Commented:
wo.....................can give me some another advice.
thanks!
0
 
aozarovCommented:
Not sure what you are looking for, but why won't you try it (it is only 2 lines of change [one in action, one in jsp])
0
 
hgbdelphiAuthor Commented:
thanks all,
  i write some tags and util to simulate the struts's Token,it can work fine.

0
 
aozarovCommented:
Nice :-)
0
 
raj3060Commented:
hgbdelphi,
I am running the same problem. I tried everything. TOKENS not working for me. Can you tell me how you acheived this.

Tell me I will open new question to give you points.

Here is what I tried to do:

when I click on the link on the menu:
I go to the ActionClass1, I saveToken before forwarding to the JSP page. Now in this JSP when I submit the form, I go to ActionClass2 I check if TOKEN is valid, and everytime I get invalid token... I am missing something somewhere...

Please help

Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now