aspose file tools*
The moose likes Struts and the fly likes Struts2 variable inside javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 variable inside javascript" Watch "Struts2 variable inside javascript" New topic
Author

Struts2 variable inside javascript

Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

hi all,

I have a url coming from my action to the jsp, 'm getting the url in my jsp.

i have to open a pop up and have to pass that url to window.open() in javascript, but the problem is my url contains special characters which i need to rewrite with escape sequence character "\",
Q) How do i get the struts2 variable which i have set as <s:set name="myVar" value="strUrl"/> (strUrl containing special characters) to the javascript to append the escape character?

thanks in advance!!


Happy Developing.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

OGNL allows arbitrary Java in the "value" attribute expression--just call whatever escaping library method you want to use.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

no david, if my URL is like this -- javaranch)123 when i try to do this



the link is rendering something like href="redirect(') " and giving an js error.
the same is happenin, when i pass that to escape() function with the url (with special chars)...

how to pass the
string to a javascript ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You have to disable HTML in order to display HTML, so I still have no idea what isn't working.

That aside, I already told you--if you're trying to create a string usable by JavaScript you use an OGNL expression in the "value" attribute to escape the Java string. One way would be to use commons-lang's "escapeJavaScript" method thusly:

Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

THANKS !! SIR it worked fine. "value" i had missed !! Thanks again.
sankey jain
Ranch Hand

Joined: Apr 29, 2010
Posts: 58
Robin John wrote:THANKS !! SIR it worked fine. "value" i had missed !! Thanks again.


hi Robin ,

I am having a similar error while using struts tag in javascript.


will the above solution apply to this ?
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Hey Sankey...

I think it should work.

You can also set the variable first and then use it in your script using ognl.



I'll not be able to confirm the above as I can't test...
sankey jain
Ranch Hand

Joined: Apr 29, 2010
Posts: 58
Robin John wrote:Hey Sankey...

I think it should work.

You can also set the variable first and then use it in your script using ognl.



I'll not be able to confirm the above as I can't test...


ok here is the funny thing.
i used the same thing on the property, and now i dont see the browser error.
Now, i want to see how the value looks like and i cant find the freakin code in the firebug.
it s got 12000 line of script.

running in loops

Nightmare !!

Anwyay.. think it worked..Thanks a ton
chets patel
Ranch Hand

Joined: Apr 27, 2010
Posts: 77

can you please post your full jsp and javascript code. I am facing same issue and not able to solve.

Thanks in advance.
sankey jain
Ranch Hand

Joined: Apr 29, 2010
Posts: 58
This is the Total Jsp code. Use the function, it should solve it

chets patel wrote:can you please post your full jsp and javascript code. I am facing same issue and not able to solve.

Thanks in advance.
 
 
subject: Struts2 variable inside javascript