日期:2014-05-20  浏览次数:23847 次

struts2 Action重复执行,急!
背景:做的是一个考试系统,每套试卷由不同的试题组成。考生每做完一道题就提交一次答案,然后页面显示下一道题目及其选项。但当提交最后一道题的时候,会将考生的所做的题目正确性进行一个统计。所以考生试题答案的action在最后一次处理时会与非最后一次提交产生的跳转有所不同。
问题:考生提交答案进入处理答案的action,action会执行两遍,然后进入获取试题信息的action。如果考生是最后一次提交处理答案的话,仍然会在处理答案的action中执行两遍,然后进入更新考生信息的action中执行两遍,再进入统计考生考试信息的action中执行两遍。所有的处理内容都是正确的,但就是要执行两次,不明白原因,急切需要帮助,谢谢!

 
JSP页面中发送form的JavaScript代码:
<script type="text/javascript">

function insertAnswer() {
var form1 = document.all.optionForm;
//得到下一道题的题号
var nextnumber = document.all.nextQuestionNumber.value;
//得到整套卷子的题目数
var size = document.all.questionSize.value;
//end代表是不是结束考试,若为end=true,则表示结束考试
if(nextnumber>1 && nextnumber<=size){
form1.action="<%=request.getContextPath()%>/front/insertAnswer.action?end=false";
}else{
form1.action ="<%=request.getContextPath()%>/front/insertAnswer.action?end=true";
}

form1.submit();
}
</script>

insertAnswer action的内容如下:  nextQuestionNumber、endTime和optionId都有相应的set和get方法

public String insertAnswer(){
log.debug("inserting an answer" );
Integer paperId = (Integer)session.get("paperId");
Long examineeId = (Long)session.get("examineeId");
Boolean timed =(Boolean)session.get("timed");
Answer answer = null;
try {
if(timed){
endTime = new Date(System.currentTimeMillis());
answer = service.save(paperId, examineeId, optionId, endTime);
}else{
answer = service.save(paperId, examineeId, optionId);
}
} catch (Exception e) {
log.error("inserting failed because of some database's reason" );
e.printStackTrace();
return ERROR;
}
if(answer != null){
log.debug("inserting success" );
log.info("answer: "+answer.getId());
Integer size = (Integer)session.get("questionSize");
if(!end){
return SUCCESS;
}else{
return "end";
}
}else{
log.debug("inserting error" );
return ERROR;
}
}
配置
<action name="insertAnswer" class="answerAction" method="insertAnswer">
<result name="success" type="redirect">
/front/getQuestionByNumber.action?number=${nextQuestionNumber}
</result>

<result name="end" type="redirect">
/front/updateExaminee.action?end=${endTime}
</result>
<result name = "error">/error.jsp</result>
</action>

<action name="getQuestionByNumber" class="frontQuestionAction" method="getQuestionByNumber">
<result name="success">questions.jsp</result>
<result name = "error">/error.jsp</result>
</action>

<action name="updateExaminee" class="examineeAction" method="updateExaminee">
<result name="success" type="redirectAction">
<param name="actionName">countAnswer</param>
           <param name="namespace">/front</param>
   
</result>
<result name = "error">/error.jsp</result>

<action name="countAnswer" class="answerAction" method="countAnswer">
<result name="success" type="redirect">/front/review.jsp</result>
<result name = "error">/error.jsp</result>
</action>

------解决方案--------------------