Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing String from struts to javascript

 
Tankred Smult
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

I'm trying to pass a string from my jsp page to javascript, to make a javascript popup with a printerfriendly edition of my page. But I'm having a problem when the string contains a newline('\n'). It totally messes up my javascript:

win1.document.write('<nested:write property="answer"/></td>');

As I said, this works like a dream when there's no \n in "answer". I was hoping there was a way to strip out the \n's from the property. I've tried the filter option, hoping that it would strip out the \n, or even maybe replace them with <br/>s.

Do anyone know how to remove the \n's?

.tankred
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about doing that in your getAnswer() method?
 
Tankred Smult
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, not really. You see, I use the same formbean to extract the data, and in other cases I would like the \n to be included. It is just in the special case where I use it to generate a javascript string that I would like to replace it. So, if there were some kind of filer I could create and use, that would replace the \n's, that would be the best solution. Is that possible?

.tankred
 
Tankred Smult
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

Just wanted you to know I hacked my way around it.
I put the field in html as a hidden field:

and then get the value in javascript by reading that property:

It's what we in norway would call a "grisehack" (pighack), but it seems to work.

Still, if anyone know a more elegant solution, please let me know.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic