zoukankan      html  css  js  c++  java
  • 2021-5-8 周报博客

    个人博客

    1.学到的东西

    SpringMVC的数据响应

    01-SpringMVC的数据响应-数据响应方式(理解)

    1. 页面跳转

    直接返回字符串

    通过ModelAndView对象返回

    2) 回写数据

    直接返回字符串

    返回对象或集合

    02-SpringMVC的数据响应-页面跳转-返回字符串形式(应用)

    03-SpringMVC的数据响应-页面跳转-返回ModelAndView形式1(应用)

    在Controller中方法返回ModelAndView对象,并且设置视图名称

    @RequestMapping(value="/quick2")
        public ModelAndView save2(){
            /*
                Model:模型 作用封装数据
                View:视图 作用展示数据
             */
            ModelAndView modelAndView = new ModelAndView();
            //设置模型数据
            modelAndView.addObject("username","itcast");
            //设置视图名称
            modelAndView.setViewName("success");
    
            return modelAndView;
        }
    

    04-SpringMVC的数据响应-页面跳转-返回ModelAndView形式2(应用)

    n在Controller中方法形参上直接声明ModelAndView,无需在方法中自己创建,在方法中直接使用该对象设置视图,同样可以跳转页面

     @RequestMapping(value="/quick3")
        public ModelAndView save3(ModelAndView modelAndView){
            modelAndView.addObject("username","itheima");
            modelAndView.setViewName("success");
            return modelAndView;
        }
    @RequestMapping(value="/quick4")
        public String save4(Model model){
            model.addAttribute("username","博学谷");
            return "success";
        }
    

    05-SpringMVC的数据响应-页面跳转-返回ModelAndView3(应用)

    在Controller方法的形参上可以直接使用原生的HttpServeltRequest对象,只需声明即可

    @RequestMapping(value="/quick5")
        public String save5(HttpServletRequest request){
            request.setAttribute("username","酷丁鱼");
            return "success";
        }
    

    06-SpringMVC的数据响应-回写数据-直接回写字符串(应用)

    通过SpringMVC框架注入的response对象,使用response.getWriter().print(“hello world”) 回写数据,此时不需要视图跳转,业务方法返回值为void

    将需要回写的字符串直接返回,但此时需要通过@ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳转是直接在http响应体中返回

    @RequestMapping(value="/quick7")
        @ResponseBody  //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
        public String save7() throws IOException {
            return "hello itheima";
        }
    
        @RequestMapping(value="/quick6")
        public void save6(HttpServletResponse response) throws IOException {
            response.getWriter().print("hello itcast");
        }
    

    07-SpringMVC的数据响应-回写数据-直接回写json格式字符串(应用)

    @RequestMapping(value="/quick8")
        @ResponseBody
        public String save8() throws IOException {
            return "{"username":"zhangsan","age":18}";
        }
    

    手动拼接json格式字符串的方式很麻烦,开发中往往要将复杂的java对象转换成json格式的字符串,我们可以使用web阶段学习过的json转换工具jackson进行转换,通过jackson转换json格式字符串,回写字符串

    @RequestMapping(value="/quick9")
        @ResponseBody
        public String save9() throws IOException {
            User user = new User();
            user.setUsername("lisi");
            user.setAge(30);
            //使用json的转换工具将对象转换成json格式字符串在返回
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(user);
    
            return json;
        }
    

    08-SpringMVC的数据响应-回写数据-返回对象或集合(应用)

    通过SpringMVC帮助我们对对象或集合进行json字符串的转换并回写,为处理器适配器配置消息转换参数,指定使用jackson进行对象或集合的转换,因此需要在spring-mvc.xml中进行如下配置:

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
                </list>
            </property>
        </bean>
    
    @RequestMapping(value="/quick10")
        @ResponseBody
        //期望SpringMVC自动将User转换成json格式的字符串
        public User save10() throws IOException {
            User user = new User();
            user.setUsername("lisi2");
            user.setAge(32);
            return user;
        }
    

    09-SpringMVC的数据响应-回写数据-返回对象或集合2(应用)

    在方法上添加@ResponseBody就可以返回json格式的字符串,但是这样配置比较麻烦,配置的代码比较多,因此,我们可以使用mvc的注解驱动代替上述配置

    <mvc:annotation-driven/>
    

    在 SpringMVC 的各个组件中,处理器映射器、处理器适配器、视图解析器称为 SpringMVC 的三大组件。

    使用<mvc:annotation-driven />自动加载 RequestMappingHandlerMapping(处理映射器)和

    RequestMappingHandlerAdapter( 处 理 适 配 器 ),可用在Spring-xml.xml配置文件中使用

    <mvc:annotation-driven />替代注解处理器和适配器的配置。

    同时使用<mvc:annotation-driven />

    默认底层就会集成jackson进行对象或集合的json格式字符串的转换

    10-SpringMVC的数据响应-知识要点小结(理解,记忆)

    1) 页面跳转

    直接返回字符串

    通过ModelAndView对象返回

    2) 回写数据

    直接返回字符串

    HttpServletResponse 对象直接写回数据,HttpServletRequest对象带回数据,Model对象带回数据或者@ResponseBody将字符串数据写回

    返回对象或集合

    @ResponseBody+<mvc:annotation-driven/>

    2.下周计划

    SpringMVC的数据请求

    3.遇到的问题

  • 相关阅读:
    Selenium库的使用
    Win10 常用快捷键
    503.下一个更大元素 II
    456.132模式
    201.数字范围按位与
    78.子集
    299.猜数字游戏
    49.字母异位词分组
    36.有效的数独
    290.单词规律
  • 原文地址:https://www.cnblogs.com/gongyunlong-blogs/p/14912164.html
Copyright © 2011-2022 走看看