You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Olivier THIERRY <ol...@gmail.com> on 2007/05/29 15:36:53 UTC
[S2] How to set an action as input result ?
Hi,
I need to do something that looks very simple to me :
- a form with a select list
- the data in the list comes from database
- when submitting the form, I want to validate datas. If it doesn't
validate, I display the form again
I configured two actions :
- An "init" action, that loads data from the database and redirects to the
JSP page
- A "submit" action, that validates datas and writes data in the database
<package name="absences" namespace="/absences" extends="struts-default">
<action name="initCreerDemandeAbsence"
class="creerDemandeAbsenceAction" method="init">
<result
name="success">/jsp/absences/creerDemandeAbsence.jsp</result>
</action>
<action name="submitCreerDemandeAbsence"
class="creerDemandeAbsenceAction">
<interceptor-ref name="defaultStack" />
<result
name="input">/absences/initCreerDemandeAbsence.action</result>
<result
name="success">/jsp/absences/confirmerCreationDemandeAbsence.jsp</result>
</action>
</package>
</struts>
My action class extends ActionSupport and contains variables and the
following methods :
public String init() throws Exception {
this.motifs = getServiceAbsence().getAllMotifsAbsence();
return SUCCESS;
}
public String execute() throws Exception {
demandeAbsence.setMatriculeAuteur(SecurityContextHolder.getContext
().getAuthentication().getName());
demandeAbsence.setVisa(VisaDemande.VISA_ATTENTE.getValue());
demandeAbsence.setLignes(new
VOLigneDemandeAbsence[]{getLigneDemandeAbsence()});
demandeAbsence = getServiceAbsence().createDemande(demandeAbsence);
return SUCCESS;
}
And my JSP page is as following :
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Demande d'absence</title>
<s:head theme="ajax" />
</head>
<body>
<s:actionerror />
<s:form action="submitCreerDemandeAbsence" id="form1" validate="false"
method="post">
<s:datetimepicker
name="demandeAbsence.dateDemande"
label="Date de la demande"
required="true"
requiredposition="left"
displayFormat="dd/MM/yyyy"
/>
....
<s:select
name="ligneDemandeAbsence.motif"
label="Motif"
list="motifs"
listKey="nom"
listValue="nom"
headerKey=""
headerValue="--- Sélectionnez un motif ---"
required="true"
requiredposition="left"
/>
....
<s:submit
type="button"
theme="xhtml"
/>
</s:form>
</body>
</html>
But it doesn't work this way : when there are validation errors, I have a
404 HTTP error when trying to redirect to "initCreerDemandeAbsence" action
(I need to do this to have the select list datas loaded from database).
In fact it works only when I set a JSP page as result ...
Anyone knows how to set an action as an input result rather than a JSP page
?
And are there better practices to do what I want ?
Thanks in advance
Olivier
Re: [S2] How to set an action as input result ?
Posted by yitzle <yi...@users.sourceforge.net>.
Somewhat of a new user myself, but I think you want to read this:
http://struts.apache.org/2.x/docs/result-types.html
The default result type is Dispatcher Result. Used for JSPs.
You want a Chain Result for a .action .
The page got sample code.
Yay. I'm going to be using this myself. I guess its a good thing you
asked and I found it ;)
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: [S2] How to set an action as input result ?
Posted by Olivier THIERRY <ol...@gmail.com>.
Didin't finish what I wanted to say !
I wanted to say it's a very elegant method ;)
2007/5/29, Olivier THIERRY <ol...@gmail.com>:
>
> And I have to say it's a very
>
>
Re: [S2] How to set an action as input result ?
Posted by Olivier THIERRY <ol...@gmail.com>.
I couldn't find this in the docs. Where could I find it please ?
Anyway, I finally succeeded.
I had already tried the Prepare interceptor but could'nt make it work as I
wanted. But this time it works !
And I have to say it's a very
So I configured the actions as following :
<package name="absences" namespace="/absences" extends="struts-default">
<action name="creerDemandeAbsence"
class="creerDemandeAbsenceAction">
<interceptor-ref name="defaultStack" />
<result
name="input">/jsp/absences/creerDemandeAbsence.jsp</result>
<result
name="success">/jsp/absences/confirmerCreationDemandeAbsence.jsp</result>
</action>
</package>
My action class has the following methods :
public String input() throws Exception {
return INPUT;
}
public String execute() throws Exception {
demandeAbsence.setMatriculeAuteur(SecurityContextHolder.getContext
().getAuthentication().getName());
demandeAbsence.setVisa(VisaDemande.VISA_ATTENTE.getValue());
demandeAbsence.setLignes(new
VOLigneDemandeAbsence[]{getLigneDemandeAbsence()});
demandeAbsence = getServiceAbsence().createDemande(demandeAbsence);
return SUCCESS;
}
public void prepare() throws Exception {
this.motifs = getServiceAbsence().getAllMotifsAbsence();
}
And my form begins as following :
<s:form action="creerDemandeAbsence" id="form1" validate="false"
method="post">
I think my error was that my action class had an "init" method returning
SUCCESS instead of a "input" method returning INPUT.
But I am not sure to understand why it works lol
Can you tell me if there is a specific behaviour with input method or
something like that ?
Thanks
Olivier
Re: [S2] How to set an action as input result ?
Posted by Dave Newton <ne...@yahoo.com>.
--- Olivier THIERRY <ol...@gmail.com> wrote:
> Well, I don't see how it can solve my problem ...
> If the input result is a JSP page, when will this
> prepare metho be called ?
It's called by the Prepare interceptor.
You should probably dig through a bit more of the docs
detailing the request cycle.
d.
____________________________________________________________________________________Building a website is a piece of cake. Yahoo! Small Business gives you all the tools to get online.
http://smallbusiness.yahoo.com/webhosting
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: [S2] How to set an action as input result ?
Posted by Olivier THIERRY <ol...@gmail.com>.
Well, I don't see how it can solve my problem ...
If the input result is a JSP page, when will this prepare metho be called ?
2007/5/29, Dave Newton <ne...@yahoo.com>:
>
> --- Olivier THIERRY <ol...@gmail.com> wrote:
> > Anyone knows how to set an action as an input result
> > rather than a JSP page?
>
> http://struts.apache.org/2.x/docs/result-types.html
>
> > And are there better practices to do what I want ?
>
> Preparable interface (didn't we do this once already?)
> is designed to... well, prepare.
>
> d.
>
>
>
>
> ____________________________________________________________________________________Got
> a little couch potato?
> Check out fun summer activities for kids.
>
> http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities+for+kids&cs=bz
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: [S2] How to set an action as input result ?
Posted by Dave Newton <ne...@yahoo.com>.
--- Olivier THIERRY <ol...@gmail.com> wrote:
> Anyone knows how to set an action as an input result
> rather than a JSP page?
http://struts.apache.org/2.x/docs/result-types.html
> And are there better practices to do what I want ?
Preparable interface (didn't we do this once already?)
is designed to... well, prepare.
d.
____________________________________________________________________________________Got a little couch potato?
Check out fun summer activities for kids.
http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities+for+kids&cs=bz
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org