zoukankan      html  css  js  c++  java
  • SpringBoot传参方式

    地址传参

    1.创建一个Action类

    package com.lion.action;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    /**2019年08月05日 15时28分02秒 **
     * 目的:进行地址重写传参
     * 运行结果:springboot启动后在浏览器访问: http://localhost:8080/?msg=hello
     * 得到:【ECHO】hello
     * 总结:springboot极大的减少了原有的SpringMVC的配置量
     * */
    @Controller
    public class MessageAction {
        @ResponseBody
        @RequestMapping("/")
        public String echo(String msg){
            return "【ECHO】" + msg ;
        }
    }

    2.在浏览器访问: http://localhost:8080/?msg=hello    加上参数

    Rest风格参数传递

    package com.lion.action;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    /**2019年08月05日 15时28分02秒 **
     * 目的:Rest传参
     * 运行结果:springboot启动后在浏览器访问: http://localhost:8080/hello
     * 得到:【ECHO】hello
     * 总结: @PathVariable 将 @RequestMapping 中value带的变量 此处是 {id}与方法中参数绑定
     * 注意:如果变量名称与方法参数名称不一致,则需要指定
     * REST缺点 : 跳转的时候浏览器不认post/get 之外的访问方法
     * 优点:可以限制接收提交的方式,有利于规范代码。
     * */
    @Controller
    public class MessageAction {
        @ResponseBody
        @RequestMapping("/{message}")
        public String echo(@PathVariable("message")String msg){
            return "【ECHO】" + msg ;
        }
    }
  • 相关阅读:
    destoon代码从头到尾捋一遍
    php中foreach()函数与Array数组经典案例讲解
    刷题比赛
    #Math
    福慧双修(both)
    NOIP17提高模拟训练18 长途旅行(travel)
    NOIP提高组模拟训练18 正确答案(answer)
    NOIP17提高组模拟赛17 -乐曲创作(music)
    #2017 Multi-University Training Contest
    CodeForces
  • 原文地址:https://www.cnblogs.com/fcitx/p/11303956.html
Copyright © 2011-2022 走看看