spring boot + Thymeleaf开发web项目

“Spring boot非常适合Web应用程序开发。您可以轻松创建自包含的HTTP应用。web服务器采用嵌入式Tomcat,或者Jetty等。大多数情况下Web应用程序将使用

spring-bootstarter-web模块快速启动和运行。”

本例子通过显示用户列表展示如何使用spring boot和Thymeleaf开发web项目。

 

几点说明:

  •  Spring boot开发web项目,通常打成jar包,使用内置的web服务器 Tomcat、Jetty、undertow 来运行。
  •  静态资源(css、js、图片等)默认放在resources/static下面。如果要修改默认存放目录,可以通过设置属性 spring.mvc.static-path-pattern来实现。
  • 模板文件默认放在 templates目录下
  •  Spring boot支持使用模板来开发web应用,支持的模板类型包括
    • FreeMarker
    • Groovy
    • Thymeleaf
    • Mustache

Spring boot不建议使用jsp开发web。

本文使用Thymeleaf来作为模板引擎开发web项目。

Thymeleaf

Thymeleaf是一个Java模板引擎开发库,可以处理和生成HTML、XML、JavaScript、CSS和文本,在Web和非Web环境下都可以正常工作。

Thymeleaf可以跟Spring boot很好的集成。

 

Spring Boot+Thymeleaf开发web

创建spring boot项目

 

选择spring boot和依赖 ,注意需要的依赖包括web和Thymeleaf

 

 点击finish。创建的项目结构如下:

其中SpringBootWebApplication.java是自动生成的。是程序启动入口。

 

生成的POM.xml文件如下

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yuny</groupId>
    <artifactId>myweb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Spring-boot-web</name>
    <description>web project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

复制代码

 

增加实体User

复制代码

public class User {
    private Integer id;
    private String name;
    private String age;
    private String address;
    //省略get和set方法、构造函数
}

复制代码

 

增加UserController

按 Ctrl+C 复制代码

 

按 Ctrl+C 复制代码

 

增加模版文件list.html,注意模版文件是放在tempplates目录下。本案例将文件放在/templates/user/下面。

按 Ctrl+C 复制代码

 

按 Ctrl+C 复制代码

 

启动

以application方式启动SpringBootWebApplication.java

 

 访问http://localhost:8080/user/list ,效果如下

总结

Spring boot开发web项目非常简单,对模版的支持也很到位。Thymeleaf模版引擎跟el表达式很相似,所以从jsp过度到使用Thymeleaf 并不是太难的事。

原文地址:https://www.cnblogs.com/junyang/p/8159383.html

Demo源码下载地址:https://download.csdn.net/download/zhizhuodewo6/10671505

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用Spring BootThymeleaf实现拦截器的步骤: 1.创建一个拦截器类,该类需要实现HandlerInterceptor接口。在该类中,您可以实现三个方法:preHandle,postHandle和afterCompletion。这些方法分别在处理程序执行之前,之后和完成之后调用。 ```java import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; @Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 在请求处理之前进行调用(Controller方法调用之前) System.out.println("请求处理之前调用……"); return true; // 如果返回false,则请求中断 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后) System.out.println("请求处理之后调用……"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 在视图被渲染之后调用(主要用于资源清理工作) System.out.println("视图渲染之后调用……"); } } ``` 2.将拦截器注册到应用程序中。在Spring Boot应用程序中,您可以通过使用WebMvcConfigurerAdapter类来注册拦截器。在该类中,您可以重写addInterceptors方法,并将您的拦截器添加到拦截器链中。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebAppConfiguration extends WebMvcConfigurerAdapter { @Autowired private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor); } } ``` 3.使用Thymeleaf进行视图渲染。在您的控制器方法中,您可以返回一个包含视图名称和模型对象的ModelAndView对象。在视图中,您可以使用Thymeleaf模板来渲染HTML内容。在下面的示例中,我们将使用layout.html作为页面布局,并使用th:replace指令将内容注入到该布局中。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>My Page</title> </head> <body> <div th:replace="layout :: content"> <h1 th:text="${message}">Hello, World!</h1> </div> </body> </html> ``` 4.创建一个包含布局的Thymeleaf模板。在该模板中,您可以定义页面的基本结构和样式,并使用th:fragment指令定义页面的各个部分。在下面的示例中,我们将使用layout.html作为页面布局,并使用th:fragment指令定义页面的内容部分。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title th:text="${pageTitle}">My Site</title> </head> <body> <div th:fragment="content"></div> </body> </html> ``` 以上就是使用Spring BootThymeleaf实现拦截器的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值