springboot重定向到页面,Spring Boot-重定向到其他控制器方法

I am creating a very basic application with SpringBoot and Thymeleaf. In the controller I have 2 methods as follows:

Method1 - This method displays all the data from the database:

@RequestMapping("/showData")

public String showData(Model model)

{

model.addAttribute("Data", dataRepo.findAll());

return "show_data";

}

Method2 - This method adds data to the database:

@RequestMapping(value = "/addData", method = RequestMethod.POST)

public String addData(@Valid Data data, BindingResult bindingResult, Model model) {

if (bindingResult.hasErrors()) {

return "add_data";

}

model.addAttribute("data", data);

investmentTypeRepo.save(data);

return "add_data.html";

}

HTML files are present corresponding to these methods i.e. show_data.html and add_data.html.

Once the addData method completes, I want to display all the data from the database. However, the above redirects the code to the static add_data.html page and the newly added data is not displayed. I need to somehow invoke the showData method on the controller so I need to redirect the user to the /showData URL. Is this possible? If so, how can this be done?

解决方案

Try this:

@RequestMapping(value = "/addData", method = RequestMethod.POST)

public String addData(@Valid Data data, BindingResult bindingResult, Model model) {

//your code

return "redirect:/showData";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值