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

访问同上

 

  

 

转载于:https://www.cnblogs.com/wy0119/p/7771617.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值