为了更上技术的革新,近期打算学习下spring-Boot跟Spring-Cloud。下面将记录下自己学习过程。Spring-boot系列学习源代码放在https://github.com/wenbo2018/spring-boot-learning,有需要的可以自行下载。
使用Spring-boot进行web开发是极其方便而且简单的,下面将展示使用Spring-boot进行web开发,本文以ftl作为模板技术进行展示。
1.maven依赖
只需要配置父模块spring-boot-starter-parent跟spring-boot-starter-web就可以发布一个简单的应用了。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<!-- Spring Boot web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Freemarker 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.配置文件
虽然Spring-boot极大简化了配置过程,但是还是需要一些简单的配置,spring-boot主要采用
application.properties和application.yaml配置文件进行配置,本文首先使用application.properties进行配置。application.properties放置于resources文件夹下面。
本文主要在application.properties配置ftl相关配置
#Freemarker 配置
spring.freemarker.template-loader-path=classpath:/web/
## 文件配置路径用于放置ftl文件文件夹,web文件夹在sources下
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
3.java代码
新建一个UserController
@Controller
public class UserController {
@RequestMapping("/ftl")
public String testFreeMaker() {
return "index";
}
}
新建一个Application.class
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(Application.class);
app.run(args);
}
}
ftl文件在sources/web/index.ftl下。运行main函数,只需要在浏览器中输入http://localhost:8080/ftl就能看到返回页面,至于传值跟Spring MVC一样。