thymeleaf表达式

 

index.html

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>$Title$</title>
    <link rel="stylesheet" th:src="@{/css/css.css}">

</head>
<body>
注释:<!--/*-->
<a th:href="@{/order/list}">ahref</a>   <!--*/--> ||
<!--
<a th:href="@{/order/list}">ahref</a>   -->
<hr>
消息:  [[${#messages.msg('i18n.no')}]]
||     [[${#messages.msgOrNull('i18n.no')}]]
||     [[#{i18n.welcome}]] &nbsp;
<zsz th:text="#{i18n.welcome}">test</zsz>
<hr>

[[${thymeleaf}]] &nbsp;
<input type="text" name="userName" th:value="${thymeleaf}"/>
<hr>

上下文1: <a th:href="@{/order/list}">ahref</a>
<hr>
servletContext上下文2:
[[${#servletContext.contextPath}]]
contextPath: <a th:href="${#servletContext.contextPath}">contextPath</a>
<hr>
语言:[[${#locale}]]  <hr>

参数:
[[${param.test}]]
<hr>
请求属性 :
[[${#request.getAttribute('req')}]]  &nbsp; ||
<span th:text="${#request.getAttribute('req')}">US</span>.
||[[${req}]]  &lt;  请求属性无需指定命名空间,会自动增加到上下文变量中 &gt;
<hr>
会话相关:
<span th:text="${#session.id}">US</span>.|| session attr:
[[${session.session}]]
<hr>
包含:<div th:insert="~{footer :: copy}"></div>

设置标签属性(条件):
<input type="checkbox" name="active" th:checked="${session.session1}"
       />ddd
<input type="checkbox" name="active" th:checked="${session.session}"
/>ddd
<hr>
</body>
<script  th:inline="javascript">
  var username = [[${session.session}]];
  // alert("javascript使用thymeleaf"+username);
  document.body.append("javascript使用thymeleaf"+username);
</script>
</html>

 

 

 footer.html

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<body>

<div th:fragment="copy">
    &copy; 2011 The Good Thymes Virtual Grocery
</div>

</body>

</html>

Listener

package  com.thymeleaf1.listener;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener()
public class ListenerTest implements ServletContextListener {

    private static final String TEMPLATE_ENGINE_INST = "com.test.thymeleaf.TemplateEngine";

    public static void storeTemplateEngine(ServletContext context, TemplateEngine engine) {
        context.setAttribute(TEMPLATE_ENGINE_INST, engine);
    }

    public static TemplateEngine getTemplateEngine(ServletContext context) {
        return (TemplateEngine) context.getAttribute(TEMPLATE_ENGINE_INST);
    }


    // Public constructor is required by servlet spec
    public ListenerTest() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */


        ServletContextTemplateResolver templateResolver =
                new ServletContextTemplateResolver(sce.getServletContext());
// HTML is the default mode, but we set it anyway for better understanding of code
        templateResolver.setTemplateMode(TemplateMode.HTML);
// This will convert "home" to "/WEB-INF/templates/home.html"
        templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
// Template cache TTL=1h. If not set, entries would be cached until expelled
        templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
// Cache is set to true by default. Set to false if you want templates to
// be automatically updated when modified.
        templateResolver.setCacheable(false);
        templateResolver.setCharacterEncoding("UTF-8");

        TemplateEngine engine = new TemplateEngine();
        engine.setTemplateResolver(templateResolver);

        storeTemplateEngine(sce.getServletContext(), engine);
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }


}

Servlet

package com.thymeleaf1.servlet;


import com.thymeleaf1.listener.ListenerTest;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletTest", urlPatterns = "/test")
public class ServletTest extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        request.setAttribute("req","req attr");
        request.getSession().setAttribute("session","session attr");
        TemplateEngine engine = ListenerTest.getTemplateEngine(request.getServletContext());
        WebContext context = new WebContext(request, response, request.getServletContext());
        response.setCharacterEncoding("utf-8");
        context.setVariable("thymeleaf", "first  thymeleaf page!!");
        engine.process("index", context, response.getWriter());
    }
}

运行效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值