wood burning stoves 2.0*
The moose likes Struts and the fly likes [Struts 2] Use a JSP variable in struts2 tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "[Struts 2] Use a JSP variable in struts2 tag" Watch "[Struts 2] Use a JSP variable in struts2 tag" New topic
Author

[Struts 2] Use a JSP variable in struts2 tag

Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
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


SCJP 5
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
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

Joined: Oct 27, 2008
Posts: 90
It doesn't work
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
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

Joined: Oct 27, 2008
Posts: 90
Not yet
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
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

Joined: Oct 27, 2008
Posts: 90
it still doesn't work

Are you sure that it's the correct way to retrieve the value?
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Struts 2] Use a JSP variable in struts2 tag