首先建立一个controller
类html
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class GreetingController { @RequestMapping("/greeting") public String greeting(@RequestParam(value="name", defaultValue="world")String name, Model model){ model.addAttribute("name", name); return "index"; } }
@Controller
注解标注这个类是一个控制类;java
@RequestMapping("/greeting")
注解映射/greeting
访问到此方法;web
@RequestParam(value="name", defaultValue="world")
注解将请求中的name
参数传递给变量,若无取默认值;spring
model
对象添加的属性能够从前台取到;浏览器
return
返回的值即为此controller
绑定的页面名。app
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Insert title here</title> </head> <body> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html>
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) throws Exception{ SpringApplication.run(Application.class, args); } }
而后浏览器访问: http://localhost:8080/greeting?name=Johnsvg