【SpringMVC学习笔记(五)!!敲黑板!!】SpringMVC返回值类型及响应数据类型

使用Model对象

这里我们可以使用Model对象,就类似于setAttribute的方式来设置键值对,存储在request域,然后jsp页面的话,就可以直接通过 ${user.username}、${user.password}、${user.age} 来进行显示,但是要注意不要忽略EL表达式,要打开支持,使用 isELIgnored = "false" 打开对EL表达式的支持:

    /**
     * 返回值类型为字符串
     * @param model
     * @return
     */
    @RequestMapping("/testString")
    public String testString(Model model){
        System.out.println("测试方法执行了AAA");

        //模拟查询到用户
        User user = new User();
        user.setUsername("妹妹");
        user.setPassword("123456");
        user.setAge(30);

        //model对象,存到request域
        model.addAttribute("user",user);

        return "success";
    }

使用request、response、response.getWriter()、

注意:request跟response不是同一个目录,response引用的是servlet的一个路径,而不是根目录,而request引用的直接是根目录,所以request可以直接指定到项目的根路径,而response在重定向的时候要同时使用request.getContextPath()。

这里主要是在参数列表中调用了 HttpServletRequest 和 HttpServletResponse 两个原生接口。或者我们还可以使用response.getWriter.write(" ")的方式来进行跳转。一共有三种方式:

    /**
     * 返回值类型为void
     * 请求转发一次请求,不用编写项目的名称
     *
     */
    @RequestMapping("/testVoid")
    public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("成功");
        //编写请求转发的程序,手动调用转发的方法的话,就不会自动调用视图解析器,这里一定要加 “/success.jsp”
        request.getRequestDispatcher("/success.jsp").forward(request,response);

        //重新发新请求
        response.sendRedirect(request.getContextPath() + "/success.jsp");

        //修改编码格式
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset-utf-8");

        //直接进行响应
        response.getWriter().write("您好");

        return;
    }

使用ModelAndView对象

注意:ModelAndView中在addObject的时候,也是根据键值对的方式,把数据存入request域的

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        System.out.println("testModelAndView执行了");

        //创建 ModelAndView 对象
        ModelAndView modelAndView = new ModelAndView();

        //模拟查询到用户
        User user = new User();
        user.setUsername("妹妹");
        user.setPassword("123456");
        user.setAge(30);

        //把user对象存入modelAndView对象中,这个也会把user对象存入request域对象
        modelAndView.addObject("user",user);

        //想跳转到那个页面,这里调用的是视图解析器
        modelAndView.setViewName("success");

        return modelAndView;
    }

使用关键字forward、redirect的方式来进行转发或重定向

        需要注意的是,如果用了 forward:则路径必须写成实际视图 url,不能写逻辑视图。 它相当于“request.getRequestDispatcher("url").forward(request,response)”。使用请求转发,既可以转发到 jsp,也可以转发到其他的控制器方法。

//请求转发,使用关键字的话视图解析器是无法为我们解析的
return "forward:/success.jsp";    //这里我的success.jsp是在根目录webapp下的
return "forward:/WEB-INF/pages/success.jsp";

//重定向,不用加项目名称,框架已经帮我们加好了
return "redirect:/success.jsp"; 

异步请求的方式来进行,使用@ResponseBody、@RequestBody

前端Ajax代码:

                $.ajax({
                    //编写JSON格式,设置属性和值
                    url : "userController/testAjax",
                    data : {"username":"yangshiwen","password":"123456","age":30},
                    type : "post",
                    success : function (data) {

                        //data是指服务器端响应的json数据,进行解析
                        alert(data);
                        alert(data.username);
                        alert(data.password);
                        alert(data.age);

                    }
                });

controller层代码:

    /**
     * 模拟异步请求刷新
     */
    @RequestMapping(value = "/testAjax")
    @ResponseBody
    //这里因为导入了jackSon,所以直接用user接收就行了
    public User testAjax(User user){
        System.out.println("testAjax测试方法执行了");

        //客户端发送ajax请求,传的是json字符串,后端把json字符串封装到user对象中去了
        System.out.println(user);

        //做响应,模拟查询数据库,这里重新更新了user
        user.setUsername("hahaha");
        user.setAge(50);
        user.setPassword("123456");

        //做出响应
        return user;
    }

↓↓↓↓↓↓↓↓↓↓或者这种使用@RequestBody的方式进行↓↓↓↓↓↓↓↓↓

               $.ajax({
                    //编写JSON格式,设置属性和值
                    url : "userController/testAjax",
                    contentType : "application/json;charset=UTF-8",
                    data : '{"username":"yangshiwen","password":"123456","age":30}',
                    dataType : "json",
                    type : "post",
                    success : function (data) {

                        //data是指服务器端响应的json数据,进行解析
                        alert(data);
                        alert(data.username);
                        alert(data.password);
                        alert(data.age);

                    }
                });
    /**
     * 模拟异步请求刷新
     */
    @RequestMapping(value = "/testAjax")
    @ResponseBody
    //这里因为导入了jackSon,所以直接用user接收就行了
    public User testAjax(@RequestBody User user){
        System.out.println("testAjax测试方法执行了");

        //客户端发送ajax请求,传的是json字符串,后端把json字符串封装到user对象中去了
        System.out.println(user);

        //做响应,模拟查询数据库,这里重新更新了user
        user.setUsername("hahaha");
        user.setAge(50);
        user.setPassword("123456");

        //做出响应
        return user;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值