As some people asked me by private message, a few words to explain how I
could make it work.
I hope it will help other Struts users ;)

The original need : when the user types an elmployee code, I want to
retrieve his name and firstname and display it in a div.

My action class is as followin (it works with Spring ioc) :
It calls a service class to retrieve employee infos

public class InfosEmployeAction {

   private ServiceEmploye serviceEmploye;
   public ServiceEmploye getServiceEmploye() {
       return serviceEmploye;
   }
   public void setServiceEmploye(ServiceEmploye serviceEmploye) {
       this.serviceEmploye = serviceEmploye;
   }

   private VOEmploye employe;
   public VOEmploye getEmploye() {
       return employe;
   }
   public void setEmploye(VOEmploye employe) {
       this.employe = employe;
   }

   private String matricule;
   public String getMatricule() {
       return matricule;
   }
   public void setMatricule(String matricule) {
       this.matricule = matricule;
   }

   public String  execute() throws Exception {
       if (getMatricule() == null || getMatricule().length() == 0) {
           this.setEmploye(null);
       }
       else {
           this.setEmploye
(getServiceEmploye().getEmployeByMatricule(getMatricule()));
       }
       return "success";
   }

}

An extract from the JSP page from where I call the action :

   <s:url id="urlInfosEmploye" action="getInfosEmploye"
namespace="/employes" />
   <s:form action="submitCreerDemandeAbsence" id="form1">
       <s:textfield     id="matriculeDemandeur"
                       name="demandeAbsence.matriculeDemandeur"
                       label="Matricule"

onchange="dojo.event.topic.publish('updateInfosDemandeurTopic',
this.value);" />
       <s:div     theme="ajax"
               loadingText="Chargement en cours ..."
               href="%{urlInfosEmploye}"
               formId="form1"
               listenTopics="updateInfosDemandeurTopic"
               formFilter="filterInfosDemandeur" />
   </s:form>

The div listens a topic on which I publish when the onchange event is fired
for my text field.
It is bound to the form including the text field, the formFilter is used to
filter informations sent to the action (here the employee code), thanks to
the following JS function :

   <script type="text/javascript">
     function filterInfosDemandeur(field) {
       return field.name == "demandeAbsence.matriculeDemandeur";
     }
   </script>


My Struts action is configured as following :

   <package name="employes" extends="struts-default" namespace="/employes">
       <action name="getInfosEmploye" class="infosEmployeAction">
           <param name="aliases">#{'demandeAbsence.matriculeDemandeur' :
'matricule'}</param>
           <interceptor-ref name="alias" />
           <result name="success">/jsp/employes/infosEmploye.jsp</result>
       </action>
   </package>

The alias interceptor changes the name of parameter to what is expected by
the action.

And finally the infoEmploye.jsp page :

<!DOCTYPE html PUBLIC
   "-//W3C//DTD XHTML 1.1 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>

<[EMAIL PROTECTED] contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" 
%>
<[EMAIL PROTECTED] prefix="s" uri="/struts-tags" %>

<s:property value="employe.nom" />&nbsp;<s:property value="employe.prenom"
/>

The drawback of this solution is that you have to duplicate getInfosEmploye
action for each JSP page where you want to use it :(
Now it works, but if you have any suggestion to improve it, you're welcome.

And thanks a lot again to Musachy for his help ;)

Olivier

Reply via email to