[学习笔记]基于注解的spring3.0.x MVC学习笔记(六)

近期心情不太好,想起当初有些事情,继续springmvc的返回类型中带有redirect方式的使用,使用redirect方式返回跟有3种方式首先介绍第一种,代码如下:

   1:  @RequestMapping("/redirect")
   2:      public String testRedirect(ModelMap map){
   3:  //        Map<String,Object> map = new HashMap<String, Object>();
   4:          map.put("testdata", "hello world!");
   5:          return "redirect:redirect.jsp";
   6:      }

使用redirect后他不会对modelmap中的数据进行调用request.setAttribute而采用带参数的形式进行传值,如下图:

 

2C30F2D8-1820-44C6-8BD9-2AE75CBDD915

再页面中调用request.getParameter("testdata")后得到以下结果:

DE9DA99C-C0DF-4A05-881E-C344FEC34D61

jsp中的代码如下:

   1:  <%@ page language="java" contentType="text/html; charset=GB18030"
   2:      pageEncoding="GB18030"%>
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
   4:  <html>
   5:  <head>
   6:  <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
   7:  <title>跳转过来的jsp</title>
   8:  </head>
   9:  <body>
  10:  ${testdata}
  11:  :
  12:  <%=request.getParameter("testdata")+"----" %>
  13:  </body>
  14:  </html>

事实证明使用了redirect后就变成了带参传值.

另外一种则是可以返回一个链接代码如下:

片段代码1:

   1:  @RequestMapping(value="/redirect",method=RequestMethod.GET)
   2:      public String testRedirect(ModelMap map){
   3:  //        Map<String,Object> map = new HashMap<String, Object>();
   4:          map.put("testdata", "redirect:hello world!");
   5:          return "redirect:forward";
   6:      }
片段代码2:
   1:  @RequestMapping("/forward")
   2:      public String testForword(ModelMap map){
   3:  //        Map<String,Object> map = new HashMap<String, Object>();
   4:          map.put("testdata", "forward to:hello world!");
   5:          return "forward:collection1";
   6:      }
片段代码3
   1:  @RequestMapping("/collection1")
   2:      public Collection collection1() {
   3:          logger.info("collection is runing1");
   4:          List toys = new ArrayList();
   5:          toys.add("gg5555555");
   6:          toys.add(new UserBean(1, "a", "b"));
   7:          toys.add(new UserBean(2, "PSP", "2100"));
   8:   
   9:          return toys;
  10:      }

在浏览器上运行/redirect得到结果如下:

979F5665-5F89-42EA-BFAA-9166FFDD3ACF

很明显运行/redirect跳转到forward中然后forward中又引用了collection1所以才变成这样.

最终一种则是跳转到外部链接:代码如下:

   1:  @RequestMapping(value="/redirect",method=RequestMethod.GET)
   2:      public String testRedirect(ModelMap map){
   3:  //        Map<String,Object> map = new HashMap<String, Object>();
   4:          map.put("testdata", "redirect:hello world!");
   5:          return "redirect:http://blog.163.com/edwardlauxh";
   6:      }

可以直接跳转到外部链接中,跳转到外部链接,在modelmap中所保存的值也会一直带过去所对应的网站.

posted @ 2011-01-18 22:16  EdwardLau  阅读(3784)  评论(0编辑  收藏  举报