验证失败后不会调用Prepare

时间:2014-11-17 13:54:43

标签: jsp validation struts2

我有一个jsp页面,显示考试表格和问题列表。当用户想要编辑其中一个问题但尚未选择问题时,问题操作中的验证将失败,我们将继续参加考试jsp。

为了重新填充exam.jsp。 ExamActionActionSupport)实现了Preparable,并在那里设置了所有参数。

但在调试时,我发现验证后永远不会调用prepare() 但是,当调用ExamAction的其他方法时,prepare()也是如此。为什么它只是没有进行验证?

我正在使用defaultStack,我读过的不应该是问题。

如果您需要任何进一步的信息或代码,请告知我们。


编辑

这是我的ExamAction

BaseAction扩展ActionSupport并实施SessionAware

public class ExamAction extends BaseAction implements Preparable {


/** class variables */


@Override
public void prepare() {

    questionTypes = new ArrayList<String>();
    QuestionType[] allTypes = QuestionType.values();
    for (QuestionType questionType : allTypes) {
        questionTypes.add(questionType.toString());
    }

    long id = 0l;
    if (examId != null) {
        id = examId;
    } else if (question != null) {
        id = question.getExam().getId();
    } else {
        Exam currentExam = (Exam) getSession().get("exam");
        if (currentExam != null) {
            id = currentExam.getId();
        }
    }
    if (id != 0) {
        exam = examService.loadExam(id);
        getSession().put("exam", exam);
        questionList = exam.getQuestions();
        getSession().remove("question");
        participantList = new ArrayList<User>();
        for (ExamKey examKey : exam.getExamKeys()) {
            participantList.add(examKey.getUser());
        }
    }
}


/**
 * Displays the selected/current exam in the exam form and load it's
 * questions.
 * 
 * @return the result string.
 */
public String load() {
    long id = 0l;
    if (examId != null) {
        id = examId;
    } else if (question != null) {
        id = question.getExam().getId();
    } else {
        Exam currentExam = (Exam) getSession().get("exam");
        if (currentExam != null) {
            id = currentExam.getId();
        }
    }
    if (id != 0) {
    exam = examService.loadExam(id);
    getSession().put("exam", exam);
    questionList = exam.getQuestions();
    getSession().remove("question");
        participantList = new ArrayList<User>();
    for (ExamKey examKey : exam.getExamKeys()) {
        participantList.add(examKey.getUser());
        }
    }
    return SUCCESS;
}


@Override
public void validate() {
    // If the exam is not set, the exam ID has to be set.
    if (exam == null && examId == null) {
        exam = (Exam) getSession().get("exam");
        if (exam != null) {
            questionList = exam.getQuestions();
        } else {
            if (question == null) {
                addActionError(getText("msg.selectExam"));
            }
        }
    }
}

/** all getters and setters*/

}

struts.xml中:

<action name="ShowExam" class="de.nak.cars.action.ExamAction"
        method="load">
    <result type="tiles">examForm</result>
</action>

<!-- Loads a existing question and shows it in the question form. -->
<action name="EditQuestion" class="de.nak.cars.action.QuestionAction"
        method="load">
    <result type="tiles">questionForm</result>
    <result type="tiles" name="input">examForm</result>
</action>

2 个答案:

答案 0 :(得分:1)

我想你的ExamAction正在呼叫EditAction(或类似的东西)。

然后在目标操作(prepare())上执行validation()(如果存在)和EditAction(如果存在)。如果验证失败,您将呈现前一个JSP(examForm),而不会被前一个Action(ExamAction)传递。

您需要重新配置input结果以将redirectAction返回ExamAction,而不是直接返回JSP。

但是,在redirectAction期间,由于正在创建新请求,因此操作消息/错误和字段错误将丢失。然后你可以把它们放在会话中,或者使用MessageStore拦截器(不是defaultStack的一部分......但首先尝试不用,然后当一切正常工作时打开那个&#34;新章节&#34;:)

答案 1 :(得分:0)

prepare()方法在验证之前调用,因此validate()方法不能成为问题

有关详细信息,请查看this页面