• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using javascript as attribute  RSS feed

 
Matt Sall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,

is it possible to use javascript as an attribute value in a JSTL tag, e.g.



so that the pattern can be determined at run-time? Any other way?

Thanks.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Other way is to do it in Java.

What does the Javascript´s getPattern() do? You should undoubtely be able to do the same thing in Java.
 
Matt Sall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The javascript would return a different pattern depending on the value, something like


I don't see how I would do it in the (struts) action class.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use an array, or a List or a Map and use the brace notation.

Before giving an example, what does 'val' represent and where does it come from?
 
Matt Sall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
val is a string representing a BigDecimal. I want to be able to use different formatting for negative and positive values, as well as for null.
val is a property of a form bean in request scope.

Thanks.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, if the 'val' already is in the bean, then just do
and use ${bean.pattern} instead.
 
Matt Sall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, that is a possible solution, however, I have many form beans (in a list) that I iterate through on my jsp-page. It would be nice having it on the presentation layer where I think formatting belongs. But if there is no such solution I might have to go with this.

Thanks.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on the information which you have given as far, this is the best solution. If there is more into the picture, you need to elaborate about it instead of hiding it. You can, as said before, just use an array/list/map in combination with the brace notation or create an EL function and so on.
 
Matt Sall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, well if you could show me how to prepopulate a map with patterns, which are initparams, it would be great. That is, the patterns are retrieved on the jsp with initParam['theFirstPattern'] and so on. So based on the value the fmt:formatNumber would use the correct pattern with javascript, if it's possible, that is.

I know I can use c:if or c:choose to get the right pattern, but the code duplication would be huge and not very pretty.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some small self-explaining examples:
and

roughly resolves to bean.getMap().get("key") and

roughly resolves to bean.getMap().get(otherbean.getKey()).
 
Matt Sall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is solved, I use a bean that hold the patterns along with some logic to retrieve the correct pattern in the way you described.

Thank you. I noticed you replied on Sun's forum, if you post your reply there I will award you the duke points.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You´re welcome.

Though in the future you shouldn´t crosspost without notification of a valid reason.
Read this: http://faq.javaranch.com/java/BeForthrightWhenCrossPostingToOtherSites
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!