Thymeleaf的使用
Thymelaef帮助我们将数据与与html进行结合,渲染到界面上
要将这些水果以list的方式渲染到界面上
使用步骤
导入jar包
写入方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| package com.zss.servlets;
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 {
ServletContext servletContext = this.getServletContext();
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext);
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");
templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
}
protected void processTemplate(String templateName, HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html;charset=UTF-8");
WebContext webContext = new WebContext(req, resp, getServletContext());
templateEngine.process(templateName, webContext, resp.getWriter()); } }
|
在web.xml中添加配置
下面中的param-name分别配置上下文参数,而其分别对应ViewBaseServlet的对应的上下文参数
1 2 3 4 5 6 7 8 9
| <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>
|
继承ViewBaseServlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.zss.servlets;
import com.zss.impl.FruitDAOImpl; import com.zss.object.Fruit;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.util.List;
public class InquireFruitServlet extends ViewBaseServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { req.setCharacterEncoding("UTF-8"); FruitDAOImpl fruitDAO=new FruitDAOImpl(); List<Fruit> list=fruitDAO.inquFruit(new Fruit()); HttpSession session=req.getSession(); session.setAttribute("fruitList",list); for (int i=0;i<list.size();i++){ System.out.println(list.get(i)); } super.processTemplate("show/FruitShow",req,resp); }
}
|
进行判断,添加对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <tr th:if="${#lists.isEmpty(session.fruitList)}" > <td colspan="4">对不起,库存为空</td> </tr>
<tr th:unless="${#lists.isEmpty(session.fruitList)}" th:each="fruit : ${session.fruitList}"> <td th:text="${fruit.fid}">1</td> <td th:text="${fruit.fname}">苹果</td> <td th:text="${fruit.price}">5</td> <td th:text="${fruit.fcount}">20</td> <td th:text="${fruit.remark}">无</td> <td>100</td> <td>×</td> </tr> <tr >
|