[英]RedirectView from Spring MVC dont works
我認為錯誤來自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";
}
如您所見,我必須為新 URL 更改@GetMapping
,現在該函數返回一個字符串,它是沒有擴展名的 JSP 的名稱。 除了@RequestParam
函數的參數之外,我不得不將其更改為@PathVariable
,其值與@GetMapping
的 mustache 表達式@GetMapping
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.