springmvc重定向(springmvc教程三)
阿新 • • 發佈:2018-11-28
目錄
工程程式碼
github: https://github.com/dengjili/springmvc
頁面檔案:redirect.jsp
<html> <body> <h2>Hello World!</h2> hehe <hr/> <a href="add">add</a> <hr/> <a href="add2">add2</a> <hr/> <a href="add3">add3</a> <hr/> <a href="add4">add4</a> <hr/> <a href="add5">add5</a> </body> </html>
普通引數跳轉
@RequestMapping("/add")
public ModelAndView add() {
// 資料域
ModelAndView mv = new ModelAndView();
mv.addObject("id", "1");
mv.addObject("name", "12");
mv.addObject("age", "135");
mv.setViewName("redirect:./show");
return mv;
}
// 被跳轉到
@RequestMapping("/show") public ModelAndView show(String id, String name, String age) { logger.debug("id: {}", id); logger.debug("name: {}", name); logger.debug("age: {}", age); ModelAndView mv = new ModelAndView(); mv.setViewName("redirect"); return mv; }
普通引數跳轉
@RequestMapping("/add")
public ModelAndView add() {
// 資料域
ModelAndView mv = new ModelAndView();
mv.addObject("id", "1");
mv.addObject("name", "12");
mv.addObject("age", "135");
mv.setViewName("redirect:./show");
return mv;
}
// 被跳轉到頁面
@RequestMapping("/show") public ModelAndView show(String id, String name, String age) { logger.debug("id: {}", id); logger.debug("name: {}", name); logger.debug("age: {}", age); ModelAndView mv = new ModelAndView(); mv.setViewName("redirect"); return mv; }
字串返回頁面方式,跳轉,正確方式
@RequestMapping("/add2")
public String add2(Model m) {
// 資料域
m.addAttribute("id", "1");
m.addAttribute("name", "12");
m.addAttribute("age", "135");
return "redirect:./show";
}
// 被跳轉到頁面
@RequestMapping("/show")
public ModelAndView show(String id, String name, String age) {
logger.debug("id: {}", id);
logger.debug("name: {}", name);
logger.debug("age: {}", age);
ModelAndView mv = new ModelAndView();
mv.setViewName("redirect");
return mv;
}
字串返回頁面方式,跳轉,錯誤方式
// 重定向頁面,無法獲取到值,ModelAndView必須使用向後傳遞方式,如add3方法
@RequestMapping("/add3")
public String add3(ModelAndView mv) {
// 資料域
mv.addObject("id", "1");
mv.addObject("name", "12");
mv.addObject("age", "135");
return "redirect:./show";
}
// 被跳轉到頁面
@RequestMapping("/show")
public ModelAndView show(String id, String name, String age) {
logger.debug("id: {}", id);
logger.debug("name: {}", name);
logger.debug("age: {}", age);
ModelAndView mv = new ModelAndView();
mv.setViewName("redirect");
return mv;
}
正常情況是無法傳遞pojo物件的,只能傳遞簡單的引數,跳轉,錯誤方式
@RequestMapping("/add4")
public String add4(Model m) {
BeanParam param = new BeanParam();
param.setName("張三");
param.setNote("test");
// 資料域
m.addAttribute("param", param);
return "redirect:./showPojo";
}
// 被跳轉到頁面
@RequestMapping("/showPojo")
public ModelAndView showPojo(BeanParam param ) {
logger.debug("name: {}", param.getName());
ModelAndView mv = new ModelAndView();
mv.setViewName("redirect");
return mv;
}
RedirectAttributes方式,跳轉,正常方式
@RequestMapping("/add5")
public String add5(RedirectAttributes ra) {
BeanParam param = new BeanParam();
param.setName("張三");
param.setNote("test");
// 資料域
ra.addFlashAttribute("param", param);
return "redirect:./showPojo2";
}
// 被跳轉到頁面
// 需要使用@ModelAttribute來接收引數
@RequestMapping("/showPojo2")
public ModelAndView showPojo2(@ModelAttribute("param") BeanParam param ) {
logger.debug("name: {}", param.getName());
ModelAndView mv = new ModelAndView();
mv.setViewName("redirect");
return mv;
}