ThymeLeaf
1.简介
ThymeLeaf是springboot默认支持的服务器端Java模板引擎,与JSP类似,但是没有JSP性能优秀,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用
2.如何使用
只需要在pom文件中增加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
等待下载jar包,然后在resources中增加文件夹templates,将html放入templates,访问时直接访问localhost:8080/xxx.html即可
3.基本用法
①基本传值
后端通过model传值,前端接收
model.addAttribute("dept1",dept);
<p th:text="${dept1.deptNo}"></p>
或者
[[${dept1.deptNo}]]
②条件表达式
<p th:if="${dept1.deptNo == 001}">xxx</p>
③常用内置对象
request,response,session
用法:<p th:text="${session.userName}"></p>
④工具类
用法:<p th:text="${#dates.format(dept1.birthDay,'yyyy/MM/dd')}"></p>
④+1遍历
用法:
错误页面的处理
1.springboot对于错误页面的处理分为两种,如果添加啦ThymeLeaf模板,那错误页面处理就在resources–>templates–>error,如果没添加模板错误页面处理就在resources–>static–>error
2.在error中是通过错误码来命名html文件的,比如404错误,404.html,当然也可以使用4xx.html,但是404.html优先级高于4xx.html
SpringBoot中使用servlet,filter,listener,拦截器(inteceptor)
1.使用servlet
package club.laomile.springboottest2.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("123456Servlet");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
package club.laomile.springboottest2.Config;
@Configuration
public class MyWebMvcConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
return servletRegistrationBean;
}
}
2.使用filter
package club.laomile.springboottest2.Filter;
import javax.servlet.*;
import java.io.IOException;
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("这里面是过滤器");
filterChain.doFilter(servletRequest,servletResponse);
}
}
package club.laomile.springboottest2.Config;
@Configuration
public class MyWebMvcConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.addUrlPatterns("/myServlet");
return filterRegistrationBean;
}
}
3.使用listener
package club.laomile.springboottest2.Listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("删除");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("创建");
}
}
package club.laomile.springboottest2.Config;
@Configuration
public class MyWebMvcConfig {
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new MyListener());
return servletListenerRegistrationBean;
}
}
4.使用拦截器(inteceptor)
package club.laomile.springboottest2.inteceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInteceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("这里是拦截器");
return true;
}
}
package club.laomile.springboottest2.Config;
import club.laomile.springboottest2.inteceptor.MyInteceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInteceptor()).addPathPatterns("/list");
}
}