How to declare a Generics Action in struts2.xml fi

2019-09-14 02:31发布

问题:

My problems is in a Struts2 action, where

I have a class :

public class MyAction<T> extends ActionSupport

with a private member like this :

private T myData;

And I would like to declare this aciton in the struts.xml file, how can i manage to do so ?

Thanks for the answer.

Ps : I've tried without declaration of T, but it did not work

回答1:

For example, you cannot obvioulsy write (in struts-XX.xml)

<action name="doSomething" class="xx.xx.MyAction<java.util.Date>">

But you can easily code a class (sort of alias) for each parametrization you intend to use.

public class MyAction_Date extends MyAction<java.util.Date> {}

and then :

<action name="doSomething" class="xx.xx.MyAction_Date">


回答2:

In struts2 the action object is instantiated by the framework for each request. Then, I don't think you can use a parametrized class for that. (Except if struts allows you to specify a particular class parametrization, say MyAction<Date> , for a particular action mapping - I don't think it allows that)