aspose file tools*
The moose likes Struts and the fly likes How to get subclass objects in an arraylist? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to get subclass objects in an arraylist?" Watch "How to get subclass objects in an arraylist?" New topic
Author

How to get subclass objects in an arraylist?

Sunil Kumar Pissaye
Greenhorn

Joined: Apr 22, 2009
Posts: 7
Howdy fellow ranchers,

My subject line may not be close to the problem.... I will go straight into code so i can explain my problem clearly...

I have an action class which looks like this:

Please note, I have left out all the getter and setter methods....




When i run this, I get an error which says
ERROR: ParametersInterceptor - [setParameters]: Unexpected Exception caught setting questions[0].blankAnswer' on 'class com.mtg.action.TestAction: Error setting expression questions[0].answer' with value '[Ljava.lang.String;@2c06b2'

The questions in the jsp page are being added using javascript and each question can be of a different type.. for example, first question is FillBlankQuestion, the next one can be EssayQuestion (which also extends Question)..

Now, I know that the arraylist has Question objects and not FillBlankQuestion objects... But there must be a way for me to intercept setting of objects in the arraylist...
I dont know how to use interceptors.. but somehow have the feeling the solution lies in interceptors...

I cant change the way questions are added because the model is from hibernate....

Please help me out with this.. I am losing my hair....

Thank you.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Well I also feel that you'll have to create a custom interceptor for this task. To do that, you'll have to create a class which extends the org.apache.struts2.util.StrutsTypeConverter class and then you'll have to provide implementation of two methods



In these methods, you'll have to put the logic to convert string to your custom type(i.e. Question class and sub-classes) and vice versa. Then you'll have to create a xwork-conversion.properties file, put it in the class path i.e. WEB-INF/classes and then write something like this in it

com.mypackage.Question=com.mypackage.MyQuestionConverter

That's it. Since you are using generics, so Struts will automatically detect that the list is of type Question and call your custom interceptor...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Do your Question classes have a default (no-arg) constructor? Are you properly incrementing the array index for each new question?
Sunil Kumar Pissaye
Greenhorn

Joined: Apr 22, 2009
Posts: 7
Thank you for your reply Ankit..

I am not able to get the converter to run..
I have created a custom type converter by extending StrutsTypeConverter..
Also created a file TestAction-conversion.properties
When I try to convert a property like name, it goes through the converter, ex: when i put this:
name=test.MyQuestionConverter

It works fine, but to intercept the arraylist it just doesnt seem to go through the converter..
I tried using
questions=test.MyQuestionConverter
Element_questions=test.MyQuestionConverter
questions[]=test.MyQuestionConverter

None of these work! How do i get the converter to be called for an ArrayList ???
Sunil Kumar Pissaye
Greenhorn

Joined: Apr 22, 2009
Posts: 7
Hi David,
Yes and yes.. I have a default constructor.. and the increment also works properly...
I can add a property in the Question class QuestionType to find out what type of question is coming through...

But where do i find the correct class?

The ArrayList gets populated with Question objects... but apparently it does not know what to do with attributes that belong to subclasses, ex: blankAnswer

I need to somehow intercept the parameters and after comparing the 'questiontype' attribute, find out the correct class and put it back into the arraylist..

Is this possible? Or any workaround for this???

looks like a deadend
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh, I missed that you had to choose the class on form submit.

Yeah, you'll most likely need to write a custom type converter and include information in the submission that can be used to determine which class to instantiate.
Sunil Kumar Pissaye
Greenhorn

Joined: Apr 22, 2009
Posts: 7
Hi David,

I have now added a questionType attribute to the question class which will help me determine which subclass to instantiate....

But i am still struggling with creating the Type Converter...
I followed Ankit's advice and created a class QuestionTypeConverter which extends StrutsTypeConverter
When i configure TestAction-conversion.properties with
name=test.MyQuestionConverter
It works fine, but to intercept the arraylist it just doesnt seem to go through to the converter..
I tried using
questions=test.MyQuestionConverter
Element_questions=test.MyQuestionConverter
questions[]=test.MyQuestionConverter

I also tried
com.blah.Question=test.MyQuestionConverter

This also does not seem to work...

One More question here... I am using generics for the List declarations in the model class..
List<Question> questions = new ArrayList<Question>();

Does this in any way affect calling of the converter?
Sunil Kumar Pissaye
Greenhorn

Joined: Apr 22, 2009
Posts: 7
after a lot of beating around.. i still cant get this to work....
i have given up and changed my database design...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get subclass objects in an arraylist?