gpt4 book ai didi

java - 重定向到接收 Controller 后传递 modelAndView 对象

转载 作者:行者123 更新时间:2023-12-02 09:03:36 25 4
gpt4 key购买 nike

我有一个后 Controller ,它接受输入表单字段,经过一些计算并检索数据库值后,它重定向到带有两个参数的获取 Controller 。

@RequestMapping(value = "/user/calculate", method = RequestMethod.POST )
public String compareClients(@Valid UserInfo calculate, BindingResult bindingResult) {

/*some calculations*/
long firstClient=calculate.getFirstId();
long secondClient=calculate.getSecondId();
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("firstInfo",firstInfo);
modelAndView.addObject("secondInfo",secondInfo);
modelAndView.addObject("infoId",id);

return "redirect:/user/calculate?firstId=" + firstClient + "&secondId=" + secondClient;

我想将firstInfo、secondInfo和infoId传递给另一个 Controller ,以便从那里我将其显示在thymeleaf上

@RequestMapping(value = "user/calculate", method = RequestMethod.GET, params = { "firstId", "secondId" })
public ModelAndView comparison(@RequestParam(value = "firstId", required = true) String firstId,@RequestParam(value = "secondId", required = true) String secondId) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("firstClient", firstId);
modelAndView.addObject("secondClient", secondId);
return modelAndView;
}

我该如何实现这一目标?请帮忙。我尝试使用转发而不是重定向,但响应时间太长,并且不会转发到获取 Controller 。无法使用 flashAttributes,因为如果刷新页面,我需要 Thymeleaf 上的信息。我无法通过 URL 传递所有 5 个对象。如果将这些内容隐藏起来,那就更好了。

最佳答案

我对 Thymeleaf 不太了解,但您可以使用 RedirectAttributes 将所需的值传递给另一个 Controller 。由于您的评论希望在刷新页面时保留该值。所以我给你一个使用 SessionAttributes

的例子
public class YourInfo{
private String firstInfo;
private String secondInfo;

//setter and getter
}

您的 Controller 类添加 session 属性注释

@Controller
@SessionAttributes("myInfo")

现在移至请求部分。

@RequestMapping(value = "/user/calculate", method = RequestMethod.POST )
public String compareClients(@Valid UserInfo calculate, BindingResult bindingResult, Model model) {

/*some calculations*/
long firstClient=calculate.getFirstId();
long secondClient=calculate.getSecondId();
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("firstInfo",firstClient);
modelAndView.addObject("secondInfo",secondClient);
modelAndView.addObject("infoId",id);
if(!model.containsAttribute("myInfo")){
YourInfo yourInfo = new YourInfo();
yourInfo.setFirstInfo(String.valueOf(firstClient));
yourInfo.setSecondInfo(String.valueOf(secondClient));
model.addAttribute("myInfo",yourInfo);
}

return "redirect:/user/calculate";
}

和你的获取 Controller (假设你的firstInfo和secondInfo将是字符串)

@RequestMapping(value = "user/calculate", method = RequestMethod.GET)
public ModelAndView comparison(@ModelAttribute("myInfo") YourInfo yourInfo) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("firstClient", yourInfo.getFirstInfo());
modelAndView.addObject("secondClient", yourInfo.getSecondInfo());
return modelAndView;
}

关于java - 重定向到接收 Controller 后传递 modelAndView 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46886498/

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