This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Struts and the fly likes problem calling static method using OGNL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "problem calling static method using OGNL" Watch "problem calling static method using OGNL" New topic
Author

problem calling static method using OGNL

shukla raghav
Ranch Hand

Joined: Aug 03, 2008
Posts: 200
here is the flow of my test application

- User logs in by entering usrname and password -

the action class that is invoked is User.java
the result jsp for login success is RegisterStudent.jsp - this registers a pre-existing user for a new batch where in the user has to enter his personal details and select his batch ID using a drop down menu(a select tag). the list attributed of the select tag has to be populated using a static method from a Class called Batch. but since the object of the batch class has not been set on the value stack i am unable to access the static method that populates the select tag. here is the code for RegisterStudent.jsp




type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: tag 'select', field 'list', name '': The requested list key '%{@administration.Batch@getBatchList}' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]


what could be the problem ? is it a problem with my OGNL that i have used why is the struts unable to resolve the name
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you're calling a method, use method syntax, like ".getBatchList()". If it's a simple getter with no args, use property syntax, like ".batchList".

Make sure you're configured to allow static method access.
shukla raghav
Ranch Hand

Joined: Aug 03, 2008
Posts: 200
I a really sorry Mr David, i think i have not explained my question properly i would like to clear it more

here i am pasting a segment of my struts.xml



User_Login is the starting action i.e. User_Login action is created from the index.jsp. If the login is successfull then RegisterStudent.jsp is sent back to the browser.

The problem is RegisterStudent.jsp has a <s:select> tag named batchID (here the student selects his current batch) whose list must be populated using a method getBatchList() from class Batch. now given below is the tag



we can see that i am trying to populate the list using %{@administration.Batch@getBatchList()}. But my mistake is that i am trying to call the getBatchList method from Batch class using OGNL although the Batch class object does not exist on the value stack. Since the batch class is not mapped to any action so it cannot be registered in the struts.xml.

After a little thought i think i have following options

1. using the <s:bean> tag, i should modify my Batch class in this manner



i set this Batch bean to value stack using <s:bean>, and then access the bean. if the bean set to the value stack using <s:bean> is it possible for me to access its static getBatchList() method using OGNL like this

%{@administrator.Batch@getBatchList()}

secondly as you said
Make sure you're configured to allow static method access.


how do i know if i am configured to access static methods.

2. Using <s:set> tag

i can set the Batch object to the servletcontext and access the method getBatchList()

which one would be the correct way of doing this so that i can get the batch list and use it in my <s:select> tag to populate the options
shukla raghav
Ranch Hand

Joined: Aug 03, 2008
Posts: 200
Thirdly i wanted to ask is it possible to map one action to more than one action classes

like

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

shukla raghav wrote:Thirdly i wanted to ask is it possible to map one action to more than one action classes

Action mappings map a URL to the class that handles them--it makes no sense to handle a single URL request with multiple classes.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you're calling a static method the class the method is in doesn't need to be on the stack:

http://struts.apache.org/2.x/docs/ognl-basics.html#OGNLBasics-Accessingstaticproperties
rajduvvi reddy
Greenhorn

Joined: Apr 18, 2010
Posts: 13
HI, to access the static methods , we have to set the constant "struts.ognl.allowStaticMethodAccess" to true in struts.xml/strutsproperties.xml

See the below snippet

<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>

because be default in struts2 it this constant value is false.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(As already stated in the link I provided, but thanks.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem calling static method using OGNL