You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Alireza Fattahi <af...@yahoo.com> on 2014/02/25 14:45:36 UTC
Struts 2 defining a base action
We are creating a base action to manage export function. This base class deals with file name and content type.
public class public abstract class AbstractExportAction{ //Will be override protected abstract InputStream exportInputStream() ; @Action(value = "export", results = { @Result(name = "success", type = "stream", params = { "inputName", "inputStream", "contentType", "${exportContentType}; charset=UTF-8", "Content-Disposition", "attachment; filename=\"${filename}\"", "contentDisposition", "attachment; filename=\"${filename}\"", "bufferSize", "2048" }) }) public String export() throws ClientException { inputStream = exportInputStream(); LOG.debug("Exporting to {} file ", getFilename()); return SUCCESS; }
//setter getters for filename and exportContentType }
Now the other actions extend above class and return they own data as InputStream
package action.account;
public class AccountsSummary extends AbstractExportAction { @Override protected InputStream exportInputStream() { } }
*We have /account/export.action without any need to deal with file name and content *
Generally every thing works fine, but in below case, the export action will not map to correct class:
* Action A extends AbstractExportAction
* Action B extends AbstractExportAction
* Action A and B are in same namespace (package)
In above case, struts ( struts convention plugin) randomly took one action A or B and map export action to that class.
Do you think we are using correct approach?!
~Regards,
~~Alireza Fattahi
Re: Struts 2 defining a base action
Posted by Christoph Nenning <Ch...@lex-com.net>.
Basically it is better to reuse code via interceptors, not action base
classes. You can have a look at JSONValidationInterceptor for an example
how an interceptor can create a result.
In your case you could define an interface Exportable which actions have
to implement and your interceptor can use it to get data from actions.
Regards,
Christoph
>
> We are creating a base action to manage export function. This base
> class deals with file name and content type.
> public class public abstract class AbstractExportAction{ //Will be
> override protected abstract InputStream exportInputStream() ;
> @Action(value = "export", results = { @Result(name = "success", type
> = "stream", params = { "inputName", "inputStream", "contentType", "$
> {exportContentType}; charset=UTF-8", "Content-Disposition",
> "attachment; filename=\"${filename}\"", "contentDisposition",
> "attachment; filename=\"${filename}\"", "bufferSize", "2048" }) })
> public String export() throws ClientException { inputStream =
> exportInputStream(); LOG.debug("Exporting to {} file ", getFilename
> ()); return SUCCESS; }
> //setter getters for filename and exportContentType }
> Now the other actions extend above class and return they own data as
> InputStream
> package action.account;
> public class AccountsSummary extends AbstractExportAction {
> @Override protected InputStream exportInputStream() { } }
> *We have /account/export.action without any need to deal with file
> name and content *
> Generally every thing works fine, but in below case, the export
> action will not map to correct class:
> * Action A extends AbstractExportAction
> * Action B extends AbstractExportAction
> * Action A and B are in same namespace (package)
> In above case, struts ( struts convention plugin) randomly took one
> action A or B and map export action to that class.
> Do you think we are using correct approach?!
>
> ~Regards,
> ~~Alireza Fattahi
This Email was scanned by Sophos Anti Virus