aspose file tools*
The moose likes Struts and the fly likes how to pass a string and variable as value on a tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to pass a string and variable as value on a tag?" Watch "how to pass a string and variable as value on a tag?" New topic
Author

how to pass a string and variable as value on a tag?

Klament J. Kruoghst
Ranch Hand

Joined: Oct 24, 2008
Posts: 37
    
    1
hi there,

can someone please tell me why this is not working in JSP?: <bean:message key="col.<%=rowNum%>"/> i'm trying to get the rowNum dynamically, if I hardcode it it works i.e key="col.row2" so
also if I print <%=rowNum%> on its own it works so I know the variable is valid, but the combination of the string and the variable isn't working, how do I fix this?

I've also tried:
"col."<%=rowNum%> and
key=col."<%=rowNum%>"

No luck.

Thanks for the help in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

When using discredited scriptlet expressions in a custom tag value, the expression must be the entire value.

Scriptlets have been discredited for almost 10 years now. Time to update your JSP knowledge and use the EL and JSTL instead. EL expressions, by the way, do not have this attribute value limitation.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Klament J. Kruoghst
Ranch Hand

Joined: Oct 24, 2008
Posts: 37
    
    1
Hi Bear thanks for your comment.

Would that just be instead of using <%= %> use ${}? so the expression would become: <bean:message key="col.${rowNum}"/>? i tried that before but it doesn't work.
I even tried just printing ${rowNum} and it doesn't print anything while doing <%=rowNum%> does work, so that's why i'm sticking with it.

Would you be able to help me either make it right using EL/JSTL or just how to re-write the expression with scriplet? by the way we use java 1.4

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

You can't just replace scriptlet expressions with the EL if the rest of the page is still using scriptlets. For example, rownum is a scripting variable set by a scriptlet elsewhere on the page, right? That has to go as well. You really need to get up to speed on proper web app structure (using servlet controllers to do the data processing) as well as the JSTL and EL.

Meanwhile, you can try something like: to get around the current problem.

But, please, for your own sake, move away from 2001-style code and catch up to 2011!
Klament J. Kruoghst
Ranch Hand

Joined: Oct 24, 2008
Posts: 37
    
    1
I'd definitely love to be coding with that if that's best practice but the whole application code is written using scriplets, I can't start mixing it up so please bear with me using something so old

I did the change and here's my current situation:
1) <h1><bean:message key="<%= "col." +rowNum %>"/></h1>
is printing nothing and this is what i see in the html source code: <h1><bean:message key="col.row2"/></h1>

That same line in my JSP (without using variable):
2) <h1><bean:message key="row1.col2"/></h1>
does get evaluated and prints the value I want.

Therefore it looks like (1) is not being evaluated, how can that be corrected?

Thanks in advance.

EDITED: I fixed it by declaring somewhere else the

I dislike it but if there's no choice then i have to split it.
Thanks bear for your help on this.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

As <bean:message> is not a standard JSP tag, this has been moved to the Struts forum to determine why it's behaving badly.
 
wood burning stoves
 
subject: how to pass a string and variable as value on a tag?