Spring 傳參

spring傳參之@RequestParam註解

@RequestParam註解有三個參數分別是: value、 required、 defaultValue

  1. 代碼:

    @RequestMapping(value="test1", method = RequestMethod.GET)

    public String reqTest1(@RequestParam("name") String name){

     return name;
    

    }

    通過@RequestParam註解生命接收用戶傳入的參數,這樣當我們在瀏覽器輸入http://localhost:8080/test1?name=123

  2. 不設置參數的key

    例如:http://localhost/p/8324234

    代碼如下:

    @RequestMapping(value="/test2/{id}", method = RequestMethod.GET)

    public Integer reqTest2(@PathVariable("id") Integer id){

     return id;
    

    }

靜態資源映射

resources資源springboot默認只映射static、 template兩個文件夾下的文件

如果我們要在resources下新建一個image資源,想要讓spring boot找到它,只需在項目主類中配置一下就好  


    @SpringBootApplication
    public class MyApplication extends WebMvcConfigurerAdapter{

        public static void main(String[] args){

            SpringApplication.run(MyApplication.class,args);

        }

        @Override

        public void addResourceHandlers(ResourceHandlerRegistry registry){

            super.addResourceHandlers(registry);

            //這種方式會在默認的基礎上增加,不會影響默認的方式

            registry.addResourceHandler("/image/**").addResourceLocations("classpath:/image/");

        }

    }

參考資料 http://www.mamicode.com/info-detail-2154485.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章