2014-11-03 70 views
1

我正在使用JSP文件添加註釋的spring-mvc項目。目前,所有CRUD操作都在該項目中運行。唯一的問題是要查看數據庫中的更新信息,我必須每次都點擊刷新瀏覽器按鈕。我檢查了flashattributes,但由於我沒有重定向,只是返回對象,所以我覺得沒有用。我將我的控制器代碼和JSP代碼一起發佈。任何想法,爲什麼我需要刷新。Spring-MVC模型只在頁面刷新後更新

P.S:刷新不是必要的刪除功能,是的,我嘗試使用其他功能已經重定向。

控制器代碼:

@RequestMapping(value= "/note/add") 
    public String addNote(@ModelAttribute("notices") Notes p,Model model) { 
     Person person = personService.getCurrentlyAuthenticatedUser(); 
     model.addAttribute("notices", new Notes()); 
     model.addAttribute("listNotes",this.notesService.listNotes()); 
     model.addAttribute("listNotes", this.notesService.listNotePerson(person)); 
     model.addAttribute("section1",this.notesService.listNotesBySectionId(1,person)); 
     model.addAttribute("section2",this.notesService.listNotesBySectionId(2,person)); 
     model.addAttribute("section3",this.notesService.listNotesBySectionId(3,person)); 
} 
@RequestMapping("/editnote/{id}") 
    public String editNote(@PathVariable("id") Integer id, Model model){ 
     Person person = personService.getCurrentlyAuthenticatedUser(); 
     model.addAttribute("notices", this.notesService.getNoteById(id)); 
     model.addAttribute("section1",this.notesService.listNotesBySectionId(1,person)); 
     model.addAttribute("section2",this.notesService.listNotesBySectionId(2,person)); 
     model.addAttribute("section3",this.notesService.listNotesBySectionId(3,person)); 
} 
@RequestMapping(value = "/note/listing", method=RequestMethod.GET) 
    public String listNotices(Model model) { 

     Person person = personService.getCurrentlyAuthenticatedUser(); 
     model.addAttribute("notices",new Notes()); 
     model.addAttribute("listNotes", this.notesService.listNotePerson(person)); 
     model.addAttribute("section1",this.notesService.listNotesBySectionId(1,person)); 
     model.addAttribute("section2",this.notesService.listNotesBySectionId(2,person)); 
     model.addAttribute("section3",this.notesService.listNotesBySectionId(3,person)); 
} 

JSP代碼:

<c:url var="addAction" value="/note/add" ></c:url> 
<form:form action="${addAction}" commandName="notices"> 
    <table> 
     <c:if test="${!empty notices.notetext}"> 
      <tr> 
       <td> 
        <form:label path="noticesid"> 
         <spring:message text="noticesid"/> 
        </form:label> 
       </td> 
       <td> 
        <form:input path="noticesid" readonly="true" size="8" disabled="true" /> 
        <form:hidden path="noticesid" /> 
       </td> 
      </tr> 
     </c:if> 
     <tr> 
      <td> 
       <form:label path="notetext"> 
        <spring:message text="notetext"/> 
       </form:label> 
      </td> 
      <td> 
       <form:input path="notetext" /> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <form:label path="notetag" > 
        <spring:message text="notetag"/> 
       </form:label> 
      </td> 
      <td> 
       <form:input path="notetag"/> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <form:label path="notecolor"> 
        <spring:message text="notecolor"/> 
       </form:label> 
      </td> 
      <td> 
       <form:input path="notecolor" /> 
      </td> 
     </tr> 

     <tr> 
      <td> 
       <form:label path="canvasid"> 
        <spring:message text="canvasid"/> 
       </form:label> 
      </td> 
      <td> 
       <form:input path="canvasid" /> 
      </td> 
     </tr> 

     <tr> 
      <td> 
       <form:label path="sectionid"> 
        <spring:message text="sectionid"/> 
       </form:label> 
      </td> 
      <td> 
       <form:input path="sectionid" /> 
      </td> 
     </tr> 

     <tr> 
      <td> 
       <form:label path="canvasnName"> 
        <spring:message text="canvasnName"/> 
       </form:label> 
      </td> 
      <td> 
       <form:input path="canvasnName" /> 
      </td> 
     </tr> 


     <tr> 
      <td colspan="2"> 
       <c:if test="${!empty notices.noticesid}"> 
        <input type="submit" 
          value="<spring:message text="Edit note"/>" /> 
       </c:if> 
       <c:if test="${empty notices.notetext}"> 
        <input type="submit" 
          value="<spring:message text="Add note"/>" /> 
       </c:if> 
      </td> 
     </tr> 
    </table> 
</form:form> 
<br> 

<h3>Notes List</h3> 
<c:url var="listAction" value="/note/listing" ></c:url> 
<form:form action="${section1}" commandName="notices" method="post"> // Same code with section2 and section3 
<c:if test="${!empty notices.noticesid}"> 
    <table class="tg"> 
     <tr> 
      <th width="80">Notes ID</th> 
      <th width="120">Notes text</th> 
      <th width="120">Note Tag</th> 
      <th width="120">Note color</th> 
      <th width="120">Note section</th> 
      <th width="120">Canvas id</th> 
      <th width="120">Canvas name</th> 
      <th width="120">Other id</th> 
      <th width="60">Edit</th> 
      <th width="60">Delete</th> 
     </tr> 
     <c:forEach items="${section1}" var="notices"> 
      <tr> 
       <td>${notices.noticesid}</td> 
       <td>${notices.notetext}</td> 
       <td>${notices.notetag}</td> 
       <td>${notices.notecolor}</td> 
       <td>${notices.sectionid}</td> 
       <td>${notices.canvasid}</td> 
       <td>${notices.canvasnName}</td> 
       <td>${notices.personid}</td> 
       <td><a href="<c:url value='/editnote/${notices.noticesid}' />" >Edit</a></td> 
       <td><a href="<c:url value='/removenote/${notices.noticesid}' />" >Delete</a></td> 
      </tr> 
     </c:forEach> 
    </table> 
</c:if> 
</form:form> 

Debug screenshot

+0

你檢查過'stacktraces'。你甚至可以在瀏覽器控制檯本身找到這種錯誤 – 2014-11-03 09:25:59

+0

@SanKrish在stacktrace中沒有錯誤,在數據庫中,一切都按預期工作。只是它不刷新之前顯示。 – 2014-11-03 09:26:57

+0

所以當你點擊'submit'按鈕。它不更新?點擊提交按鈕後,您是否獲得了所需的網址 – 2014-11-03 09:28:23

回答

1

所以我能夠通過調用回終於解決它 「重定向:/筆記/列表」,在結束/ note/add功能。

1

要對這個答案完全清楚,只需創建:

ModelAndView控制器,並在控制器結束後,請加:

ModelAndView mv = new ModelAndView("redirect:/note/listing"); 
return mv;