You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Laetitia DUBY <la...@gmail.com> on 2009/07/12 13:30:17 UTC

Using Java Generics in Struts Actions

Hi all !

I'm new to Struts2 and I'm starting with implementing a simple app. So
far, everything was fine. But now I want to complexify a bit by
introducing java generics (to prevent from repeating same treatments
in several classes) and integrating with Spring.

I tried writing a GenericAction which has 2 parameterized type (one
for the service used, one for the generated object from the form) and
which contains all basic methods for saving, deleting, updating,
listing objects (but with parameterized types). Then I wrote an action
which extends this GenericAction, the 2 types are passed in argument
in the extends statement.

The struts.xml config file references the later one and I have an
additional constant declaration to use Spring : <constant
name="struts.objectFactory" value="spring" />.

When I fill in the form and call the save.action, I get the following
error: ognl.NoSuchPropertyException: java.lang.Object.id

This tells me that Struts (or Spring) do not create the right type of
object and is then not capable to set the properties. But I don't
understand why. I tested the generics principle with simple classes
(MyBox extends Box<Integer>, where Box<T> is a generic class) and when
I create a MyBox object in a main function, the property has the right
type (Integer).

Can you help me to point out where is the problem ? Am i missing
something with the generics or is it a configuration problem with
Struts (or Spring) ?

Many thanks in advance for your help.

Laëtitia

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Using Java Generics in Struts Actions

Posted by Laetitia DUBY <la...@gmail.com>.
On Sun, Jul 12, 2009 at 3:20 PM, Dave Newton<ne...@yahoo.com> wrote:
> You don't describe the circumstances under which you get the error, so it's
> difficult to help. It might just be OGNL's result vs. action property thing,
> which is ignorable.
I am not sure what you mean by circumstances, so I'll add all the
relevant part of my code.

First, I have a jsp with a form, using struts taglib :
  <s:form action="save" validate="true">
    <s:textfield id="id" name="entity.id" cssStyle="display:none"/>
    <s:textfield id="titre" label="Titre" name="entity.titre"/>
    <s:textfield id="sujet" label="Sujet" name="entity.sujet"/>
    <s:textfield id="texte" label="Texte" name="entity.texte"/>
    <sx:submit  targets="automaticAnswers" notifyTopics="/save"/>
  </s:form>

The target action is AutomaticAnswerAction, and should use an "entity"
of type "ReponseAutomatique" (but that's the problem, at runtime it is
only of type "Object") :
public class AutomaticAnswerAction extends
GenericAction<ReponseAutomatique, AutomaticAnswer> {
}

AutomaticAnswerAction inherits from the generic class GenericAction :
public abstract class GenericAction<T, S extends GenericService<T>>
implements Preparable {
    private S service;
    private List<T> entities;
    public T entity;
    private Integer id;
[...]
    public T getEntity() {
        return  entity;
    }

    public void setEntity(T entity) {
        this.entity = (T) entity;
    }
}

The fields from the form are related to an object of type
ReponseAutomatique (hence the try to use
GenericAction<ReponseAutomatique, AutomaticAnswer>), but when I use
the debugger I can see that the object is only of type "Object", so I
get the error below :
error: ognl.NoSuchPropertyException: java.lang.Object.id

Of course, Object has no property id, but ReponseAutomatique has one.

The struts.xml (not relevant I guess in this case) :
<action name="save" class="automaticAnswerAction" method="save">
     <result>pages/parameter/automaticAnswer/list.jsp</result>
     <result name="input">pages/parameter/automaticAnswer/list.jsp</result>
</action>

I am not sure wether we can use the generics at all in this case : I
believe there is only servlet instanciated, so that would mean to
change the type of the object at runtime, and that is not possible, as
the java generics are used only at compile time.

Thanks for your help,

Laetitia



> Laetitia DUBY wrote:
>>
>> Hi all !
>>
>> I'm new to Struts2 and I'm starting with implementing a simple app. So
>> far, everything was fine. But now I want to complexify a bit by
>> introducing java generics (to prevent from repeating same treatments
>> in several classes) and integrating with Spring.
>>
>> I tried writing a GenericAction which has 2 parameterized type (one
>> for the service used, one for the generated object from the form) and
>> which contains all basic methods for saving, deleting, updating,
>> listing objects (but with parameterized types). Then I wrote an action
>> which extends this GenericAction, the 2 types are passed in argument
>> in the extends statement.
>>
>> The struts.xml config file references the later one and I have an
>> additional constant declaration to use Spring : <constant
>> name="struts.objectFactory" value="spring" />.
>>
>> When I fill in the form and call the save.action, I get the following
>> error: ognl.NoSuchPropertyException: java.lang.Object.id
>>
>> This tells me that Struts (or Spring) do not create the right type of
>> object and is then not capable to set the properties. But I don't
>> understand why. I tested the generics principle with simple classes
>> (MyBox extends Box<Integer>, where Box<T> is a generic class) and when
>> I create a MyBox object in a main function, the property has the right
>> type (Integer).
>>
>> Can you help me to point out where is the problem ? Am i missing
>> something with the generics or is it a configuration problem with
>> Struts (or Spring) ?
>>
>> Many thanks in advance for your help.
>>
>> Laėtitia
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Using Java Generics in Struts Actions

Posted by Dave Newton <ne...@yahoo.com>.
You don't describe the circumstances under which you get the error, so 
it's difficult to help. It might just be OGNL's result vs. action 
property thing, which is ignorable.

Dave

Laetitia DUBY wrote:
> Hi all !
> 
> I'm new to Struts2 and I'm starting with implementing a simple app. So
> far, everything was fine. But now I want to complexify a bit by
> introducing java generics (to prevent from repeating same treatments
> in several classes) and integrating with Spring.
> 
> I tried writing a GenericAction which has 2 parameterized type (one
> for the service used, one for the generated object from the form) and
> which contains all basic methods for saving, deleting, updating,
> listing objects (but with parameterized types). Then I wrote an action
> which extends this GenericAction, the 2 types are passed in argument
> in the extends statement.
> 
> The struts.xml config file references the later one and I have an
> additional constant declaration to use Spring : <constant
> name="struts.objectFactory" value="spring" />.
> 
> When I fill in the form and call the save.action, I get the following
> error: ognl.NoSuchPropertyException: java.lang.Object.id
> 
> This tells me that Struts (or Spring) do not create the right type of
> object and is then not capable to set the properties. But I don't
> understand why. I tested the generics principle with simple classes
> (MyBox extends Box<Integer>, where Box<T> is a generic class) and when
> I create a MyBox object in a main function, the property has the right
> type (Integer).
> 
> Can you help me to point out where is the problem ? Am i missing
> something with the generics or is it a configuration problem with
> Struts (or Spring) ?
> 
> Many thanks in advance for your help.
> 
> Laëtitia
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org