0

我尝试用 Struts2 制作一个基本的应用程序。

我的问题是我的参数没有被 Xworks 转换。我的操作属性(idParticipant)是一个字符串,但参数是一个数组²。如果我没记错的话,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', ]

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

谢谢

4

1 回答 1

1

有两件事:

  1. 改成这个<s:hidden name="idParticipant" value="%{'4'}"></s:hidden>
  2. 参数的名称应该是idParticipant,而不是idParticiPant
于 2013-02-01T11:01:11.663 回答