没有合适的资源?快使用搜索试试~ 我知道了~
thymeleafspring.pdf
需积分: 9 2 下载量 161 浏览量
2016-06-17
17:15:25
上传
评论
收藏 334KB PDF 举报
温馨提示
试读
34页
Java thymeleaf expansion package to facilitate the integration of spring.
资源推荐
资源详情
资源评论
Preface
This tutorial explains how Thymeleaf can be integrated with the Spring Framework, especially (but not only) Spring MVC.
Note that Thymeleaf has integrations for both versions 3.x and 4.x of the Spring Framework, provided by two separate
libraries called thymeleaf-spring3 and thymeleaf-spring4 . These libraries are packaged in separate .jar files
( thymeleaf-spring3-{version}.jar and thymeleaf-spring4-{version}.jar ) and need to be added to your classpath in
order to use Thymeleaf’s Spring integrations in your application.
The code samples and example application in this tutorial make use of Spring 4.x and its corresponding Thymeleaf
integrations, but the contents of this text are valid also for Spring 3.x. If your application uses Spring 3.x, all you have to
do is replace the org.thymeleaf.spring4 package with org.thymeleaf.spring3 in the code samples.
Page 2 of 34
1 Integrating Thymeleaf with Spring
Thymeleaf offers a set of Spring integrations that allow you to use it as a full-featured substitute for JSP in Spring MVC
applications.
These integrations will allow you to:
Make the mapped methods in your Spring MVC @Controller objects forward to templates managed by Thymeleaf,
exactly like you do with JSPs.
Use Spring Expression Language (Spring EL) instead of OGNL in your templates.
Create forms in your templates that are completely integrated with your form-backing beans and result bindings,
including the use of property editors, conversion services and validation error handling.
Display internationalization messages from messages files managed by Spring (through the usual MessageSource
objects).
Note that in order to fully understand this tutorial, you should have first gone through the “Using Thymeleaf” tutorial,
which explains the Standard Dialect in depth.
Page 3 of 34
2 The SpringStandard Dialect
In order to achieve an easier and better integration, Thymeleaf provides a dialect which specifically implements all the
needed features for it to work correctly with Spring.
This specific dialect is based on the Thymeleaf Standard Dialect and is implemented in a class called
org.thymeleaf.spring4.dialect.SpringStandardDialect , which in fact extends from
org.thymeleaf.standard.StandardDialect .
Besides all the features already present in the Standard Dialect – and therefore inherited –, the SpringStandard Dialect
introduces the following specific features:
Use Spring Expression Language (Spring EL) as a variable expression language, instead of OGNL. Consequently, all
${...} and *{...} expressions will be evaluated by Spring’s Expression Language engine.
Access any beans in your application context using SpringEL’s syntax: ${@myBean.doSomething()}
New attributes for form processing: th:field , th:errors and th:errorclass , besides a new implementation of
th:object that allows it to be used for form command selection.
An expression object and method, #themes.code(...) , which is equivalent to the spring:theme JSP custom tag.
An expression object and method, #mvc.uri(...) , which is equivalent to the spring:mvcUrl(...) JSP custom function
(only in Spring 4.1+).
New DTDs for validation, including these new attributes, as well as new corresponding DOCTYPE translation rules.
Note that you shouldn’t use this dialect directly in a normal TemplateEngine object as a part of its configuration. Instead,
you should instance a new template engine class that performs all the required configuration steps:
org.thymeleaf.spring4.SpringTemplateEngine .
An example bean configuration:
<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>
Page 4 of 34
3 Views and View Resolvers
3.1 Views and View Resolvers in Spring MVC
There are two interfaces in Spring MVC that conform the core of its templating system:
org.springframework.web.servlet.View
org.springframework.web.servlet.ViewResolver
Views model pages in our applications and allow us to modify and predefine their behaviour by defining them as beans.
Views are in charge of rendering the actual HTML interface, usually by the execution of some template engine like JSP (or
Thymeleaf).
ViewResolvers are the objects in charge of obtaining View objects for a specific operation and locale. Typically, controllers
ask ViewResolvers to forward to a view with a specific name (a String returned by the controller method), and then all the
view resolvers in the application execute in ordered chain until one of them is able to resolve that view, in which case a
View object is returned and control is passed to it for the renderization of HTML.
Note that not all pages in our applications have to be defined as Views, but only those which behaviour we wish to
be non-standard or configured in a specific way (for example, by wiring some special beans to it. If a ViewResolver
is asked a view that has no corresponding bean —which is the common case—, a new View object is created ad hoc
and returned.
A typical configuration for a JSP+JSTL ViewResolver in a Spring MVC application looks like this:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsps/" />
<property name="suffix" value=".jsp" />
<property name="order" value="2" />
<property name="viewNames" value="*jsp" />
</bean>
A quick look at its properties is enough to know about how it’s configured:
viewClass establishes the class of the View instances. This is needed for a JSP resolver, but it will not be needed at all
when we’re working with Thymeleaf.
prefix and suffix work in a similar way to the attributes of the same names in Thymeleaf’s TemplateResolver objects.
order establishes the order in which the ViewResolver will be queried in the chain.
viewNames allows the definition (with wildcards) of the view names that will be resolved by this ViewResolver.
3.2 Views and View Resolvers in Thymeleaf
Thymeleaf offers implementations for the two interfaces mentioned above:
org.thymeleaf.spring4.view.ThymeleafView
org.thymeleaf.spring4.view.ThymeleafViewResolver
These two classes will be in charge of processing Thymeleaf templates as a result of the execution of controllers.
Configuration of the Thymeleaf View Resolver is very similar to that of JSP’s:
Page 5 of 34
剩余33页未读,继续阅读
资源评论
史上最强的弟子
- 粉丝: 72
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功