2014-10-30 60 views
7

我正在使用Hibernate的Spring MVC應用程序中工作。從jsp傳遞參數到Spring Controller方法

在JSP頁面中,我有一個列出存儲在數據庫中的值(當前是所有值)的函數。

我寫了一個方法,其中列表僅限於在JSP文件中傳遞的ID。我得到了HQL查詢正常工作,所以我知道它是基於ID作爲參數檢索數據。

現在,我想在控制器中使用這種方法。爲此,我必須將ID的參數傳遞給列表,因此在控制器端調用將根據該ID檢索列表的函數。

不幸的是我不知道如何從JSP文件傳遞參數。

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/list/2323" ></c:url> 
<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="${listNotes}" 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> 

Controller文件:

@RequestMapping(value = "/note/list/{id}", method=RequestMethod.GET) 
    public String listNotes(@PathVariable int id,Model model) { 
     Person person = personService.getCurrentlyAuthenticatedUser(); 
     this.setSectionid(id); 
     model.addAttribute("person", new Person()); 
     model.addAttribute("listPersons", this.personService.listPersons()); 
     model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person)); 
     return "note"; 
    } 

@RequestMapping(value= "/note/add") 
    public String addNote(@ModelAttribute("notices") Notes p,Model model) { 
     Person person = personService.getCurrentlyAuthenticatedUser(); 
     model.addAttribute("listNotes",this.notesService.listNotes()); 

     int id = getSectionid(); 
     System.out.println("Section id is"+id); 
     model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person)); 
     this.notesService.addNote(p, person); 
     return "note"; 
    } 

我試圖尋找了網,但我不知道它叫什麼,我找的,所以很難過。任何幫助都會很好。謝謝。

+0

請解釋一下您正在嘗試以清晰的方式做什麼 – 2014-10-30 12:13:43

+0

@SanKrish:我修改了這個問題,好心一看。 – 2014-10-30 12:18:26

+0

你是否試圖將'id'從jsp傳遞給控制器​​? – 2014-10-30 12:22:47

回答

5

你的控制器方法應該是這樣的,

@RequestMapping(value = " /<your mapping>/{id}", method=RequestMethod.GET) 
    public String listNotes(@PathVariable("id")int id,Model model) { 
      Person person = personService.getCurrentlyAuthenticatedUser(); 
      int id = 2323; // Currently passing static values for testing 
      model.addAttribute("person", new Person()); 
      model.addAttribute("listPersons", this.personService.listPersons()); 
      model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person)); 
      return "note"; 
     } 

使用該ID在你的代碼。

呼叫從JSP控制器方法

/{你的映射}/{您的ID}

UPDATE:

更改JSP代碼到

<c:forEach items="${listNotes}" var="notices" varStatus="status"> 
      <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/${listNotes[status.index].noticesid}' />" >Edit</a></td> 
       <td><a href="<c:url value='/removenote/${listNotes[status.index].noticesid}' />" >Delete</a></td> 
      </tr> 
     </c:forEach> 
+0

似乎你的方法正在工作。就一件事。我如何提取@PathVariable值並分配給int id(當前設置爲2323)。 – 2014-10-30 12:44:00

+0

將id的數據類型更改爲int。修改我的答案 – rahul 2014-10-30 13:09:44

+0

謝謝。你可以看看添加的JSP和Controller,id的值始終保持爲零。 – 2014-10-30 13:13:08

7

使用@RequestParam將參數傳遞給Controller處理程序方法。

在jsp表單應該具有名稱=「ID」的輸入字段類似如下:

<input type="text" name="id" /> 
<input type="submit" /> 

然後在控制器,你的處理程序方法,應是這樣的:

@RequestMapping("listNotes") 
public String listNotes(@RequestParam("id") int id) { 
     Person person = personService.getCurrentlyAuthenticatedUser(); 

     model.addAttribute("person", new Person()); 
     model.addAttribute("listPersons", this.personService.listPersons()); 
     model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person)); 
     return "note"; 
    } 

請同時參考這些回答herehere

和本教程here

希望有所幫助。

+0

你可以看看修改後的JSP和控制器,不知何故,控制器中的id始終保持爲零。 – 2014-10-30 13:12:43

+0

你的意思是在使用@requestParam之後? – 2014-10-30 13:14:53

+0

是的。在JSP中,我嘗試使用靜態值,比如/ note/list/2323。它仍然是零。 – 2014-10-30 13:15:54