2014-10-30 27 views
7

Pracuję w aplikacji Spring MVC, która używa Hibernate.Przekazywanie parametrów z jsp do metody kontrolera Spring

Na stronie JSP mam funkcję, która zawiera wartości przechowywane w bazie danych (obecnie wszystkie wartości).

Napisałem metodę, w której lista jest ograniczona tylko do identyfikatora przekazanego w pliku JSP. Mam poprawnie działającą kwerendę HQL, więc wiem, że pobiera ona dane w oparciu o identyfikator jako parametr.

Teraz chciałbym użyć tej metody w kontrolerze. W tym celu muszę podać parametr ID do listy, więc po stronie kontrolera wywoływana jest funkcja, która pobierze listę na podstawie tego identyfikatora.

Niestety nie wiem, jak przekazać parametry z pliku JSP.

JSP pliku:

<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> 

plik Kontroler z funkcją listy:

@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"; 
    } 

Próbowałem patrząc w internecie, ale nie wiem jak to się nazywa, że ​​szukam, więc ciężko. Każda pomoc byłaby dobra. Dziękuję Ci.

+0

proszę wyjaśnić, co próbujesz zrobić w jasny sposób –

+0

@SanKrish: Zrewidowałem pytanie, życzę miłego spojrzenia. –

+0

Czy próbujesz przekazać "id" z jsp do kontrolera? –

Odpowiedz

5

Twoja metoda kontroler powinien być taki,

@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"; 
     } 

Użyj identyfikatora w kodzie.

wywołać metodę kontroler z JSP jako

/{Twój mapowanie}/{Twój id}

UPDATE:

zmiany kodu 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

Wygląda na to, że twoja metoda zadziała. Jeszcze jedna rzecz.W jaki sposób mogę wyodrębnić wartość @PathVariable i przypisać do int id (obecnie ustawiony na 2323). –

+0

Zmień typ danych identyfikatora na int. Zmodyfikowałem moją odpowiedź – rahul

+0

Dzięki. Czy możesz spojrzeć na dodany JSP i kontroler, wartość id zawsze pozostaje na zero. –

7

użyj parametru @RequestParam, aby przekazać parametr do metody obsługi kontrolera.

w JSP swoją postać powinna mieć pole wejściowe z name = „id” jak następuje:

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

następnie w kontrolerze, metodę procedury obsługi, powinny być tak:

@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"; 
    } 

Proszę również zapoznać się z tymi odpowiedziami here i here.

i ten samouczek: here.

Nadzieję, że pomaga.

+0

Czy możesz spojrzeć na zmodyfikowaną stronę JSP i kontroler, w jakiś sposób identyfikator w sterowniku zawsze pozostaje na poziomie zero. –

+0

masz na myśli po użyciu @requestParam? –

+0

Tak. W JSP próbowałem teraz użyć wartości statycznej, np./Note/list/2323. Wciąż pozostaje na zero. –

Powiązane problemy