spring 将参数从jsp传递到Spring Controller方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26652679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing parameters from jsp to Spring Controller method
提问by We are Borg
I am working in a Spring MVC application which uses Hibernate.
我在使用 Hibernate 的 Spring MVC 应用程序中工作。
In the JSP page I have a function which lists the values stored in the database(currently all the value).
在 JSP 页面中,我有一个函数可以列出存储在数据库中的值(当前是所有值)。
I have written a method, where the list is only limited to an ID passed in the JSP file. I got the HQL query working right, so I know it is retrieving data based upon the ID as a parameter.
我写了一个方法,其中列表仅限于在 JSP 文件中传递的 ID。我让 HQL 查询正常工作,所以我知道它正在根据 ID 作为参数检索数据。
Now, I would like to use this method in the controller. For that I have to pass a parameter of ID to the list, so in the controller side, the function is called which will retrieve list based upon that ID.
现在,我想在控制器中使用这个方法。为此,我必须将 ID 的参数传递给列表,因此在控制器端,调用该函数将根据该 ID 检索列表。
Unfortunately I don't know how to pass parameters from a JSP file.
不幸的是,我不知道如何从 JSP 文件传递参数。
JSP File :
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 file with list function :
具有列表功能的控制器文件:
@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";
}
I tried looking up the net, but I don't know what it is called that I am looking for, so having a hard time. Any help would be good. Thank you.
我试着在网上查了一下,但我不知道我在找什么,所以很难。任何帮助都会很好。谢谢你。
采纳答案by rahul
Your controller method should be like this:
你的控制器方法应该是这样的:
@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";
}
Use the idin your code, call the controller method from your JSP as:
使用id在你的代码,请从JSP作为呼叫控制器的方法:
/{your mapping}/{your id}
UPDATE:
更新:
Change your jsp code to:
将您的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>
回答by We are Borg
Use the @RequestParamto pass a parameter to the controller handler method.
In the jsp your form should have an input field with name = "id"like the following:
使用@RequestParam将参数传递给控制器处理程序方法。在 jsp 中,您的表单应该有一个输入字段,name = "id"如下所示:
<input type="text" name="id" />
<input type="submit" />
Then in your controller, your handler method should be like the following:
然后在您的控制器中,您的处理程序方法应如下所示:
@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";
}
Please also refer to these answers and tutorial:
另请参阅这些答案和教程:

