简体   繁体   English

提交表单后,返回上一页(春季/冬眠)

[英]After submitting a form go back to previous page (Spring/Hibernate)

I have a restaurant edit page located "/restaurant/edit/{id}". 我的餐厅编辑页面位于“ / restaurant / edit / {id}”。 From that page I can (among other things) add tables to the restaurant by pressing an "Add tables" button. 在该页面上,我可以通过按“添加表格”按钮将表格添加到餐厅中。 That button takes me to another page located "/restaurant/edit/{id}/table". 该按钮将我带到位于“ / restaurant / edit / {id} / table”的另一页。 The question is, after I have added the table - how do I get back to the previous page by pressing a button? 问题是,添加表格后-如何通过按按钮返回上一页? Right now my contoller is returning "editRestaurant.jsp" which is the right value, but I don't know how to pass that same restaurant id as well. 现在,我的contoller返回的是“ editRestaurant.jsp”,它是正确的值,但是我也不知道如何传递相同的餐厅ID。 I hope you understand what I mean. 我希望你明白我的意思。

My RestaurantTableController.java: 我的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";
    }

}

My "newTable.jsp": 我的“ 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>

Relevant methods in RestaurantController.java: 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": “ 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>

There is a different method you can use to return the model that will include the parameter. 您可以使用其他方法来返回将包含参数的模型。 I believe this may solve your problem. 我相信这可以解决您的问题。

@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);
}

After successful POST you should do a redirect. POST成功POST您应该进行重定向。

Something like this: 像这样:

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM