Spring Boot总结

先从注解开始了解,SpringBoot提供了很多注解,可以帮助我们快速构建应用程序。以下是SpringBoot最常用的

@Autowired

作用:用于自动装配Spring容器中的Bean。

@Component

作用:用于标识一个类是Spring容器中的组件。@Component是Spring框架中的一个通用注解,用于标注一个类作为Spring Bean。

@Service

作用:用于标识一个类是Spring容器中的服务组件。@Service是Spring框架中的一个注解,用于标注一个类作为服务类(Service)

@Repository

作用:用于标识一个类是Spring容器中的数据访问组件。@Repository是Spring框架中的一个注解,用于标注一个类作为数据访问对象(DAO)。

@Configuration

作用:用于标识一个类是Spring的配置类。@Configuration是Spring框架中的一个注解,用于标注一个类作为配置类。

@Value

作用:用于获取配置文件中的属性值。@Value是Spring框架中的一个注解,用于将配置文件中的属性值注入到Bean对象中。

@SpringBootApplication
作用:这是一个组合注解,包括了@Configuration、@EnableAutoConfiguration和@ComponentScan三个注解。用于标识SpringBoot应用程序的入口类。

@Configuration:指示这个类是一个配置类,它定义了一个或多个@Bean方法,用于创建和配置Spring应用程序上下文中的Bean。

@EnableAutoConfiguration:启用Spring Boot的自动配置机制,它会自动添加所需的依赖项和配置,以使应用程序能够运行。

@ComponentScan:指示Spring Boot扫描当前包及其子包中的所有@Component、@Service、@Repository和@Controller注解的类,并将它们注册为Spring Bean。

@SpringBootApplication注解通常被用于Spring Boot应用程序的入口类上,用于启动Spring Boot应用程序。它可以简化Spring应用程序的配置和启动过程。

@RestController
作用:与@Controller类似,但是@RestController会自动将返回值转换为JSON格式。

@RestController是Spring Framework 4.0版本引入的一个注解,它是@Controller和@ResponseBody的组合注解。它用于标注一个类,表示这个类是一个RESTful风格的控制器,可以处理HTTP请求并返回JSON/XML格式的响应。

@RestController注解用于替代原来的@Controller注解,它默认情况下会将控制器方法的返回值转换为JSON格式,并以HTTP响应的方式返回给客户端。如果需要返回XML格式的响应,可以使用其他注解,如@Produces和@Consumes。

@RequestMapping
作用:用于映射请求URL和处理方法。@RequestMapping是Spring MVC框架中的一个核心注解,它用于映射HTTP请求和控制器方法之间的关系。它可以用于类级别和方法级别,用于指定请求URL和HTTP方法(GET、POST、PUT、DELETE等)。

前端模板引擎Thymeleaf的整合和使用

一、添加依赖
1.1首先,在项目的构建文件中(比如 Maven 或 Gradle)添加 Thymeleaf 的依赖。例如,对于 Maven 项目,在 pom.xml 文件中添加以下依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.2保存并更新项目依赖
这样就完成了Thymeleaf的引入。在你的代码中,你可以使用Thymeleaf提供的标签和表达式来处理模板中的动态内容,并将其渲染为最终的HTML页面。

二、配置Thymeleaf
2.1模板位置配置
可以通过配置 spring.thymeleaf.prefix 和 spring.thymeleaf.suffix 来指定模板文件的位置和后缀

spring.thymeleaf.prefix=/WEB-INF/templates/
spring.thymeleaf.suffix=.html
上述配置将会使 Thymeleaf 在 /WEB-INF/templates/ 目录下查找以 .html 结尾的模板文件。

2.2模板缓存配置
Thymeleaf 默认开启了模板缓存,以提高性能。在开发阶段可能需要关闭缓存以方便调试,可以通过配置 spring.thymeleaf.cache 进行设置。

spring.thymeleaf.cache=false
上述配置将会使Thymeleaf的模板缓存

2.3自定义标签配置
Thymeleaf 支持自定义标签,可以在配置中注册自定义标签处理器。

@Configuration
public class ThymeleafConfig implements ITemplateResolver {
    
    // ...其他配置
    
    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        // 配置模板位置、缓存等
        // ...
        return templateResolver;
    }
    
    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        // 注册自定义标签处理器
        // ...
        return templateEngine;
    }
 
    @Bean
    public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        // 其他配置
        // ...
        return viewResolver;
    }
 
    // ...其他配置
}

上述代码中,通过 templateResolver() 方法配置模板解析器,通过 templateEngine() 方法配置模板引擎,最后通过 viewResolver() 方法配置视图解析器。在 templateResolver() 和 templateEngine() 方法中可以设置一些自定义的属性,如模板位置、缓存等。在 templateEngine() 方法中还可以注册自定义的标签处理器

三、创建模板文件
3.1创建一个HTML文件
将其命名为模板名称,如“template.html"。

3.2在HTML文件中引入Thymeleaf命名空间
<h1 th:text="${title}">Page Title</h1>
<p th:text="${content}">Page Content</p>
3.3在HTML文件中使用Thymeleaf语法来定义模板内容
<h1 th:text="${title}">Page Title</h1>
<p th:text="${content}">Page Content</p>
3.4在Java代码中加载模板,并将数据传递给模板
// 加载模板文件
Template template = thymeleafTemplateEngine.getTemplate("template.html");
 
// 创建一个上下文对象,用于传递数据给模板
Context context = new Context();
context.setVariable("title", "My Page Title");
context.setVariable("content", "Hello, world!");
 
// 渲染模板并生成HTML代码
String renderedHtml = templateEngine.process(template, context);
在上面的例子中,我们使用Thymeleaf模板引擎加载模板文件"template.html",然后创建一个上下文对象并通过它将数据传递给模板。最后,我们调用process()方法来渲染模板,并生成HTML代码。

3.5将生成的HTML代码响应给客户端
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(renderedHtml);
四、控制器中使用Thymeleaf
4.1在Spring Boot中,在pom.xml文件中添加以下依赖项
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
4.2在Spring MVC控制器类中,使用@Controller注解标记该类,并使用@RequestMapping注解定义处理请求的方法。
@Controller
public class MyController {
    @RequestMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("message", "Hello, Thymeleaf!");
        return "hello";
    }
}
4.3创建一个包含Thymeleaf模板的HTML文件,并将其放置在/resources/templates目录下。
例如,创建一个名为"hello.html"的文件。在模板中使用Thymeleaf的语法来渲染数据。例如,使用${message}获取在控制器中添加到Model的属性。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello Thymeleaf</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html>

4.4运行
运行应用程序,并访问http://localhost:8080/hello,即可看到渲染后的页面。

五、在模板中使用Thymeleaf语法
5.1输出变量值
使用${}表达式来输出变量的值。

<p th:text="${message}"></p>
5.2判断条件
使用th:if和th:else来实现条件判断。

<p th:if="${user.isAdmin}">管理员</p>
<p th:unless="${user.isAdmin}">普通用户</p>
5.3循环迭代
使用th:each来遍历集合,使用th:object来指定迭代对象的别名。

<ul>
    <li th:each="item : ${items}" th:object="${item}">
        <p th:text="${name}"></p>
        <p th:text="${price}"></p>
    </li>
</ul>
5.4设置属性
使用th:attr来设置HTML元素的属性,如href、src等。

<a th:href="@{/product/{id}(id=${productId})}" th:attr="title=${productName}">
    <img th:src="@{${imageUrl}}" />
</a>
5.5表单处理
使用th:field和th:errors来绑定表单字段和错误信息。

<form th:action="@{/login}" method="post" th:object="${user}">
    <label>用户名:<input type="text" th:field="*{username}" /></label>
    <span th:errors="*{username}"></span>
    <label>密码:<input type="password" th:field="*{password}" /></label>
    <span th:errors="*{password}"></span>
</form>
总结
使用Thymeleaf的步骤包括引入依赖、配置Thymeleaf、创建模板文件、在控制器中使用Thymeleaf和在模板中使用Thymeleaf语法。Thymeleaf提供了强大而灵活的功能,使开发者能够方便地实现数据与页面的动态绑定。
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值