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;
        }

    访问同上

      

  • 相关阅读:
    统计学(第六版)14单元——学习总结
    统计学(第六版)13单元——学习总结(时间序列分析总结)
    统计学(第六版)11到12单元——学习总结
    Kubernetes: 微内核的分布式操作系统
    彻底搞懂JavaScript之原型
    手把手带你玩转k8s-一键部署vue项目
    新一代缓存Caffeine,速度确实比Guava的Cache快
    理解 Es6 中的 Symbol 类型
    一天一大 leet(用两个栈实现队列)难度:简单 DAY-30
    (Java 源码阅读) 春眠不觉晓,HashMap知多少
  • 原文地址:https://www.cnblogs.com/wy0119/p/7771617.html
Copyright © 2011-2022 走看看