Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Struts 2] Use a JSP variable in struts2 tag

 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I wonder how can I access to a JSP variable in struts2 tag, for example:
I have a Java class with constants and I import it in my jsp


I would like to display a constant value included into the Sector class with a Struts 2 tag and not a JSP tag like "<%=Sector.name%>"
I need to compare a variable with the constant in the Sector class by using the <s:if> tag. Apparantly Struts 2 doesn't allow JSP expressions.

I tried :



but it doesn't work I got an exception.


Thanks in advance
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can access static values from a class in OGNL using the @ sign.

so I think this should work:

<s:if test="mySector = @xxx.Sector.name">

I believe the fully qualified package name is required for this.

Hope it helps.
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, bad syntax on my part, you need the @ befor the class and before thename of the static member of the class (and a double = as well).

<s:if test="mySector == @xxx.Sector@name">

 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not yet
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange, Are you using the fully qualified package name? Is "name" a public static member of that class? if you put a



in your JSP, does it write teh value to the screen?
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it still doesn't work

Are you sure that it's the correct way to retrieve the value?
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I'm sure this works, I do this all the time. I don't see answers to he questions I asked. They might help track down the problem. Also, the code for the tag you are using, and the declaration of the variable and the package and name of the class its in would help me verify if the tag is correct.

The OGNL reference site that is in the struts2 documentation appears to be gone, but this address looks like a copy of what used to be there:

webpage

There are pages for calling static methods and getting static fields. They are a little sparce but they might help.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic