zoukankan      html  css  js  c++  java
  • SpringMVC注解之Object返回值

    一.springMVC的注解使用

        springMVC作为一个请求驱动类型的轻量级web框架,在该框架中支持使用注解来配置控制器.

      1.1maven工程引入jar包.

      

      

    <!--httpManagerConverter转换器-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.6.3</version>
            </dependency>

      1.2新建一个普通类.

    @Controller  //表示该类是一个控制器
    public class FirstContreller {
    
        @RequestMapping(value = "/hello001")//将前台请求映射到该方法上
        public String first01(@PathVariable("color") String color0){
            System.out.println(color0);
            return "/index.jsp";
        }
    }

      1.3在xml文件中打开包扫描器

      

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    
      <!--注解:包扫描器  -->
      <context:component-scan base-package="cn.wy.day04returnObject"></context:component-scan>
    
    
    
      <!--配置驱动-->
        <mvc:annotation-driven/>
      <!--解决静态资源不可用-->
      <mvc:default-servlet-handler></mvc:default-servlet-handler>
    
    
    </beans>

    一.int类型做返回值

      

     /*返回值是int*/
        @RequestMapping("/first001")
        @ResponseBody
        public Object first(){
    
            return 1;
        }

    启动后在浏览器中直接访问first001

    二.String类型做返回值

    /*返回值是string*/
        @RequestMapping("/first01")
        @ResponseBody
        public Object first01(){
    
            return "12313123";
        }

    访问同上

    三.对象做返回值

    /*返回值是对象*/
        @RequestMapping("/first02")
        @ResponseBody
        public Object first02(){
            Car car1=new Car();
            car1.setAge(1000);
            car1.setName("呵呵");
            return car1;
        }

    访问同上

    四.list集合做返回值

        /*返回值是list*/
        @RequestMapping("/first03")
        @ResponseBody
        public Object first03(){
            List<Car> list=new ArrayList<Car>();
            Car car1=new Car();
            car1.setAge(1000);
            car1.setName("呵呵");
            Car car2=new Car();
            car2.setAge(1000);
            car2.setName("屁屁");
    
            list.add(car1);
            list.add(car2);
            return list;
        }

    访问同上

      

  • 相关阅读:
    挂载硬盘,提示 mount: unknown filesystem type 'LVM2_member'的解决方案
    mongo3.4 配置文件 注意事项
    Rsync 传输不需要输入密码
    Robomongo 0.9.0 连接mongo数据库时,提示连接失败 的解决方案
    linux 安装 mongo
    mysql GTID主从复制(主库在线,添加新丛库)
    计算机网络原理精讲第四章--网络层
    Chrome浏览器商店安装的插件保存到本地
    计算机网络原理精讲第三章--链路层
    计算机网络原理精讲第二章--物理层
  • 原文地址:https://www.cnblogs.com/wy0119/p/7771617.html
Copyright © 2011-2022 走看看