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

为了更上技术的革新,近期打算学习下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一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值