SpringBoot的web开发

简介

Web开发的核心内容主要包括内嵌的Servlet容器和SpringMVC。

SpringBoot使用起来非常简洁,大部分配置都有SpringBoot自动装配。

SpringBoot的web支持

SpringBoot提供了spring-boot-starter-web为web开发予以支持,而这个启动器内嵌了Tomcat以及SpringMVC依赖。而web相关的自动配置存储在spring-boot-autoconfigure.jar的org.springframework.boot.autoconfigure.web包下。
其中如图:
1

从这些文件名就可以看出:

  • ServletWebServerFactoryAutoConfiguration和ServerProperties自动配置内嵌的Servlet容器
  • HttpEncodingAutoConfiguration和ServerProperties自动配置http编码
  • MultipartAutoConfiguration和MultipartProperties自动配置上传文件属性
  • WebMvcAutoConfiguration和WebMvcProperties配置Spring MVC

Thymeleaf,模板引擎

SpringBoot中推荐使用Thymeleaf,是因为Thymeleaf提供了完美的SpringMVC支持。

基础

  • Thymeleaf是一个Java类库,让是一个xml/xhtml/html5的模板引擎,可以最为MVC的View层。

  • Thymeleaf还提供了额外的模块与SpringMVC集成。

  • Thymeleaf 官网

  • Thymeleaf 在Github 的主页

  • Thymeleaf的自动配置规则:ThymeleafProperties

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    private Charset encoding;
}

可以看到其中默认的前缀和后缀,所以只需要把html页面放在templates下,thymeleaf就会自动渲染。

模板引擎的作用

做好一个前端页面模板,其中包含一些动态的值,我们写一些表达式代替。而这些值,是从后台获取的已经封装好的数据。然后把这个页面模板和数据交给模板引擎,模板引擎按照数据帮我们解析表达式、并把结果填充到指定的位置,然后把这个数据最终生成一个想要的内容输出到前端页面,这就是我们这个模板引擎,不管是jsp还是其他模板引擎,都是这个思想。只不过,不同模板引擎之间,语法不同。

Thymeleaf模板引擎,是一个高级语言的模板引擎,语法更简单,功能更强大。

使用Thymeleaf模板引擎

引入Thymeleaf并在html页面添加命名空间约束
<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
 xmlns:th="http://www.thymeleaf.org"
Thymeleaf语法

参考Thymeleaf 官网

  1. 我们可以使用任意的th:xxx来替代html中原生属性的值

3

  1. 常见的表达式语法
Simple expressions:(表达式语法)
  Variable Expressions: ${...}:获取变量值
  Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
  Message Expressions: #{...}:获取国际化内容
  Link URL Expressions: @{...}:定义URL;
  Fragment Expressions: ~{...}:片段引用表达式

Literals(字面量)
      Text literals: 'one text' , 'Another one!' ,…
      Number literals: 0 , 34 , 3.0 , 12.3 ,…
      Boolean literals: true , false
      Null literal: null
      Literal tokens: one , sometext , main ,…
      
Text operations:(文本操作)
    String concatenation: +
    Literal substitutions: |The name is ${name}|
    
Arithmetic operations:(数学运算)
    Binary operators: + , - , * , / , %
    Minus sign (unary operator): -
    
Boolean operations:(布尔运算)
    Binary operators: and , or
    Boolean negation (unary operator): ! , not
    
Comparisons and equality:(比较运算)
    Comparators: > , < , >= , <= ( gt , lt , ge , le )
    Equality operators: == , != ( eq , ne )
    
Conditional operators:条件运算(三元运算符)
    If-then: (if) ? (then)
    If-then-else: (if) ? (then) : (else)
    Default: (value) ?: (defaultvalue)
    
Special tokens:
    No-Operation: _

使用SpringBoot的步骤

  1. 创建一个SpringBoot项目,选择需要的模块,SpringBoot默认将选择的模块自动配置。
  2. 手动配置文件中的配置部分。
  3. 更加专注编写业务代码,不需要考虑之前的一大堆配置。

静态资源的自动配置

在自动配置类WebMvcAutoConfigurationAdapter的addResourceHandlers方法中 定义了以下静态资源的自动配置。

1. 类文件路径

把类路径向下的/META-INF/resources,/resources,/static和/public文件夹下的静态文件直接映射为/**,可以通过http://localhost:8080/**来访问

2. webjar

何谓webjar,webjar就是将我们常用的脚本框架封装在jar包中的jar包

webjar官网

把webjar的/META-INF/resources/webjars/下的静态资源文件映射成/webjar/**,可以通过http://localhost:8080/webjar/**来访问

"classpath:/META-INF/resources/"   localhost:8080/webjars/
"classpath:/resources/"  //优先级最高   localhost:8080/
"classpath:/static/"   //优先级其次,默认     localhost:8080/
"classpath:/public/"    //优先级最低    localhost:8080/

我们可以在resources根目录下新建对应的文件夹,都可以存放我们的静态文件;
比如我们访问 http://localhost:8080/1.js , 他就会去这些文件夹中寻找对应的静态资源文件;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是一个快速构建基于 Spring 框架的应用程序的工具。它为 Spring 应用程序开发提供了一种简单的方法,无需繁琐地配置 XML,只需要使用注解即可实现常见的业务逻辑。 下面是一个基本的 Spring Boot Web 应用程序的步骤: 1. 创建一个 Maven 项目,并添加 Spring Boot 的依赖。 2. 创建一个 Controller 类,并添加处理请求的方法。 3. 配置应用程序的入口点,并启动应用程序。 以下是一个简单的示例: 1. 创建 Maven 项目 使用 Maven 创建一个新的项目,可以参考以下命令: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=webapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 2. 添加 Spring Boot 依赖 在 pom.xml 文件中添加 Spring Boot Starter Web 依赖: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 3. 创建 Controller 类 创建一个 HomeController 类,并添加处理请求的方法: ``` @RestController public class HomeController { @GetMapping("/") public String home() { return "Hello, World!"; } } ``` 4. 配置应用程序的入口点 创建一个 SpringBootWebApplication 类,并使用 @SpringBootApplication 注解标记为应用程序的入口点: ``` @SpringBootApplication public class SpringBootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringBootWebApplication.class, args); } } ``` 5. 启动应用程序 使用以下命令启动应用程序: ``` mvn spring-boot:run ``` 在浏览器中访问 http://localhost:8080/ ,即可看到 "Hello, World!"。 这就是一个简单的 Spring Boot Web 应用程序的开发过程。当然,除了以上步骤,还有很多其他的配置和实现方式,具体可以参考官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值