我有一個jsp頁面,顯示考試和問題列表的表單。當用戶想要編輯其中一個問題但未選擇問題時,問題操作中的驗證失敗,我們將繼續進行jsp考試。驗證失敗後未調用準備
爲了重新填充exam.jsp。 ExamAction
(ActionSupport
)執行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>
顯示你的動作配置 –
我添加了它問題 – LightOfDay
配置:) –