You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by m....@acamaya.com on 2007/07/12 16:54:44 UTC

[S2] Generic Actions attributes and stack values.

Hi,
I got a problem with an extremly generic platform I'm trying to implement.
Here is my genericAction code to understand the question mentionned above:

===================================================================
public abstract class  GenericAction<T> extends ActionSupport implements 
Preparable, SessionAware{
        private static final long serialVersionUID = 1L;
        protected final Log logger = LogFactory.getLog(getClass());
        protected Map session;
 
        protected IGenericService<T, Long> dedicatedService;
        protected Long currentModelItemId = new Long(0);
        protected T currentModelItem;
        protected List<T> theCurrentModelItems;
        protected int totalRows;
 
        public String save() {
                dedicatedService.saveOrUpdate(currentModelItem);
                return SUCCESS;
        }
 
        public String delete() {
                currentModelItem = dedicatedService.findById(
currentModelItemId);
                if (currentModelItem != null) {
                        dedicatedService.delete(currentModelItem);
                        return SUCCESS;
                } else {
                        return ERROR;
                }
        }
 
        public  String getAll(){
                this.theCurrentModelItems = dedicatedService.findAll();
                return SUCCESS;
        }
 
        public void prepare() {
                //ready to be overloaded if needed
        }
 
        //abstract because it needs model instanciation that can't be done 
with generics types
        public abstract String list(); 
        public abstract String input();

        + getters and setters
} 
===================================================================
A sample Action using my generic one:

public class FichierAction extends GenericAction<Fichier> {
        private static final long serialVersionUID = 1L;

        public String input() {
                currentModelItem = dedicatedService.findById(
currentModelItemId);
                if (currentModelItem == null) {
                        currentModelItem = new Fichier(); 
                }
                return INPUT;
        }
        ...
}
===================================================================
And finally the form that is using this action:

<s:action name="SupportListAll" id="supportlist" />

<s:form action="FichierSave" method="post" validate="false">
        <s:textfield id="edtNom" name="currentModelItem.nom" value=
"%{currentModelItem.nom}" label="Nom" size="20" />
        <s:textfield name="currentModelItem.extension" 
value="%{currentModelItem.extension}" label="Extension" size="4" />
        <s:select name="currentModelItem.support.id" value=
"%{currentModelItem.support.id}" list=
"%{#supportlist.theCurrentModelItems}" listKey="id" listValue="ref" label=
"Support" />
        <s:hidden name="currentModelItemId" value="%{currentModelItem.id}" 
/>

        <s:submit id="submit" value="Create" />
        <s:submit id="cancel" value="Cancel" name=
"redirect-action:FichierList" /> 
</s:form>
===================================================================

The problem seam to be about the stack values when my form execute the 
ListAll method on the SupportAction (first line of the form). 
The Fichier model items list (currentModelItems) seems to be replaced by 
the Support model items list (same name) when the action is executed, so 
when I go back to the page that displays de rows of Fichier, the list is 
filled with Oeuvre items.
Is there a way to get out of that problem by being able to prefix 
something in the OGNL expressions or have I to move my attributes down in 
my action and to name it in a different way.

Thanks,

Michaƫl.