3. Servlets
Hello!
public class HelloWorldServlet extends HttpServlet {
protected void doGet(request, response) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>"); out.println("<head>");
out.println("<title>Hello!</title>");
out.println("</head>");
out.println("<body bgcolor="white">");
out.println("</body>");
out.println("</html>");
}
}
Html symbols are writing directly to the browser from java code.
4. Servlets
Hello!
public class HelloWorldServlet extends HttpServlet {
protected void doGet(request, response) {
request.getRequestDispatcher("/login.jsp").
forward(request, response);
}
}
Html code saved separately from java code
and can be dynamically changed before sending to browser . It’s good.
<html>
<head>
<title>Hello!</title>
</head>
<body bgcolor=“gray">
<h1>Hello!</h1>
</body>");
</html>
10. Custom properties
Core is a DOM processing engine.
It is based on XML tags and attributes.
XML/Valid XML/XHTML/Valid XHTML/HTML5/Legacy
HTML5 processor.
Allows a fast processing of templates by intelligent caching
of parsed files.
Not complex syntax.
13. Integration with Spring framework
Spring application context:
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
Html file template:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
14. Standard dialect
Expressions:
Variable Expressions: ${...}
Selection Variable Expressions: *{...}
Message Expressions: #{...}
Link URL Expressions: @{...}
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
Using with basic objects:
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#httpServletRequest : (only in Web Contexts) the
HttpServletRequest object.
#httpSession : (only in Web Contexts) the
HttpSession object.
15. Expression Utility Objects:
#dates : utility methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : utility methods for formatting numeric objects.
#strings : utility methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : utility methods for objects in general.
#bools : utility methods for boolean evaluation.
#arrays : utility methods for arrays.
#lists : utility methods for lists.
#sets : utility methods for sets.
#maps : utility methods for maps.
#aggregates : utility methods for creating aggregates on arrays or collections.
#messages : utility methods for obtaining externalized messages inside variables expressions, in the
same way as they would be obtained using #{…} syntax.
#ids : utility methods for dealing with id attributes that might be repeated.
21. Local variable:
<div th:with="firstPer=${persons[0]},secondPer=${persons[1]}">
<span th:text="${secondPer.name}">Some Name</span>.
</div>
Set values to JavaScript:
<script th:inline="javascript">
/*<![CDATA[*/
...
var username = /*[[${session.user.name}]]*/ ‘User';
...
/*]]>*/
</script>
Switch:
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
</div>
22. Summary
Advatages:
Java template engine for XML, XHTML and HTML5.
Works both in web and non-web (offline) environments. No hard dependency
on the Servlet API.
Several template modes: XML, XHTML 1.0 and 1.1, HTML5:
Internationalization support.
Parsed template cache
Is extensible
Not very complex in usage
Many documentation
Disadvatages:
It's slowly than other templates.