我的餐厅编辑页面位于“/ restaurant / edit / {id}”。从该页面我可以(除其他外)通过按“添加表格”按钮向餐厅添加表格。该按钮将我带到另一个位于“/ restaurant / edit / {id} / table”的页面。问题是,在我添加表格后 - 如何通过按下按钮返回上一页?现在我的控制器正在返回“editRestaurant.jsp”,这是正确的值,但我不知道如何传递相同的餐厅ID。我希望你明白我的意思。
我的RestaurantTableController.java:
@Controller
public class RestaurantTableController {
@Autowired
private RestaurantService restaurantService;
@Autowired
private RestaurantTableService restaurantTableService;
@RequestMapping(value="restaurant/{id}/table", method = RequestMethod.GET)
public String addRestaurantTable(Model model, @PathVariable Long id) {
model.addAttribute("table", new RestaurantTable());
return "newTable";
}
@RequestMapping(value = "restaurant/{id}/table", method = RequestMethod.POST)
public String addRestaurantTableAction(@PathVariable Long id, @ModelAttribute ("table") RestaurantTable table, BindingResult result) {
RestaurantTableFormValidator restaurantTableFormValidator = new RestaurantTableFormValidator();
restaurantTableFormValidator.validate(table, result);
if (result.hasErrors()) {
return "newTable";
}
restaurantService.mergeRestaurant(id, table);
return "editRestaurant";
}
}
我的“newTable.jsp”:
<body>
<jsp:include page="../fragments/menu.jsp"/>
<div id="body">
<section class="content-wrapper main-content clear-fix">
<h2>Add New Table</h2>
<form:form method="POST" modelAttribute="table">
<table>
<tr>
<td>Table size:</td>
<td><form:input path="tableSize" /></td>
<td><form:errors path="tableSize" cssClass="error"/></td>
</tr>
<tr>
<td>Table number:</td>
<td><form:input path="tableNumber" /></td>
<td><form:errors path="tableNumber" cssClass="error"/></td>
</tr>
<tr>
<td colspan="3"><input type="submit" onclick="goback()"/>
</td>
</tr>
</table>
</form:form>
</section>
</div>
<jsp:include page="../fragments/footer.jsp"/>
</body>
RestaurantController.java中的相关方法:
@RequestMapping(value = "restaurant/edit/{id}", method = RequestMethod.GET)
public String editRestaurant(@PathVariable Long id, Model model) {
Restaurant restaurant = restaurantService.getRestaurant(id);
model.addAttribute("restaurant", restaurant);
return "editRestaurant";
}
@RequestMapping(value = "restaurant/edit/{id}", method = RequestMethod.POST, params="submit")
public String editRestaurant(@ModelAttribute ("restaurant") Restaurant restaurant, BindingResult result) {
RestaurantFormValidator restaurantFormValidator = new RestaurantFormValidator();
restaurantFormValidator.validate(restaurant, result);
if (result.hasErrors()) {
return "editRestaurant";
}
restaurantService.updateRestaurant(restaurant);
return "redirect:/bookings";
}
“editRestaurant.jsp”:
<div id="body">
<section class="content-wrapper main-content clear-fix">
<h2>Edit</h2>
<form:form method="POST" modelAttribute="restaurant" >
<table>
<tr>
<td>Restaurant:</td>
<td><form:input path="restaurantName" /></td>
<td><form:errors path="restaurantName" cssClass="error"/></td>
</tr>
<tr>
<td>Address:</td>
<td><form:input path="address" /></td>
<td><form:errors path="address" cssClass="error"/></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="Submit" name="submit"/>
</td>
</tr>
<tr>
<c:forEach items="${restaurant.table}" var="item">
<td>${item.toString()}</td>
</c:forEach>
</tr>
<tr>
<td><a href="/restaurant/${id}/table">Add Table</a></td>
</tr>
</table>
</form:form>
<div>
<a href="/bookings">Back to List</a>
</div>
</section>
</div>
答案 0 :(得分:1)
您可以使用不同的方法返回包含参数的模型。我相信这可以解决你的问题。
@RequestMapping(value = "restaurant/edit/{id}", method = RequestMethod.GET)
public String editRestaurant(@PathVariable Long id, Model model) {
Restaurant restaurant = restaurantService.getRestaurant(id);
return new ModelAndView("editRestaurant", "restaurant", restaurant);
}
答案 1 :(得分:1)
成功POST
后,您应该进行重定向。
这样的事情:
return "redirect:/restaurant/edit/" + restaurant.getId();
或
return new RedirectView("/restaurant/edit/" + restaurant.getId(), false);