Spring Boot 之 HelloWorld詳解
阿新 • • 發佈:2018-01-29
www repos lookup dex lns jar nap put epo
SpringBoot介紹~<暫時假裝有>
配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nowcoder</groupId> <artifactId>wenda</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>wenda</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Controller層
簡單Hello輸出
@Controller
public class IndexController {
@RequestMapping(path = {"/","/index"})
@ResponseBody
public String index(){
return "Hello";
}
}
訪問:
http://127.0.0.1:8080/index
http://127.0.0.1:8080/
輸出:
Hello
參數解析輸出
路徑傳遞
@Controller public class IndexController { @RequestMapping(path = {"/","/index"}) @ResponseBody public String index(){ return "Hello"; } @RequestMapping(path = {"/profile/{userId}"}) @ResponseBody public String profile(@PathVariable("userId") int userId ){ return String.format("Profile Page of %d",userId); } }
訪問http://127.0.0.1:8080/profile/2
:
Profile Page of 2
參數解析輸出例2
@RequestMapping(path = {"/profile/{groupId}/{userId}"}) @ResponseBody public String profile(@PathVariable("userId") int userId , @PathVariable("groupId") String groupId ){ return String.format("Profile Page of %s / %d",groupId,userId); }
訪問http://127.0.0.1:8080/profile/admin/10086
Profile Page of admin / 10086
eg3請求參數傳遞
@RequestMapping(path = {"/profile/{groupId}/{userId}"})
@ResponseBody
public String profile(@PathVariable("userId") int userId ,
@PathVariable("groupId") String groupId ,
@RequestParam("type") int type ,
@RequestParam("key") String key ){
return String.format("Profile Page of %s / %d , t:%d k:%s",groupId,userId,type,key);
}
訪問http://127.0.0.1:8080/profile/admin/10086?type=2&key=w
Profile Page of admin / 10086 , t:2 k:w
模板Velocity使用
controller
@RequestMapping(path = {"/vm"},method = {RequestMethod.GET})
public String template(Model model){
model.addAttribute("value1","vvvv1");
return "home";
}
指向home.html 渲染
<html>
<body>
<pre>
#*
你看不到我~~~~
*#
$!{value1}
$!{value2} ## 如果不存在,強制為空
${value3}
</pre>
</body>
</html>
依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
application.properties 配置
spring.velocity.suffix=.html
訪問http://127.0.0.1:8080/vm
vvvv1
${value3}
總結
- @Controller 註釋來定義說這是一個網頁入口
- @RequestMapping 指定訪問地址or傳入參數
- 用@PathVariable和@RequestParam對傳入參數做解析
- 用@ResponseBody or 模板文件 返回結果
Spring Boot 之 HelloWorld詳解