-
目的:
搭建一个spring boot框架的项目环境 能进行网页访问
-
使用工具:
eclipse + maven + tomcat7.0.67
-
程序结构
-
使用maven的pom.xml导jar包:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.3.2.RELEASE</version>
<!-- 1.修改pom,排除内嵌tomcat8插件 -->
1. <exclusions>
<exclusion> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
2. <dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>7.0.67</version>
</dependency>
</dependencies>
- 写一个@SpringBootApplication // 是用来初始化配置文件 在以后可以自行配置
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationSpring {
public static void main(String[] args) {
// 启动配置 自动扫描注解
SpringApplication.run(ApplicationSpring.class, args);
}
}
- 写一个带注解HelloSpring类 映射路径
package cn.springboot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloSpring {
@RequestMapping("/hello")
public String index(ModelMap map) {
map.addAttribute("host", "http://blog.csdn.net/qq_33399709");
return "index";
}
}
- 编写index.html模版文件
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<!-- 如果host中有值 就显示host中存储的值,否则显示hello world -->
<h1 th:text="${host}">Hello World</h1>
</body>
</html>
- 启动 ApplicationSpring 类
使用http://localhost:8080/hello 就可以成功访问 (这里不需要添加项目名)
- 总结问题
-
spring boot 内嵌Tomcat8 如果Tomcat不匹配 就需要将内嵌的Tomcat8给注释掉
使用在pom.xml 引入jar -
8080端口被占用的情况
1). 使用cmd命令 输入 netstat -aon|findstr “8080” 查看出PID号 然后打开任务管理器 -->服务 找到对应的PID将任务关闭