gpt4 book ai didi

java - 提交表单后返回上一页(Spring/Hibernate)

转载 作者:行者123 更新时间:2023-12-02 03:45:10 26 4
gpt4 key购买 nike

我有一个餐厅编辑页面,位于“/restaurant/edit/{id}”。在该页面上,我可以(除其他外)通过按“添加 table ”按钮向餐厅添加 table 。该按钮将我带到位于“/restaurant/edit/{id}/table”的另一个页面。问题是,添加表格后 - 如何通过按按钮返回到上一页?现在我的 Controller 正在返回“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>

最佳答案

成功POST后,您应该执行重定向。

类似这样的事情:

return "redirect:/restaurant/edit/" + restaurant.getId();

return new RedirectView("/restaurant/edit/" + restaurant.getId(), false);

关于java - 提交表单后返回上一页(Spring/Hibernate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36362846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com