2014-10-30 66 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