员工列表-公共页抽取
公共片段抽取
<!-- 1、抽取公共片段 --> <div th:fragment="copy">content</div> <!-- 2、引入公共片段 --> <div th:insert="~{footer::copy}">content</div> <!-- 3、默认效果 --> <!-- insert功能片段在div标签中 -->
~{templateName::selector} 模板名::选择器 ~{templateName::fragmentName}模板名::片段名
3 种方式引入片段
th:insert 插入
th:replace 替换
th:include 引入片段内容
使用th:insert可以不写~{}
转义[[~{}]]
不转义[(~{})]
引入片段时候传入参数
链接高亮&列表完成
redirect 重定向
forward 转发
日期格式化
spring.mvc.format.date: yyyy-MM-dd
HiddenHttpMethodFilter
SpringMVC 中配置 HiddenHttpMethodFilter
页面创建一个 Post 表单
创建一个 input 项 name="_method",值就是请求方式
<input type="hidden" name="_method" value="put" th:if="${emp!=null}" />
有些版本可能需要配置
application.yml
spring.mvc.hiddenmethod.filter.enabled = true
错误页面
ErrorMvcAutoConfiguration
有模板引擎的情况下
错误页面 ErrorMvcAutoConfiguration 有模板引擎的情况下
自定义异常处理
1、返回 JSON 数据
package com.example.demo.controller; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; @ControllerAdvice public class ExceptionController { @ResponseBody @ExceptionHandler public Map<String, Object> handleException(Exception e){ Map<String, Object> map = new HashMap<>(); map.put("code", -1); map.put("msg", e.getMessage()); return map; } }
{ "msg": "用户不存在", "code": -1 }
嵌入式 Servlet 容器配置修改
Tomcat
通用配置
# servlet配置 server.port=8001 server.context-path=/demo # Tomcat配置 server.tomcat.uri-encoding=UTF-8
注册 servlet 三大组件
Servlet/Filter/Listener
1、Servlet
package com.example.demo.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 doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().println("MyServlet"); } }
2、Filter
package com.example.demo.filter; import javax.servlet.*; import java.io.IOException; public class MyFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("MyFilter"); chain.doFilter(request, response); } }
3、Listener
package com.example.demo.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("contextInitialized"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("contextDestroyed"); } }
注册组件
package com.example.demo.config; import com.example.demo.filter.MyFilter; import com.example.demo.listener.MyListener; import com.example.demo.servlet.MyServlet; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Arrays; /** * 注册Servlet/Filter/Listener组件 */ @Configuration public class MyServletConfig { @Bean public ServletRegistrationBean myServlet(){ ServletRegistrationBean registrationBean = new ServletRegistrationBean(); registrationBean.setServlet(new MyServlet()); registrationBean.setUrlMappings(Arrays.asList("/servlet")); return registrationBean; } @Bean public FilterRegistrationBean myFilter(){ FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new MyFilter()); registrationBean.setUrlPatterns(Arrays.asList("/servlet")); return registrationBean; } @Bean public ServletListenerRegistrationBean myListener(){ ServletListenerRegistrationBean registrationBean = new ServletListenerRegistrationBean(new MyListener()); return registrationBean; } }
访问
http://localhost:8080/servlet
默认拦截: /
修改 server.servletPath
其他 Servlet 容器
Tomcat 默认
Jetty 长连接
Undertow 不支持 jsp
嵌入式容器
外部容器
jar 包:执行 SpringBoot 主类的 main 方法,启动 IOC 容器,创建嵌入式的 Servlet 容器
war 包:启动服务器,服务器启动 SpringBoot,启动 IOC 容器