• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble using JSP expression inside a custom action attribute

 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I used an expression inside a JavaScript call, it does not print the value of the constant, but rather the tag itself.

The constant class

public final class DSConstants {

final static public String INDEXJOIN = "join";

}

Import it into a JSP

& lt;%@ page language="java" import="ds.constants.DSConstants" & lt;


Try to use it in a JavaScript call

& lt; html:submit onclick1="setUserEvent('& lt;%=DSConstants.INDEXJOIN%& lt;')/& lt;
[ February 14, 2007: Message edited by: Bear Bibeault ]
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. I really detest having to deal with the problem that you describe. I've only found one good solution:

instead of:


I'll often decompose into two lines, like this:


Anybody know a cleaner solution?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Wingate:



Be aware that by using the <%! %> construct, you can easily make your JSP non-thread safe. In the case of a constant expression that will be the same for all threads, it's ok. But proceeed with caution.

And it is true that when supplying a scriplet expression as the value of a custom action attribute that the scriptlet expression must comprise the whole attribute, not just part of it.

This is not a constraint when using the EL in lieu of scriplets.

[ February 14, 2007: Message edited by: Bear Bibeault ]
[ February 14, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Burke, as you can clearly see, your code is mangled and full of &lt; and such that make it hard to read. You will get more responses if your code is readable.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. This has nothing to do with the fact that the attribute value is Javascript. it is a factor of using a custom action. I have changed the title of your post accordingly.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for observing that <%! %> is non-thread safe ... you seem to be very concerned about that fact.

Can you help me understand why I should care about thread safety for a variable whose value I don't plan to update (only read)? I don't see how there's a race condition that I should be concerned about?
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and, also, it's good to know that EL is a better way to solve the problem.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The declaration issue is a sore spot with me as gives rise to so many hard-to-diagnose issues. But yes, in the case of a constant value, it's safe. With all the markup issues in the original post, I missed the fact that it was a constant.

I still recommend against using scriplets at all unless one is stuck in the JSP 1.x world.

I have amended my reply to be a little less strident and more cautionary. Thanks for catching that.
 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
M Burke, as you can clearly see, your code is mangled and full of &lt; and such that make it hard to read. You will get more responses if your code is readable.


The forum was blocking my tags.
 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


Be aware that by using the <%! %> construct, you can easily make your JSP non-thread safe. In the case of a constant expression that will be the same for all threads, it's ok. But proceeed with caution.

And it is true that when supplying a scriplet expression as the value of a custom action attribute that the scriptlet expression must comprise the whole attribute, not just part of it.

This is not a constraint when using the EL in lieu of scriplets.

[ February 14, 2007: Message edited by: Bear Bibeault ]

[ February 14, 2007: Message edited by: Bear Bibeault ]


I tried using EL but the results where blank

<html:submit onclick1="setUserEvent('${DSConstants.INDEXJOIN}')"/>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use the EL directly on class constants. You'll either need to store the contstant vlaue as a scoped variable, or use one of the techniques described in this article.
 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear,
Not being able to use constants directly in a jsp is a major drawback.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by M Burke:
Thanks Bear,
Not being able to use constants directly in a jsp is a major drawback.


Yup, which is why I wrote that article and developed the constantsMap tag. I use it a lot.

P.S. I've been thinking about doing something similar for 1.5 enums, but haven't had any time to dig into it.
[ February 14, 2007: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!