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

TOKEN not working in struts

Hi experts:
I am trying to avoid resubmit when user clicks back button or refresh button. After a rigorous search I found that Struts has TOKENS mechanism to acheive this. I tried everything and TOKENs don't want to work with me. I am sure I am missing something.

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
0
raj3060
Asked:
raj3060
  • 7
  • 7
  • 2
2 Solutions
 
suprapto45Commented:
Hi,

Can you post your codes? or error messages?

Regarsd
Dave
0
 
suprapto45Commented:
Hi,

You may have looked at this but I just let you know anyway.

http://www.javaworld.com/javaworld/javatips/jw-javatip136.html

Regards
Dave
0
 
boonlengCommented:
You codes should be similar to below, right? Did you see the hidden field for the token after struts render the form?

ActionClass1
------------------------------------------
saveToken(request);


ActionClass2
------------------------------------------
if (isTokenValid(request)) {
    resetToken(request);
    return mapping.findForward("success");
} else {
    return mapping.findForward("fail");
}

JSP
------------------------------------------
<form ..>
<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="...">
0
Technology Partners: 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!

 
raj3060Author Commented:
suprapto45:
I don't have any error. it's just I am getting isTokenValid(request) FALSE all the time. That means when I saveToken struts is not doing what it is supposed to do.

Here is what I am doing:
in my menuAction class:

String link = request.getParameter("screen").trim();
      if(link.equals("manage")){
            saveToken(request);                        
            forward=forward = mapping.findForward("admin");
      }

in adminAction:

if (isTokenValid(request)) {
//processing request........

resetToken(request);
    return mapping.findForward("success");
} else {
    return mapping.findForward("duplicate");
}

boonleng:

how do I check if struts is creating hidden field:
<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="...">

or I have to do it myself??

I am not seeing it..


      
0
 
raj3060Author Commented:
Typo:

forward=forward = mapping.findForward("admin");
 should be
forward = mapping.findForward("admin");
0
 
suprapto45Commented:
Hi,

"or I have to do it myself??"
You have to do it manually in your JSP page.

Regards
Dave
0
 
boonlengCommented:
Struts will automatically render the <html:form> to include the hidden field for the token when the action called the method saveToke().

Regards,
Boon Leng

0
 
raj3060Author Commented:
OK I am confused now:
suprapto45:

>>You have to do it manually in your JSP page.

boonleng:

>>Struts will automatically render the <html:form> to include the hidden field for the token when the action called the method saveToke().

That's what I thought that struts does it automatically....

boonleng
>>Struts will automatically render the <html:form>

can you explain what does that mean, and also I have given above what I am doing, do you see any error or anything missing there..

If possible give me one working example and let me see where I am wrong. Just create one JSP with one field and formClass, and actionClass with tokens...

thanks in advance
0
 
suprapto45Commented:
Hi,

boonleng may be correct.....let me check again. I was not in front of my machine when I was answering your question....I am sorry raj3060 and boon leng.

Regards
Dave
0
 
suprapto45Commented:
Hi,

Yes, boonleng is right, the hidden field will be transparent to user. It will be done automatically in the background. Can you tell me the flow on how the menuAction is related to AdminAction? i.e. JSP -> menuAction -> AdminAction or etc

Regards
Dave
0
 
raj3060Author Commented:
menuAction is not related to the AdminAction..
menuAction is Action class for navigation menu. When I click on the Administrator link in Menu then menuAction class is called and it is saving token and forwarding to the admin.jsp. I think that's where I am doing something wrong. But I have tried to call AdminAction class from menu instead of menuAction and then saveToken and then forward to admin.jsp.
But both cases not working. I am lost, I don't know where I am going wrong. This is giving me headache.....
Suggestions.

Thanks
0
 
raj3060Author Commented:
OK TOKEn is being rendered in JSP as hidden field...
Problem is that ActionClass is not seeing it...
What could be the problem?

This is what I am getting in JSP..
<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="7fcc4066e52fc3b151c64ba2c78dc30f">

I tried this in ActionClass:

System.out.println("TOKEN: "+request.getParameter("org.apache.struts.taglib.html.TOKEN"));

Which is giving me
TOKEN: null
0
 
raj3060Author Commented:
I found the problem:

org.apache.struts.taglib.html.Constants.TOKEN_KEY  is always null, i.e. token is not being passed with request..

Suggestions..
0
 
suprapto45Commented:
Mmm........

So in your AdminAction, you will always get org.apache.struts.taglib.html.Constants.TOKEN_KEY  to be null. How do you call the AdminAction from your admin.jsp? Is it within the form tag like <form action="AdminAction.do" >?

Regards
Dave
0
 
raj3060Author Commented:
I found the solution...
Problem was that I was calling the action class from a link, and because form was not being submitted it was not passing any form data in request. What a mess....

I struggled whole weekend grrrrrr................ Anyway I thank you all for your patience and help....

0
 
suprapto45Commented:
Hi,

Glad I could help.

Regards
Dave
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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