2014-01-28 25 views
0

我有一個映射到包含路徑變量(@RequestMapping(value="/{studentId}/activity/add", ...)的URL的控制器方法。由於我在這種方法中進行了一些驗證,因此可能會發生該方法被多次調用。對該方法的第一個調用工作正常,但如果我第二次嘗試調用它(例如,如果我第一次填充字段時發生了某些錯誤),則路徑變量會丟失,並且頁面addNewActivity無法加載。Spring MVC:首次調用控制器方法後路徑變量丟失

控制器的方法:

@RequestMapping(value="/{studentId}/activity/add", method = RequestMethod.POST, params="saveNew") 
    public String postAddNewActivity(@PathVariable Integer studentId, 
      @Validated(Activity.ActivityAddNewChecks.class) @ModelAttribute("studentActivityDTO") StudentActivityDTO studentActivityDTO, 
      BindingResult result, 
      Model model) { 

     logger.debug("Received request to add new activity to student"); 

     if (result.hasErrors()) { 
      model.addAttribute("studentActivityDTO", studentActivityDTO); 
      model.addAttribute("courseList", courseService.getAll()); 
      model.addAttribute("teacherList", teacherService.getAll()); 
      return "addNewActivity"; 
     } 
     else { 
      Activity activity = studentActivityDTO.getActivity(); 
      activityService.add(studentId, activity); 
      return "success/addActivitySuccess"; 
     } 
    } 

這是URL看起來像第一次調用方法:

http://someserver/essaysWebApp/essays/main/student/39/activity/add 

這是怎麼看起來像第二次調用方法:

http://someserver/essaysWebApp/essays/main/student//activity/add 

這是我第二次調用該方法時得到的堆棧跟蹤:

[TRACE] [http-bio-8080-exec-4 12:54:10] (FrameworkServlet.java:initContextHolders:1018) Bound request context to thread: [email protected] 
[DEBUG] [http-bio-8080-exec-4 12:54:10] (DispatcherServlet.java:doService:823) DispatcherServlet with name 'spring' processing POST request for [/essaysWebApp/essays/main/student//activity/add] 
[TRACE] [http-bio-8080-exec-4 12:54:10] (DispatcherServlet.java:getHandler:1088) Testing handler map [org.springframework[email protected]21a40993] in DispatcherServlet with name 'spring' 
[DEBUG] [http-bio-8080-exec-4 12:54:10] (AbstractHandlerMethodMapping.java:getHandlerInternal:220) Looking up handler method for path /main/student//activity/add 
[DEBUG] [http-bio-8080-exec-4 12:54:10] (AbstractHandlerMethodMapping.java:getHandlerInternal:230) Did not find handler method for [/main/student//activity/add] 
[TRACE] [http-bio-8080-exec-4 12:54:10] (DispatcherServlet.java:getHandler:1088) Testing handler map [or[email protected]62cc540b] in DispatcherServlet with name 'spring' 
[TRACE] [http-bio-8080-exec-4 12:54:10] (AbstractUrlHandlerMapping.java:getHandlerInternal:127) No handler mapping found for [/main/student//activity/add] 
[ WARN] [http-bio-8080-exec-4 12:54:10] (DispatcherServlet.java:noHandlerFound:1108) No mapping found for HTTP request with URI [/essaysWebApp/essays/main/student//activity/add] in DispatcherServlet with name 'spring' 
[TRACE] [http-bio-8080-exec-4 12:54:10] (FrameworkServlet.java:resetContextHolders:1028) Cleared thread-bound request context: [email protected] 
[DEBUG] [http-bio-8080-exec-4 12:54:10] (FrameworkServlet.java:processRequest:966) Successfully completed request 
[TRACE] [http-bio-8080-exec-4 12:54:10] (AbstractApplicationContext.java:publishEvent:332) Publishing event in WebApplicationContext for namespace 'spring-servlet': ServletRequestHandledEvent: url=[/essaysWebApp/essays/main/student//activity/add]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[spring]; session=[null]; user=[null]; time=[16ms]; status=[OK] 
[TRACE] [http-bio-8080-exec-4 12:54:10] (AbstractApplicationContext.java:publishEvent:332) Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/essaysWebApp/essays/main/student//activity/add]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[spring]; session=[null]; user=[null]; time=[16ms]; status=[OK] 

我甚至可以開始明白爲什麼會發生這種情況。任何人都可以幫忙嗎?

我忘了說,如果用戶第一次輸入正確的值到所有字段,else部分if語句得到執行,一切工作正常。

更新:這是JSP頁面中的控制器被映射到:

<c:url var="studentUrl" value="/essays/main/student/${studentActivityDTO.student.studentId}/activity/add" /> 
<form:form modelAttribute="studentActivityDTO" method="POST" action="${studentUrl}"> 
<form:errors path="*" cssClass="errorblock" element="div" /> 

    <form:label path="student.firstName">First name:</form:label> 
    <form:input path="student.firstName" readonly="true"/> 

    <form:label path="student.lastName">Last name:</form:label> 
    <form:input path="student.lastName" readonly="true"/> 

    <form:label path="student.indexNumber">Index number:</form:label> 
    <form:input path="student.indexNumber" readonly="true"/> 

    <form:label path="student.program.programId">MK:</form:label> 
    <form:input path="student.program.programId" readonly="true"/> 

    <form:label path="student.rollNumber">Roll number:</form:label> 
    <form:input path="student.rollNumber" readonly="true"/> 

    <form:label path="student.rollYear">Roll year:</form:label> 
    <form:input path="student.rollYear" readonly="true"/> 

    <form:label path="student.program.programDescription">Program desc:</form:label> 
    <form:input path="student.program.programDescription" readonly="true"/> 

    <h2>Add new activity</h2> 

    <form:radiobutton path="activity.essayFlag" value="Essay" />Essay 
    <form:radiobutton path="activity.essayFlag" value="Other"/>Other 

    <form:label path="activity.activityDescription">Desc:</form:label> 
    <form:input path="activity.activityDescription"/> 
    <form:errors path="activity.activityDescription" cssClass="error"/> 

    <form:label path="activity.course">Course:</form:label> 
    <form:select path="activity.course" id="courseSelect"> 
     <form:option value="" label="Select" /> 
     <form:options items="${courseList}" itemValue="courseId" itemLabel="courseDescription" />    
    </form:select> 
    <form:errors path="activity.course" cssClass="error"/> 

    Teachers: 
    <select multiple="multiple" name="activity.teachers" > 
     <c:forEach var="theTeacher" items="${teacherList}"> 
       <option value="${theTeacher.teacherId}">${theTeacher.title.titleDescription} ${theTeacher.firstName} ${theTeacher.lastName}</option> 
     </c:forEach> 
    </select> 
    <form:errors path="activity.teachers" cssClass="error"/> 

    <form:label path="activity.submissionDate">Date:</form:label> 
    <form:input path="activity.submissionDate" class="datepicker"/> 
    <form:errors path="activity.submissionDate" cssClass="error"/> 

    <form:label path="activity.score">Score:</form:label> 
    <form:input path="activity.score"/> 
    <form:errors path="activity.score" cssClass="error"/> 

    <form:label path="activity.note">Note:</form:label> 
    <form:textarea path="activity.note"/> 

    <input type="submit" value="Cancel" name="cancel"/> 
    <input type="submit" value="Submit" name="saveNew"/> 
</form:form> 
+1

我刪除了日誌中的重複項。你應該真的解決這個問題。查看log4j或logback中的additive標誌,或者你正在使用的任何東西。 –

+0

這看起來像是一個客戶端問題。你使用的是什麼類型的客戶端?你的客戶如何構建它的網址? –

+0

感謝您編輯我的帖子和提示@SotiriosDelimanolis,我會檢查我的log4j。我已更新我的帖子,希望這是所有需要的信息。 –

回答

1

你永遠不會從你的<form>提交student.studentId。因此,Spring無法以相應的studentId值重建您的StudentActivityDTOstudent字段。

當你將它添加到模型

model.addAttribute("studentActivityDTO", studentActivityDTO); 

,並轉發給您的看法,對於studentId,我會認爲沒有值String類型或一些Number類(例如:Long)的。

EL解析表達式${}null將只打印空的String

只要確保提交一個student.studentId<input>

+0

作品!我爲'student.studentId'添加了隱藏的'',瞧!謝謝@SotiriosDelimanolis! –

+0

親愛的@SotiriosDelimanolis,請允許我打擾你看看這個帖子http://stackoverflow.com/questions/21436955/spring-mvc-formselect-option-wont-stay-selected並告訴我是否有可能保持選擇的選項在頁面重新加載後使用而不是html