zoukankan      html  css  js  c++  java
  • springMVC中controller的几种返回类型

    ==网文1,还不错,感觉比较老旧
    springMVC中controller的几种返回类型 - CSDN博客
    http://blog.csdn.net/qq_16071145/article/details/51222372

    Controller方法的返回值可以有以下几种: 1、返回ModelAndView 返回ModelAndView时最常见的一种返回结果。需要在方法结束的时候定义一个ModelAndView对象,并对Model和View分别进行设置。 2、返回String 1):字符串代表逻辑视图名 真实的访问路径=“前缀”+逻辑视图名+“后缀” 注意:如果返回的String代表逻辑视图名的话,那么Model的返回方式如下: public String testController(Model model){ model.addAttribute(attrName,attrValue);//相当于ModelAndView的addObject方法 return "逻辑视图名"; } 2):代表redirect重定向 redirect的特点和servlet一样,使用redirect进行重定向那么地址栏中的URL会发生变化,同时不会携带上一次的request 案例: public String testController(Model model){ return "redirect:path";//path代表重定向的地址 } 3):代表forward转发 通过forward进行转发,地址栏中的URL不会发生改变,同时会将上一次的request携带到写一次请求中去 案例: public String testController(Model model){ return "forward:path";//path代表转发的地址 } 3、返回void 返回这种结果的时候可以在Controller方法的形参中定义HTTPServletRequest和HTTPServletResponse对象进行请求的接收和响应 1)使用request转发页面 request.getRequestDispatcher("转发路径").forward(request,response); 2)使用response进行页面重定向 response.sendRedirect("重定向路径"); 3)也可以使用response指定响应结果 response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=utf-8"); response.getWriter.write("json串"); 以上三种返回值没有什么重要和不重要的分别,一般来说都会使用到, 只不过有的时候使用的方式会有一些细微的差别

    ==网文2 此文有代码,对上方的拓展

    SpringMVC学习(七)——Controller类的方法返回值 - CSDN博客
    http://blog.csdn.net/yerenyuan_pku/article/details/72511844

    == 很多返回类型很陌生

    springMVC教程(八)controller中方法的返回值类型 - CSDN博客
    http://blog.csdn.net/baidu_16702581/article/details/32695519

    == 很不错,但是里面多了很多<span> 多余的标签了,应该是博主从其他地方拷贝过来的

    SpringMVC的Controller层参数绑定以及返回值 - CSDN博客
    http://blog.csdn.net/nuowei_senlin/article/details/53695956

    当客户端通过get或post请求发送来的参数通过Controller中方法的参数接受,叫做参数绑定
    Controller方法的返回值1:返回void类型
    
    @RequestMapping("/test_void.action")  
    public void controller01(HttpServletRequest request,HttpServletResponse response) throws Exception{  
    request.setCharacterEncoding("utf-8");  
    String username = request.getParameter("username");//通过HttpServletRequest获得请求参数  
        System.out.println("用户名:"+username);  
        request.setAttribute("username",username);  
        User u = new User();  
    u.setUsername(username);  
    userService.insertUser(u);//插入数据库  
        request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);//转发  
    }  
    Controller方法的返回值2:返回ModelAndView
    
    @RequestMapping("/test_modelandview.action")  
    public ModelAndView controller02(HttpServletRequest request) throws Exception{  
        request.setCharacterEncoding("utf-8");//转码,Tomcat默认是iso-8859-1  
        String username = request.getParameter("username");  
        System.out.println("用户名:"+username);  
        ModelAndView modelAndView = new ModelAndView();//new一个ModelAndView  
        modelAndView.addObject("username",username);//相当于request.setAttribute(attrName,attrValue);  
        modelAndView.setViewName("WEB-INF/jsp/success.jsp");//视图跳转  
        return modelAndView;  
    }  
    Controller方法的返回值3:返回String类型(逻辑视图)
    
    @RequestMapping("/test_string.action")  
    public String controller03(HttpServletRequest request) throws Exception{  
        request.setCharacterEncoding("utf-8");//转码  
        String username = request.getParameter("username");  
        request.setAttribute("username",username);//设置请求参数  
        System.out.println("用户名:"+username);  
        return "/WEB-INF/jsp/success.jsp";//返回String类型,代表逻辑视图  
    }     
    Controller方法的返回值4:方法的参数是Model,返回值是String类型(逻辑视图)
    [html] view plain copy
    @RequestMapping("/test_model.action")  
    public String controller04(HttpServletRequest request,Model model) throws Exception{  
    request.setCharacterEncoding("utf-8");  
    String username = request.getParameter("username");  
    model.addAttribute("username", username);//等价于request.setAttribute(attrName,attrValue);  
    System.out.println("用户名:"+username);  
    return "/WEB-INF/jsp/success.jsp";//返回String类型,跳转到逻辑视图  
    }  
    Controller方法的返回值5:返回重定向redirect后的逻辑视图名
    
    @RequestMapping("/test_redirect.action")  
    public String controller05(HttpServletRequest request) throws Exception{  
        request.setCharacterEncoding("utf-8");  
        String username = request.getParameter("username");  
        User u = new User();  
        u.setUsername(username);  
        userService.insertUser(u);  
        request.setAttribute("username",username);//由于是redirect,所以请求参数失效  
        return "redirect:/controller/test_model.action";//redirect:重定向到一个Controller里  
    }  
    Controller方法的返回值6:返回farward转发后的逻辑视图名
    
    @RequestMapping("/test_forword.action")  
        public String controller06(HttpServletRequest request) throws Exception{  
        request.setCharacterEncoding("utf-8");  
        String username = request.getParameter("username");  
        User u = new User();  
        u.setUsername(username);  
        userService.insertUser(u);  
        System.out.println("用户名:"+username);  
        request.setAttribute("username",username);//由于是转发,所以请求参数有效  
        return "forward:/controller/test_model.action";//转发,跳转到一个Controller里  
    }  
    
    参数绑定
    参数绑定的第一种方法:绑定普通类型
    
    //参数绑定的第一种方法:客户端提交的请求的input的name属性会和Controller方法的参数名字一致才会绑定  
    @RequestMapping("/test_parambinding01.action")  
    public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws Exception{  
        //必须进行转码  
        username = new String(username.getBytes("iso8859-1"),"UTF-8");  
        request.setAttribute("username",username);  
        request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);      
    }  
    
    参数绑定的第二种方法:绑定pojo类
    
    //参数绑定的第二种方法:客户端的input标签的那么属性必须和User的属性名对应才可以映射成功  
    @RequestMapping("/test_parambinding02.action")  
    public ModelAndView controller08(HttpServletRequest request,User user) throws Exception{  
        //必须进行转码  
        user.setUsername(new String(user.getUsername().getBytes("iso-8859-1"),"utf-8"));  
        userService.insertUser(user);  
        ModelAndView modelAndView = new ModelAndView();  
        request.setCharacterEncoding("utf-8");  
        modelAndView.addObject("username",user.getUsername());  
        modelAndView.addObject("password",user.getPassword());  
        modelAndView.setViewName("/WEB-INF/jsp/success.jsp");  
        return modelAndView;  
    }  
    
    参数绑定的第三种方法:当input的name与controller的参数名不一致时,可以采用@RequestParam注解
    
    @RequestMapping("test_RequestParam.action")  
    //将客户端的请求参数"username"与"uname"绑定  
    public ModelAndView controller09(@RequestParam(value="username") String uname,@RequestParam(value="password")String pwd) throws Exception{  
        uname = new String(uname.getBytes("iso-8859-1"),"utf-8");     
        ModelAndView modelAndView = new ModelAndView();  
        modelAndView.addObject("username",uname);  
        modelAndView.setViewName("/WEB-INF/jsp/success.jsp");  
        return modelAndView;  
    }  
    原文

    ===此文 里面的小结感觉很实用,但本人理解不完全

    SpringMVC Controller 返回值的可选类型 - xiepeixing - 博客园
    http://www.cnblogs.com/xiepeixing/p/4243801.html

    小结:
    1.使用 String 作为请求处理方法的返回值类型是比较通用的方法,这样返回的逻辑视图名不会和请求 URL 绑定,具有很大的灵活性,而模型数据又可以通过 ModelMap 控制。
    2.使用void,map,Model 时,返回对应的逻辑视图名称真实url为:prefix前缀+视图名称 +suffix后缀组成。
    3.使用String,ModelAndView返回视图名称可以不受请求的url绑定,ModelAndView可以设置返回的视图名称。

  • 相关阅读:
    Spring--AOP--面向切面编程
    Spring ---annotation (重点)--Resource, Component 重要!!!
    Spring ---annotation (重点)--AutoWired 不常用
    ts 交集类型
    ts 使用 keyof typeof
    Dart 编写Api弃用警告
    js 反应&行动
    perl 打印简单的help文档
    perl 在windows上获取当前桌面壁纸
    perl 打印目录结构
  • 原文地址:https://www.cnblogs.com/rogge7/p/7767133.html
Copyright © 2011-2022 走看看