Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 提交表单后返回上一页(Spring/Hibernate)_Java_Html_Spring_Hibernate_Jsp - Fatal编程技术网

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

Java 提交表单后返回上一页(Spring/Hibernate),java,html,spring,hibernate,jsp,Java,Html,Spring,Hibernate,Jsp,我有一个餐厅编辑页面位于“/restaurant/edit/{id}”。从该页面,我可以(除其他外)通过按下“添加桌子”按钮向餐厅添加桌子。该按钮将我带到另一个页面,该页面位于“/restaurant/edit/{id}/table”。问题是,在我添加了表格之后,如何通过按下按钮返回到上一页?现在,我的控制器返回“editRestaurant.jsp”,这是正确的值,但我也不知道如何传递相同的餐厅id。我希望你明白我的意思 My RestaurantTableController.java: @

我有一个餐厅编辑页面位于“/restaurant/edit/{id}”。从该页面,我可以(除其他外)通过按下“添加桌子”按钮向餐厅添加桌子。该按钮将我带到另一个页面,该页面位于“/restaurant/edit/{id}/table”。问题是,在我添加了表格之后,如何通过按下按钮返回到上一页?现在,我的控制器返回“editRestaurant.jsp”,这是正确的值,但我也不知道如何传递相同的餐厅id。我希望你明白我的意思

My 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";
    }

}
@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";
}
我的“newTable.jsp”:

“editRestaurant.jsp”:


编辑
餐厅:
地址:
${item.toString()}

可以使用不同的方法返回包含参数的模型。我相信这可以解决你的问题

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

您可以使用另一种方法返回包含参数的模型。我相信这可以解决你的问题

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

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

大概是这样的:

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


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

大概是这样的:

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

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