• SpinrMVC方法返回值(二)


    方法返回Object类型

    返回Object类型需要在xml配置文件中声明一个注解驱动

        <context:component-scan base-package="day09"></context:component-scan>
    
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    //注解驱动 <mvc:annotation-driven></mvc:annotation-driven>

    下边是Object作为返回值的一些方法代码:

    返回数值型

        /*
        * 返回数值型
        * */
        @RequestMapping("/first")
        @ResponseBody
        public Object doFirst(){
            return 1;
        }

    返回字符串

        //返回字符串  解决乱码
        @RequestMapping(value = "/second",produces = "text/html;charset=utf-8")
        @ResponseBody
        public Object doSecond(){
            return "我是返回字符串";
        }

    返回对象

        //返回对象
        @RequestMapping("/third")
        @ResponseBody
        public Object doThird(){
            Student stu=new Student();
            stu.setUname("明天会更好");
            stu.setUpwd("没有密码");
            return  stu;
        }

    返回集合

        //返回List集合
        @RequestMapping("/four")
        @ResponseBody
        public Object doFour(){
            List<Student> list=new ArrayList<Student>();
            Student stu=new Student();
            stu.setUname("明天会更好");
            stu.setUpwd("没有密码");
            Student stu1=new Student();
            stu1.setUname("十万个为什么");
            stu1.setUpwd("没有密码");
            list.add(stu);
            list.add(stu1);
            return list;
        }
  • 相关阅读:
    一、逻辑架构与存储引擎
    三、动态SQL
    九、装饰者模式
    二、Mapper映射文件
    八、适配器模式
    测试开发系列之Python开发mock接口(二)
    测试开发系列之Python开发mock接口(三)
    html基础
    seleniumWebdriver浏览器驱动信息汇总
    用30行代码开发一个上传、下载文件的接口
  • 原文地址:https://www.cnblogs.com/1234AAA/p/8669331.html
Copyright © 2020-2023  润新知