文章目录
01-mvc模型(掌握)
- 为什么?

- 现在,会在java代码编写html代码用于渲染页面,这明显不合理;但是,也无法在html文件中使用java代码中的数据。
- mvc模型

- C : controller,控制器 , java程序
- ①处理请求
- ②调度页面
- M : model,模型 , java数据(javabean、基本类型、String…)
- ①封装数据
- V : view , 视图 , html程序
- ①渲染页面
- 将模型渲染视图使用thymeleaf技术。
02-thymeleaf概述(掌握)
- 概述
- Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP,Velocity,FreeMaker 等, 它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。它的主要作用 是在静态页面上渲染显示动态数据
- 特点
- SpringBoot官方推荐使用的视图模板技术,和SpringBoot完美整合。
03-物理视图和逻辑视图(掌握)
- 概述
- 物理视图:请求转发到一个资源时使用的绝对路径
- 逻辑视图:物理视图=视图前缀+逻辑视图+视图后缀
- 视图前缀:物理视图中最大的公共前缀
- 视图后缀:物理视图中最大的公共后缀(后缀名)
- 举例
04-thymeleaf入门(掌握)
-
开发步骤
- ①引入thymeleaf的相关jar包
- ②编写web.xml
- 配置视图前缀
- 配置视图后缀
- ③定义ViewBaseServlet类
- ④定义Servlet类继承ViewBaseServlet类
- ⑤编写html页面
- 使用thymeleaf渲染页面
-
①引入thymeleaf的相关jar包
-
②编写web.xml
<!--视图前缀--> <context-param> <param-name>view-prefix</param-name> <param-value>/pages/</param-value> </context-param> <!--视图后缀--> <context-param> <param-name>view-suffix</param-name> <param-value>.html</param-value> </context-param> -
③定义ViewBaseServlet类
public class ViewBaseServlet extends HttpServlet { private TemplateEngine templateEngine; @Override public void init() throws ServletException { // 1.获取ServletContext对象 ServletContext servletContext = this.getServletContext(); // 2.创建Thymeleaf解析器对象 ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext); // 3.给解析器对象设置参数 // ①HTML是默认模式,明确设置是为了代码更容易理解 templateResolver.setTemplateMode(TemplateMode.HTML); // ②设置前缀 String viewPrefix = servletContext.getInitParameter("view-prefix"); templateResolver.setPrefix(viewPrefix); // ③设置后缀 String viewSuffix = servletContext.getInitParameter("view-suffix"); templateResolver.setSuffix(viewSuffix); // ④设置缓存过期时间(毫秒) templateResolver.setCacheTTLMs(60000L); // ⑤设置是否缓存 templateResolver.setCacheable(true); // ⑥设置服务器端编码方式 templateResolver.setCharacterEncoding("utf-8"); // 4.创建模板引擎对象 templateEngine = new TemplateEngine(); // 5.给模板引擎对象设置模板解析器 templateEngine.setTemplateResolver(templateResolver); } /** * 执行视图 * @param templateName : 逻辑视图 * @param req : 请求 * @param resp : 响应 * @throws IOException */ protected void processTemplate(String templateName, HttpServletRequest req, HttpServletResponse resp) throws IOException { // 1.设置响应体内容类型和字符集 resp.setContentType("text/html;charset=UTF-8"); // 2.创建WebContext对象 WebContext webContext = new WebContext(req, resp, getServletContext()); // 3.处理模板数据 templateEngine.process(templateName, webContext, resp.getWriter()); } } -
④定义Servlet类继承ViewBaseServlet类
@WebServlet("/demo01") public class Demo01Servlet extends ViewBaseServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String errorMsg = "用户名已经存在!"; req.setAttribute("errorMsg", errorMsg); //转发到demo01.html processTemplate("demo01", req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } -
⑤编写html页面
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>04-thymeleaf入门</title> </head> <body> <font color="red" th:text="${errorMsg}"></font> </body> </html> -
注意事项
- 如果要使用thymeleaf指令,必须要通过ViewBaseServlet
05-thymeleaf修改标签内容(掌握)
-
概述
- 使用th:text指令修改标签内容
-
代码实现
@WebServlet("/demo02") public class Demo02Servlet extends ViewBaseServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String msg = "helloworld"; request.setAttribute("msg", msg); processTemplate("demo02", request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>05-thymeleaf修改标签内容</title> </head> <body> <span th:text="${msg}"></span> </body> </html>
06-thymeleaf修改标签属性(掌握)
-
概述
- 使用"th:属性"修改标签的属性值
-
代码实现
@WebServlet("/demo03") public class Demo03Servlet extends ViewBaseServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String msg = "helloworld"; request.setAttribute("msg", msg); processTemplate("demo03", request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>06-thymeleaf修改标签属性</title> </head> <body> <input type="text" th:value="${msg}"> <a th:href="${msg}">超链接</a> </body> </html>
07-thymeleaf解析url(掌握)
-
代码实现1 : 不带请求参数
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>07-thymeleaf解析url</title> <!--不带参数的URL--> </head> <body> <a href="/day06/demo04">请求Demo04Servlet</a> <a th:href="@{/demo04}">请求Demo04Servlet</a> </body> </html> -
代码实现2 : 携带请求参数
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>07-thymeleaf解析url</title> <!--不带参数的URL--> </head> <body> <a href="/day06/demo05?msg=aaa&msg2=bbb">请求Demo04Servlet</a> <a th:href="@{/demo05(msg=aaa,msg2=bbb)}">请求Demo04Servlet</a> </body> </html>
08-thymeleaf操作域对象(掌握)
-
域对象
- ServletRequest
- HttpSession
- ServletContext
-
代码实现
@WebServlet("/demo06") public class Demo06Servlet extends ViewBaseServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //当前请求有效 request.setAttribute("msg1","request"); //当前会话有效 request.getSession().setAttribute("msg2","session"); //当前项目有效 getServletContext().setAttribute("msg3","servletContext"); processTemplate("demo06",request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>08-thymeleaf操作域对象</title> </head> <body> <span th:text="${msg1}"></span> <span th:text="${session.msg2}"></span> <span th:text="${application.msg3}"></span> </body> </html>
09-OGNL表达式(掌握)
- 概述
- OGNL : Object-Graph Navigation Language : 对象-图导航语言
- OGNL
10-OGNL使用(掌握)
-
代码实现
@WebServlet("/demo07") public class Demo07Servlet extends ViewBaseServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Student student = new Student(); student.setStudentName("樊育东"); student.setSubject(new Subject("java")); List<School> schoolList = new ArrayList<>(); schoolList.add(new School("尚大幼儿园")); schoolList.add(new School("尚大小学")); schoolList.add(new School("尚大中学")); schoolList.add(new School("尚大大学")); student.setSchoolList(schoolList); Map<String, Teacher> teacherMap = new HashMap<>(); teacherMap.put("t1", new Teacher("xiaoqiu")); teacherMap.put("t2", new Teacher("oldqiu")); student.setTeacherMap(teacherMap); request.setAttribute("student", student); processTemplate("demo07", request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>10-OGNL使用</title> </head> <body> 姓名: <span th:text="${student.studentName}"></span> 学科名称: <span th:text="${student.subject.subjectName}"></span> 第一个学校名称 : <span th:text="${student.schoolList[0].schoolName}"></span> 老师姓名: <span th:text="${student.teacherMap.t2.teacherName}"></span> </body> </html>
11-thymeleaf条件渲染(掌握)
-
概述
- 使用th:if、th:unless指令来进行条件渲染。
-
代码实现
@WebServlet("/demo08") public class Demo08Servlet extends ViewBaseServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean flag = false; request.setAttribute("flag", flag); processTemplate("demo08", request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>11-thymeleaf条件渲染</title> </head> <body> <span th:if="${flag}"> 这是一个span if </span> <span th:unless="${flag}"> 这是一个span unless </span> </body> </html>
本文详细介绍了Thymeleaf在Java Web中的应用,从MVC模型讲解到条件渲染,包括视图解析、域对象操作和OGNL表达式的运用,是SpringBoot下高效模板引擎的实践指南。



1087

被折叠的 条评论
为什么被折叠?



