Struts2:xwork没有转换发送的参数

时间:2013-02-01 10:40:07

标签: parameters struts2 xwork

我尝试用Struts2创建一个基本的应用程序。

我的问题是我的参数不是由Xworks转换的。我的action属性(idParticipant)是一个String,但参数是一个数组²。如果我没弄错的话,XWork应该在动作执行之前转换基本参数,对吗?

我的jsp:

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

我的行动:

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";
    }
}

我的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>

最后,我提交的例外情况是:

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

我知道这可能只是一个配置问题,但我找不到我的错误。

由于

1 个答案:

答案 0 :(得分:1)

有两件事:

  1. 将其更改为此<s:hidden name="idParticipant" value="%{'4'}"></s:hidden>
  2. 参数名称应为idParticipant,而不是idParticiPant