SpringMVC笔记——controller返回值

来源:互联网 发布:ipad监控软件 编辑:程序博客网 时间:2024/05/01 23:56

1. 返回ModelAndView

    @RequestMapping(method=RequestMethod.GET)      public ModelAndView index(){          ModelAndView modelAndView = new ModelAndView("");          modelAndView.addObject("name", "xxx");          return modelAndView;      } 

可以通过 ModelAndView构造函数可以指定返回页面的名称,也可以通过setViewName方法来设置所需要跳转的页面;

2. 返回Model

一个模型对象,主要包含spring封装好的model和modelMap,以及Java.util.Map,当没有视图返回的时候视图名称将由requestToViewNameTranslator决定;Model 是一个接口, 其实现类为ExtendedModelMap,继承了ModelMap类。

//响应的view应该也是该请求的view。等同于void返回。    //返回值是map    @RequestMapping(value="index.do",params="type=map")      public Map<String, String> index3(){          Map<String, String> map = new HashMap<String, String>();          map.put("msg","这里是Map中的数据");        //在jsp页面中可直通过${msg}获得到值,map.put相当于request.setAttribute方法          return map;      }     //返回值是ModelMap   @RequestMapping(value="index.do",params="type=modelMap")   public ModelMap resultModelMap(ModelMap map){       map.put("msg","这里是modleMap中的数据");       return map;}

3. 返回String

  • 返回逻辑视图名
    //配合model进行使用      @RequestMapping(method = RequestMethod.GET)      public String index(Model model) {          User user = new User();          user.setName("XXX");          model.addAttribute("user", user);  //通过形参中的model将model数据传到页面,相当于modelandview.addObject        return "user/index";      }
  • 注解@ResponseBody

    该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
    返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

    @RequestMapping("/login")    @ResponseBody   public User login(User user){    return user;   }   //User字段:userName pwd    // 那么在前台接收到的数据为:'{"userName":"xxx","pwd":"xxx"}'    //效果等同于如下代码:    @RequestMapping("/login")   public void login(User user, HttpServletResponse response){    response.getWriter.write(JSONObject.fromObject(user).toString());   }
  • redirect重定向

    redirect的特点和servlet一样,使用redirect进行重定向那么地址栏中的URL会发生变化,同时不会携带上一次的request

    @RequestMapping(method = RequestMethod.GET)      public String index(Model model) {          User user = new User();          user.setName("XXX");          model.addAttribute("user", user);          return "redirect:user/index.do";      }     // 带参数可使用RedirectAttributes参数进行传递:    @RequestMapping(value="/redirect",method=RequestMethod.GET)      public String testRedirect(RedirectAttributes attr){           attr.addAttribute("a", "a");           attr.addFlashAttribute("b", "b");           return "redirect:/index.do";      }       /*注意:    1.使用RedirectAttributes的addAttribute方法传递参数会跟随在URL后面,如上代码即为http:/index.action?a=a    2.使用addFlashAttribute不会跟随在URL后面,会把该参数值暂时保存于session,待重定向url获取该参数后从session中移除,这里的redirect必须是方法映射路径,jsp无效。你会发现redirect后的jsp页面中b只会出现一次,刷新后b再也不会出现了,这验证了上面说的,b被访问后就会从session中移除。对于重复提交可以使用此来完成.*/
  • forward页面转发

    通过forward进行转发,地址栏中的URL不会发生改变,同时会将上一次的request携带到写一次请求中去

    @RequestMapping("/login.do")    public String login(HttpServletRequest request,HttpServletResponse response){    request.setAttribute("message", "hello");    return "forward:/index.do";  //forward在跳转后可以取到message值    }    @RequestMapping("/index.do")    public String index(HttpServletRequest request,HttpServletResponse response){    return "welcome";    }

4 . 返回void

只是纯粹的执行了方法中的程序,然后响应的url依然为请求的url

    @RequestMapping(method=RequestMethod.GET)    public void index(){        ModelAndView modelAndView = new ModelAndView();        modelAndView.addObject("xxx", "xxx");    }
0 0