2014-11-17 59 views
1

我有一個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> 
+0

顯示你的動作配置 –

+0

我添加了它問題 – LightOfDay

+0

配置:) –

回答

0

prepare()方法被調用之前有效以便在this

+0

但是'validate()'在QuestionAction中,'prepare()'在ExamAction中。我認爲,當驗證失敗時,我們返回到ExamAction,因此調用prepare(),或者我需要填充QuestionAction的prepare()中的所有變量? – LightOfDay

+1

如果您重定向到在其他操作類中使用方法的操作,那麼只要您使用默認攔截器堆棧或者您已指定,則在操作對象創建時,準備攔截器將調用該類中的prepare方法在自定義堆棧中準備攔截器。 Action類與Servlet不同,每次請求時都會重新創建它們,並且每次調用它們的prepare方法時都會重新創建。簡短的回答,是的,你可以填寫你的表格() – steven35

+0

我現在已經改變了我的QuestionAction的'prepare()',現在,驗證失敗它的工作原理! 但是,現在要編輯的問題ID不再從jsp傳遞給QuestionAction。變量questionID始終爲空(因此驗證再次失敗) 我該如何避免這種情況? – LightOfDay

1

我從你的ExamAction您所呼叫的EditAction(或類似的東西猜validate()方法不能成爲問題

欲瞭解更多信息的樣子)。

然後在目標動作(EditAction)上執行prepare()(如果存在)和validation()(如果存在)。如果驗證失敗,您將呈現之前的JSP(examForm),而不會被前面的Action(ExamAction)傳遞。

您需要重新配置input結果才能返回redirectActionExamAction,而不是直接從JSP返回。

但是,在redirectAction期間,由於正在創建新的請求,動作消息/錯誤和字段錯誤將丟失。然後你可以把它們放在會話中,或使用攔截器(不是defaultStack的一部分...但首先嚐試沒有,然後當一切工作打開「新章節」:)