parametersstruts2xwork

Struts2 : xwork not converting sent parameters


I try to make a basic application with Struts2.

My problem is that my parameters are not converted by Xworks. My action attribute (idParticipant) is a String, but the parameter is an array². if I'm not mistaken, XWork is supposed to convert basic parameters before the action execution, right?

My jsp :

<s:form action="afficher_participant">
  <s:hidden name="idParticipant" value="4"></s:hidden>
  <s:submit>bob</s:submit>
</s:form>

My Action :

package action;

import java.util.List;

import model.Participant;
import service.ParticipantService;
import service.ParticipantServiceImpl;

public class ParticipantAction extends BaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String idParticiPant;
    private List<Participant> listeParticipant;
    private Participant participant;

    private ParticipantService participantService = new ParticipantServiceImpl();

    public String getIdParticiPant() {
        return idParticiPant;
    }

    public void setIdParticiPant(String idParticiPant) {
        this.idParticiPant = idParticiPant;
    }

    public List<Participant> getListeParticipant() {
        return listeParticipant;
    }

    public void setListeParticipant(List<Participant> listeParticipant) {
        this.listeParticipant = listeParticipant;
    }

    public Participant getParticipant() {
        return participant;
    }

    public void setParticipant(Participant participant) {
        this.participant = participant;
    }

    /* Actions */

    public String lister() {
        participantService = new ParticipantServiceImpl();
        this.listeParticipant = participantService.lister();
        return "listerParticipant";
    }

    public String afficher() {
        if (idParticiPant == null) {
            return ERROR;
        }
        participantService = new ParticipantServiceImpl();
        participant = participantService.get(Integer.valueOf(idParticiPant));
        return "afficherParticipant";
    }
}

My struts.xml :

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="package" />
    <constant name="struts.i18n.encoding" value="UTF-8" />

    <package name="frontoffice" namespace="/" extends="struts-default">

        <default-interceptor-ref name="defaultStack"></default-interceptor-ref>

        <!-- Action de l'action de réference -->
        <default-action-ref name="index" />

        <!-- Navigation rules -->
        <action name="*_participant" class="action.ParticipantAction" method="{1}">
            <result name="success">jsp/listerParticipant.jsp</result>
            <result name="listerParticipant">jsp/listerParticipant.jsp</result>
            <result name="afficherParticipant">jsp/afficherParticipant.jsp</result>
            <result name="error">/index.jsp</result>
        </action>

    </package>
</struts>

and finaly, the exception I get when I submit :

    Unexpected Exception caught setting 'idParticipant' on 'class action.ParticipantAction: Error setting 
expression 'idParticipant' with value ['4', ]

I know it's probably just a configuration problem but I can't find my mistake.

Thanks


Solution

  • There are two things:

    1. change it to this <s:hidden name="idParticipant" value="%{'4'}"></s:hidden>
    2. The name of parameter should be idParticipant, not idParticiPant.