地址傳參
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 ;
}
}