SpringBoot之Thymeleaf模板、过滤器、监听器

thymeleaf依赖

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

Controller

package com.zzf.demo.controller;

import com.zzf.demo.model.ZUser;
import com.zzf.demo.service.ZUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/*
 *
 *@author:zzf
 *@time:2020-12-01
 *
 */
@Controller
@RequestMapping("/zUser")
public class ZUerController {
    @Autowired
    private ZUserService zUserService;

    @RequestMapping("/test")
    public String test(Model model){
        //查询
        List<ZUser> zUsers=zUserService.findAll();
        model.addAttribute("users",zUsers);
        return "zUser";
    }
}

thymeleaf模板

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>用户名</td>
        <td>密码</td>
    </tr>
    <tr th:each="user:${users}">
        <td th:text="${user.name}"></td>
        <td th:text="${user.password}"></td>
    </tr>
</table>
</body>
</html>

效果
在这里插入图片描述

过滤器Filter

新建过滤器类

package com.zzf.demo.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/*
 *
 *@author:zzf
 *@time:2020-12-01
 *
 */
@WebFilter(filterName = "zUserFilter",urlPatterns = "/*")
public class ZUserFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        System.out.println("------------->>>init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("------------->>>doFilter");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("------------->>>destory");

    }
}

@WebFilter用于一个类声明为过滤器,该注解会在应用部署时被容器处理掉,容器根据具体的属性配置将相应的类部署为过滤器,这样Web应用使用监听器时不需要在web.xml文件中配置监听器的相关描述信息,常用属性有filterName,指定过滤器的name,等价于xml配置文件的filter-name标签,urlPatterns,用于指定一组过滤器的URL匹配模式,等价于xml配置文件中的url-pattern,还有个value,等价于urlPatterns,不能同时使用。

入口类

package com.zzf.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

@ServletComponentScan可以让Servlet、Filter、Listener直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,不用写其它代码

监听器Listener

package com.zzf.demo.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/*
 *
 *@author:zzf
 *@time:2020-12-01
 *
 */
@WebListener
public class ZUserListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("ServletContext上下文初始化");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("ServletContext上下文销毁");
    }
}

@WebListener声明一个类为监听器,该注解会在应用部署时被容器处理掉,容器根据具体的属性配置将相应的类部署为监听器,这样Web应用使用监听器时不需要在web.xml文件中配置监听器的相关描述信息

ServletContextListener,监听ServletContext对象的生命周期,实际是Web应用的生命周期
当Servlet容器启动或Web应用终止时,会触发ServletContextEvent 事件,由
ServletContextListener类处理

contextInitialized,servlet容器启动Web应用时,先调用完该方法,再对Filter初始化,并对那些在应用启动时需要被初始化的Servlet初始化

contextDestroyed,Servlet容器终止Web应用时调用该方法,调用该方法之前,容器会销毁所有的Servlet和Filter过滤器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值