Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL and basic String methods  RSS feed

 
James Ellis
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP page which is printing the value of a JSP variable to a javascript method argument. I need to escape single quotes, and using scripting I would just do the following:



How can I accomplish the same thing assuming I have a JSTL String variable as in below:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using JSP 2.0 and JSTL 1.1 perhaps some of the builtin string functions could be of help.

If you are using JSP 1.x, you may need to resort to scripting, a custom tag, or escaping the string prior to sending it to the page.

What's your environment?
 
James Ellis
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomcat 5.0, so probably JSP 2.0. How do you call a built in string function from JSTL?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JSTL 1.1 provides a tag library that exports EL functions. It is customarily mapped to the prefix 'fn'. Most of these functions are wrappers around the methods of the String class.

For example:



To enable these functions, use the following directive on the page:



Full descriptions of these functions is available in the JSTL 1.1 Specification.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. You might be particularly interested in the replace() function.

P.P.S. You can also write your own such functions. Hint hint. Perhaps one named something along the lines of escapeJS()?
 
James Ellis
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to give everyone the solution, this is how I did it.



Where export_soure_title was a page scoped string in JSTL.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!