SpringBoot

本文介绍如何使用SpringBoot快速构建项目并部署,包括添加依赖、配置视图引擎如JSP、Freemarker及JSON支持的方法。通过简单的示例代码,展示了SpringBoot在Web开发中的便捷性。

SpringBoot Spring里面所提供的一个框架,或者一些maven的集合,(提供很多starter

我们通过它,可以快速的构建项目 编译和部署,监控;

 

我们正常跑一个springweb程序值需要添加

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

@SpringBootApplication
public class APP {
public static void main(String[] args) {
SpringApplication.run(APP.class);
}
}
@RestController
public class AppController {
@RequestMapping("/hello")
public String index(){
return "dada";
}
}


网页显示dada
springboot跳转jsp需要引入springboot对jsp的支持

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

 

<!-- servlet 依赖. -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<scope>provided</scope>

</dependency>

 

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

</dependency>

 

<!-- tomcat 的支持. -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

<scope>provided</scope>

</dependency>

需要在resources配置application.properties

剩下的写法和上面一样

jsp的路径就是spring.mvc.view.prefix=/WEB-INF/add/这个

写完后运行会404这里就需要在idea里面配置一下

 

 

 对于freemaker支持

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>

需要配置application.properties下面的内容
# FreeeMarker 模板引擎配置
# 设定ftl文件路径
spring.freemarker.tempalte-loader-path=classpath:/templates
# 关闭缓存,及时刷新,上线生产环境需要修改为true
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
对于json的支持
@SpringBootApplication
public class FreemarkerAPP {
public static void main(String[] args) {
SpringApplication.run(FreemarkerAPP.class);
}
}

@Controller
public class FreemarkerController {
@RequestMapping("/index")
public String index(Model model){
System.out.println("==========================");
model.addAttribute("name","来了老弟");
return "index";

}

}
在resources中写一个xxxx.ftl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello , ${name}</h1>
</body>
</html>
 

 

转载于:https://www.cnblogs.com/yinshunxin/p/10786160.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值