thymeleaf dependency
<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 template
<!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>
effect
Filter
New filter class
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 is used to declare a class as a filter. The annotation will be processed by the container when the application is deployed. The container will deploy the corresponding class as a filter according to the specific attribute configuration, so that the web application does not need to be on the web when using the listener. Configure the relevant description information of the listener in the xml file. Common attributes include filterName, which specifies the name of the filter, which is equivalent to the filter-name tag of the xml configuration file, and urlPatterns, which is used to specify the URL matching mode of a set of filters, equivalent In the url-pattern in the xml configuration file, there is also a value, which is equivalent to urlPatterns and cannot be used at the same time.
Entry class
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 allows Servlet, Filter, and Listener to be automatically registered directly through @WebServlet, @WebFilter, and @WebListener annotations, without writing other codes
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 declares a class as a listener. The annotation will be processed by the container when the application is deployed. The container will deploy the corresponding class as a listener according to the specific attribute configuration, so that the web application does not need to be in the web.xml file when using the listener. Configure the relevant description information of the listener in
ServletContextListener monitors the life cycle of the ServletContext object, which is actually the life cycle of the Web application.
When the Servlet container is started or the Web application is terminated, the ServletContextEvent event will be triggered, which is
handled by the ServletContextListener class
contextInitialized, when the servlet container starts the Web application, first call this method, then initialize the Filter, and initialize the servlets that need to be initialized when the application starts
contextDestroyed, this method is called when the Servlet container terminates the web application. Before calling this method, the container will destroy all Servlet and Filter filters