设置Maven
设置Maven相关的参数
创建项目
创建maven-spring项目
在pom中添加Maven依赖
在pom文件中添加相关的依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>maven-spring-demo</groupId> 8 <artifactId>maven-spring-demo</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <name>hi-spring</name> 12 <packaging>jar</packaging> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.5.8.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <dependencies> 22 <dependency> 23 <groupId>junit</groupId> 24 <artifactId>junit</artifactId> 25 <version>3.8.1</version> 26 <scope>test</scope> 27 </dependency> 28 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-devtools</artifactId> 37 <optional>true</optional> 38 </dependency> 39 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-thymeleaf</artifactId> 43 </dependency> 44 </dependencies> 45 46 47 <build> 48 <plugins> 49 <plugin> 50 <!-- This plugin will set the properties values using dependency information --> 51 <groupId>org.apache.maven.plugins</groupId> 52 <artifactId>maven-dependency-plugin</artifactId> 53 <version>2.3</version> 54 <executions> 55 <execution> 56 <goals> 57 <goal>properties</goal> 58 </goals> 59 </execution> 60 </executions> 61 </plugin> 62 <plugin> 63 <artifactId>maven-compiler-plugin</artifactId> 64 <configuration> 65 <source>1.8</source> 66 <target>1.8</target> 67 </configuration> 68 </plugin> 69 <plugin> 70 <groupId>org.springframework.boot</groupId> 71 <artifactId>spring-boot-maven-plugin</artifactId> 72 </plugin> 73 </plugins> 74 </build> 75 76 </project>
下载Maven依赖
下载相关的依赖
创建相应的类文件
在DemoRun中添加如下代码:
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 4 @SpringBootApplication 5 public class DemoRun { 6 public static void main(String[] args) { 7 SpringApplication.run(DemoRun.class, args); 8 } 9 }
在DemoController中添加如下代码:
1 import org.springframework.stereotype.Controller; 2 import org.springframework.ui.Model; 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RequestParam; 5 6 @Controller 7 public class DemoController { 8 @RequestMapping("/greeting") 9 public String greeting( 10 @RequestParam(value = "name", required = false, defaultValue = "name") String name, 11 @RequestParam(value = "age", required = false, defaultValue = "age") String age, 12 @RequestParam(value = "sex", required = false, defaultValue = "男") String sex, 13 Model model) { 14 model.addAttribute("name", name); 15 model.addAttribute("age", age); 16 model.addAttribute("sex", sex); 17 return "greeting"; 18 } 19 }