2014-11-03 103 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;