我认为错误来自于 RedirectView
参数中引入的 ulr,但我尝试了不同的 URL,但无法使其工作。
这是我的项目结构:
这是 Controller 包中 Spring MVC 的 get 方法
@GetMapping("/planetForm")
@ResponseBody
public RedirectView toPlanetForm(Model model, @RequestParam(value = "idPlanet", required = false) String idPlanet) {
if (idPlanet != null){
Planet planet = planetService.getById(Integer.parseInt(idPlanet));
model.addAttribute("planet", planet);
}
List<Satellite> satellites = satelliteService.findAll();
model.addAttribute("satellites", satellites);
return new RedirectView("./addPlanet");
}
如您所见,我正在尝试对名为 addPlanet.jsp 的 JSP 执行 RedirectView。
我也尝试过这个网址(除其他外):“../webapp/WEB-INF/jsp/addPlanet.jsp”
,我确信这是菜鸟废话,但我无法看到错误
我也尝试过创建另一个servlet并通过@ModelAtribute
检索我需要的对象,但是在第一个servlet中添加的对象在此过程中丢失了,这是代码:
@GetMapping("/updatePlanet")
@ResponseBody
String update(Model model, @ModelAttribute("planet") Planet planet){
model.addAttribute("planet",planet);
return "addPlanet";
}
该wat将用户引导至addPlanet.jSTL
,但该方法获取的对象是构造函数刚刚创建的对象,其属性值为null或0
最佳答案
终于找到解决方案了,这里是:
@GetMapping("/planetForm/{idPlanet}")
public String toPlanetForm(Model model, @PathVariable(value = "idPlanet", required = false) String idPlanet) {
if (idPlanet != null){
Planet planet = planetService.getById(Integer.parseInt(idPlanet));
model.addAttribute("planet", planet);
}
List<Satellite> satellites = satelliteService.findAll();
model.addAttribute("satellites", satellites);
return "addPlanet";
}
正如您所看到的,我必须将 @GetMapping
更改为新 URL,现在该函数返回一个字符串,它是不带扩展名的 JSP 名称。除了函数@RequestParam
的参数之外,我还必须将其更改为@PathVariable
,其值与@GetMapping
关于java - Spring MVC 中的 RedirectView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60021598/