风格熟悉的控制类
[TestController.java]
package com.demo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping(value = "/print") public String print(){ return "Hello Controller!"; } }
浏览器访问无误 http://localhost:8080/print
--------------------------------------------------------------------
GET-URL带参数方式1
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping(value = "/{name}/print/" , method = RequestMethod.GET) public String print(@PathVariable("name") String name){ return "WelCome:"+name; } }
浏览器测试:http://localhost:8080/s/print/
POST-带参方式
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping(value = "/print/{name}" , method = RequestMethod.POST) public String print(@PathVariable("name") String name){ return "WelCome:"+name; } }
浏览器测试:http://localhost:8080/print/s
其他方式获取Request Scope Parameter
import org.springframework.web.bind.annotation.RequestParam; @RestController public class TestController { @RequestMapping(value = "/print" , method = RequestMethod.GET) public String print(@RequestParam("name") String name){ return "WelCome:"+name; } }
测试:http://localhost:8080/print?name=123
重定向跳转页面方式
resources/目录下
创建/templates/
放入需要跳转的页面 hello.html
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ResponseBody; @Controller @ResponseBody public class TestController { @RequestMapping(value = "/print" , method = RequestMethod.GET) public String print(){ return "hello"; } }
测试即可跳转
不同路由访问同一返回
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ResponseBody; @Controller @ResponseBody public class TestController { @RequestMapping(value = {"/print","/hello"} , method = RequestMethod.GET) public String print(){ return "hello"; } }