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过滤器