我在使用Hibernate的Spring MVC应用程序中工作。
在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>
带列表功能的控制器文件:
@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 :(得分:7)
使用@RequestParam将参数传递给控制器处理程序方法。
在jsp中,您的表单应具有name = "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";
}
请同时参考这些答案和教程:
答案 1 :(得分:6)
你的控制器方法应该是这样的:
@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调用控制器方法:
/{your mapping}/{your id}
<强>更新强>
将您的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>