spring boot请求重定向问题

碰到的问题:

上传文件后,跳转到展示页面,并打印出上传成功的message(即带参数的跳转)。然后方法如下,一直报错404,后来改了下路径。

@Controller
@RequestMapping("/file")
public class FileUploadController {
	@GetMapping("/")
	public String listUploadedFiles(Model model) throws IOException {
		model.addAttribute("files", ....));//这里是获取已上传列表
		return "uploadForm";
	}

	@PostMapping("/")
	public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {

		storageService.store(file);
		redirectAttributes
				.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!");
		return "redirect:/file/";//这里出了问题,原先是return "redirect:/";跳转到项目index.html了
	}
}

 

另外总结:

1、使用ModelAndView,return new ModelAndView("redirect:/toList");//这样可以重定向到toList这个方法

2、返回String,return "redirect:/ toList "; 与上面一样的效果。

3、就是上面的例子,使用RedirectAttributes对象。 

 

除此之外,对于spring boot的路径问题:

假如访问http://localhost:8080会自动显示index.html,而访问http://localhost:8080/会去找映射处理器并返回,而处理器方法中,比如上面的例子返回String页面名的,会默认在resources/templates路径下找对应的页面。

猜你喜欢

转载自1181731633.iteye.com/blog/2381163
今日推荐