我认为故障是由于RedirectView
参数中引入的ulr引起的,但是我尝试了不同的URL,但无法使其正常工作。
这是我的项目结构:
这是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的,现在该函数返回一个String,它是不带扩展名的JSP的名称。除了函数的参数外,@RequestParam
我还必须将其更改为@PathVariable
,其值与的小胡子表达式相同@GetMapping