zoukankan      html  css  js  c++  java
  • SpringMVC的数据响应和结果视图

    一、返回值分类

          1.字符串

               1)  controller 方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。          

    @RequestMapping("/testString")
        public String testString(Model model){
            System.out.println("testString-----");
            //模拟从数据库中查出的User对象
            User user =new User();
            user.setUsername("张三");
            user.setPassword("123");
            user.setAge(20);
            model.addAttribute("user",user);
            return "success";
        }
    <!--在success.jsp页面获取数据-->
    
    ${user.username}

     

          2.void        

    @RequestMapping("/testVoid")
        public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {
            System.out.println("testVoid-----");
            //1.转发
            //request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
            //2.重定向
            //  response.sendRedirect(request.getContextPath()+"/index.jsp");
            //设置中文乱码
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            //3.直接响应
            response.getWriter().write("你好");
            return;
        }

           3.ModelAndView

               1) ModelAndView 是 SpringMVC 为我们提供的一个对象,该对象也可以用作控制器方法的返回值

               2)使用示例

                  

      @RequestMapping("/testModelAndView")
        public ModelAndView testModelAndView(){
            //创建testModelAndView
            ModelAndView mv =new ModelAndView();
            System.out.println("testModelAndView-----");
            //模拟从数据库中查出的User对象
            User user =new User();
            user.setUsername("王五");
            user.setPassword("123");
            user.setAge(20);
            //把user对象存储到mv对象中,也会把user对象存到request域对象
            mv.addObject("user",user);
            //跳转到那个页面
            mv.setViewName("success");
            return mv;
        }

    //获取数据
    ${user.username}
    
    

            4.转发和重定向

                 1)使用示例

                    

     @RequestMapping("/testForwardOrRedirect")
        public String testForwardOrRedirect(Model model){
            System.out.println("testForwardOrRedirect-----");
            //1.请求转发
            //return "forward:/WEB-INF/pages/success.jsp";
            //2.重定向
            return "redirect:/index.jsp";
        }

            5.ResponseBody响应josn数据

                    1)使用说明

                        作用:该注解用于将 Controller 的方法返回的对象,通过 HttpMessageConverter 接口转换为指定格式的 数据如:json,xml 等,通过 Response 响应给客户端 。

                     2)使用示例

                           在springmvc中加入配置,不拦截静态资源

        <!--配置使得前端控制器,不拦截静态资源-->
    <!--        <mvc:resources mapping="/css/**" location="/css/"/>-->
    <!--        <mvc:resources mapping="/images/**" location="/images/"/>-->
        <mvc:resources location="/js/" mapping="/js/**"/>

          

     <script>
            // 页面加载,绑定单击事件
            $(function(){
                $("#btn").click(function(){
                    alert("hello btn");
                    // //发送ajax请求
                    $.ajax({
                        //编写json格式
                    url:"user/testAjax",
                    contentType:"application/json;charset=UTF-8",
                    data:'{"username":"张三","password":"123","age","20"}',
                    dataType:"json",
                    type:"post",
                    success:function (data) {
                        //data服务器端响应的json的数据,进行解析
                         alert(data);
                         alert(data.username);
                         alert(data.age);
                         alert(data.password);
    
                    }    
    
                    });
                });
            });
    
        </script>
     @RequestMapping("/testAjax")
        public @ResponseBody User testAjax(@RequestBody User user){
            System.out.println("testAjax-----");
            //客户端发送ajax请求,传的时json字符串,后端已经把json字符串分装到对象中
            System.out.println(user);
            //模拟响应
            user.setUsername("王二麻字");
            user.setAge(21);
            return user;
        }

              

               

          

  • 相关阅读:
    GC
    java基石-JVM
    golang 结构体指针及赋值
    golang 记一次map中struct的管道造成死锁的解决方式
    golang 封装"执行shell管理redis(string,集合等)"成api
    golang panic及处理
    Python简直无所不能!在电脑上如何调用手机摄像头?教你轻松搞定!
    2021最新版Python爬取抖音小姐姐短视频,无水印,超级详细!(附视频/源码)
    自从学会Python爬虫后,爬视频我只爬小姐姐!教你批量下载某短视频网站视频!
    突然不知道听什么歌了,但是排行榜的准没错,于是用Python全部都爬下来了!
  • 原文地址:https://www.cnblogs.com/cqyp/p/12525168.html
Copyright © 2011-2022 走看看