JavaWeb-Servlet(2)
Web-Servlet(2)--Thymeleaf
视图模板技术,是做视图渲染的一个技术(静态页面和数据柔和在一起)
基本流程
在服务器端引入Thymeleaf环境
1.加入jar包
2.新建一个Servlet类ViewBaseServlet
package src.com.wht.myssm.myspringmvc;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import javax.servlet.ServletContext;
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 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);
}
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());
}
}
3.在web.xml文件中添加配置
<!-- 配置上下文参数 -->
<context-param>
<param-name>view-prefix</param-name><!--前缀-->
<param-value>/</param-value>
</context-param>
<context-param>
<param-name>view-suffix</param-name><!--后缀-->
<param-value>.html</param-value>
</context-param>
4.使我们的Servlet继承ViewBaseServlet
public class indexServlet extends ViewBaseServlet
5.根据逻辑视图名称,得到物理视图名称
-
此处的视图名称是 index
-
那么thymeleaf会将这个 逻辑视图名称 对应到 物理视图 名称上去//逻辑视图名称 : index
-
物理视图名称 : view-prefix + 逻辑视图名称 + view-suffix
-
所以真实的视图名称是: / index .html
6.使用Thymeleaf的标签
th:if, th:unless, th:each, th:text
Servlet-保存作用域
保存作用域:
保存作用域我们可以认为有4个:page(目前不用),request(一次请求响应范围),session(一次会话范围),application(整个应用程序范围)
- request:
代码的小例子(主代码):
@WebServlet("/demo01")
public class Demo01Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.向request保存作用域保存数据
request.setAttribute("uname","lili");
//2.客户端重定向
//response.sendRedirect("demo02");
//3.服务器端转发
//request.getRequestDispatcher("demo02").forward(request,response);
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取request保存作用域保存的数据,key为uname
Object unameObj = request.getAttribute("uname");
System.out.println("unameObj = " + unameObj);
}
重定向的结果:
服务器转发的结果:
-
session:(上一篇有哦)
-
application:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.向application保存作用域保存数据
//ServletContext : Servlet上下文
ServletContext application = request.getServletContext();
application.setAttribute("uname","lili");
//2.客户端重定向
response.sendRedirect("demo06");
//3.服务器端转发
//request.getRequestDispatcher("demo04").forward(request,response);
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取application保存作用域保存的数据,key为uname
ServletContext application = request.getServletContext() ;
Object unameObj = application.getAttribute("uname");
System.out.println("unameObj = " + unameObj);
}
都可以打印出,不管是直接搜demo05、demo06、还是换一个浏览器再搜索,都可以的(只要不停掉tomcat)
路径问题
- 相对路径(../返回上一级)红字
- 绝对路径 蓝字