Spring-Boot学习(2)-----Spring-Boot-web学习

来源:互联网 发布:黑客命令 Mac 编辑:程序博客网 时间:2024/06/03 20:16

为了更上技术的革新,近期打算学习下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=falsespring.freemarker.charset=UTF-8spring.freemarker.check-template-location=truespring.freemarker.content-type=text/htmlspring.freemarker.expose-request-attributes=truespring.freemarker.expose-session-attributes=truespring.freemarker.request-context-attribute=requestspring.freemarker.suffix=.ftl

3.java代码

新建一个UserController

@Controllerpublic class UserController {    @RequestMapping("/ftl")    public String testFreeMaker() {        return "index";    }}

新建一个Application.class

@SpringBootApplicationpublic 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一样。

0 0
原创粉丝点击