[英]Spring MVC: @PathVariable form
I have a data base which contains some items. 我有一个包含一些项目的数据库。 I want to create a form which edits item with some id. 我想创建一个用一些id编辑项目的表单。 I did it, form opens fine. 我做到了,表格打开很好。 Adress is /itemproject/edit_item/{id} Problems start when I'm trying to activate POST method. 地址是/ itemproject / edit_item / {id}当我尝试激活POST方法时,问题就开始了。 Instead of directing me to page with item list (/itemproject/view_items) programm sends me to /itemproject/edit_item/edit_item. 而不是将我引导到带有项目列表的页面(/ itemproject / view_items),programm将我发送到/ itemproject / edit_item / edit_item。 itemproject is context path (for example). itemproject是上下文路径(例如)。
@RequestMapping(value = "/edit_item/{id}", method = RequestMethod.GET)
public String editItem(@PathVariable("id") Integer id, Model model) {
Item item;
item = dbService.findItem(item).get(0);
model.addAttribute("item", item);
return "edit_item";
}
@RequestMapping(value = "/edit_item/{id}", method = RequestMethod.POST)
public String editItemComplete(@PathVariable("id") Integer id, @ModelAttribute("item") Item item, Model model) {
dbService.updateItem(item);
model.addAttribute("items",dbService.findAllItems());
return "view_items";
}
dbService works with data base. dbService使用数据库。
I want that programm sent me to list of all items after ediding chosen item and updating it in database. 我希望该程序在编辑所选项目并在数据库中更新后将我发送到所有项目的列表。 Here is example of edit form (url: /itemproject/edit_item/{id} 以下是编辑表单的示例(url:/ itemproject / edit_item / {id}
<spring:url value="edit_item" var="formURL"/>
<form:form action="${formURL}"
method="post" cssClass="col-md-8 col-md-offset-2"
modelAttribute="item"
>
<div class="form-group">
<label for="item-stuff">Stuff</label>
<form:input id="item-stuff"
cssClass="form-control"
path="stuff"/>
</div>
<button type="submit" class="btn btn-default">Edit item</button>
</form:form>
This is how my item list page looks (url: /itemproject/view_items) 这是我的项目列表页面的外观(url:/ itemproject / view_items)
<body>
<table class="table table-hover">
<tbody>
<tr>
<th>Stuff</th>
</tr>
<c:forEach items="${items}" var="item">
<tr>
<td><a href="/itemproject/item/${item.id}">${item.stuff}</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
From Spring docs: 来自Spring docs:
In Spring MVC you can use the
@PathVariable
annotation on a method argument to bind it to the value of a URI template variable 在Spring MVC中,您可以在方法参数上使用@PathVariable
批注将其绑定到URI模板变量的值
That means that @PathVariable
annotation is suitable when you use the GET method because when you use GET method you can pass your query string. 这意味着当您使用GET方法时, @PathVariable
注释是合适的,因为当您使用GET方法时,您可以传递查询字符串。
Instead, try to use @RequestBody
in order to try to bind your POST HTTP body message to your parameter 相反,尝试使用@RequestBody
以尝试将POST HTTP正文消息绑定到您的参数
For example: 例如:
@RequestMapping(value = "/edit_item", method = RequestMethod.POST)
public String editItemComplete(@RequestBody String body) {
//in here you'll have to pull the body content
return "view_items";
}
Let's say that you're sending an Integer id on HTTP POST body, then you can pull the data from the body like this: 假设您在HTTP POST主体上发送了一个Integer id,那么您可以从主体中提取数据,如下所示:
@RequestMapping(value = "/edit_item", method = RequestMethod.POST)
public String editItemComplete(@RequestBody String body) {
ObjectMapper objectMapper = new ObjectMapper();
try {
idJson = objectMapper.readTree(body).path("id").asInt();
} catch (IOException e) {
e.printStackTrace();
}
return "view_items";
}
assuming that you're sending json from client to service. 假设您正在从客户端向服务发送json。
您可以返回"redirect:/itemproject/view_items"
而不是加载项并返回view_items
模板,这将导致调用view_items
的处理程序,这将加载项目等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.