SPRING + THYMELEAF Configuration

Source: Internet
Author: User

1. Use spring El instead of OGNL.
2. The bean that accesses the context uses ${@myBean. dosomething ()}
3, Th:field,th:errors,th:errorclass for form processing.
4, to adopt Springtemplateengine.
5. Basic 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.spring3.SpringTemplateEngine" >
<property name= "Templateresolver" ref= "Templateresolver"/>
</bean>

<bean class= "Org.thymeleaf.spring3.view.ThymeleafViewResolver" >
<property name= "Templateengine" ref= "Templateengine"/>
<property name= "Order" value= "1"/>
<property name= "Viewnames" value= "*.html,*.xhtml"/>
</bean>


6, methods that are annotated by @modelattribute are executed before each method of the controller is executed, and if the method of the @modelattribute annotation has a return value, the Property object that holds the implied name in the model. Return account, for example, is equivalent to Model.addattribute ("account", account), if @modelattribute (value= "aname") annotation method, Indicates that the value of the Aname property is increased in the model. @ModelAttribute Note the parameters of a method are taken from the model or from the form form or URL.
7, @RequestMapping ("/hello") public void Novoid () {}, returns the view as a prefix +/hello+ suffix, when the method returns a map, Modelmap and so on are equivalent to Request.setattribute ().
8, <td th:text= "${{sb.dateplanted}" >13/01/2011</td> double brackets denote automatic use of transformations, often used in format conversions.
9, <td th:text= "${#strings. Arrayjoin (#messages. arraymsg (#strings. Arrayprepend (Sb.features, ' Seedstarter.feature. ')}, ', ')} ">electric heating, turf</td> First prefix the array feathers and then use the messages translation to internationalize, Eventually combined into a string.
10, specify command object with Th:object, for example: <form action= "#" th:action= "@{/save}" th:object= "${person}" method= "POST" , two-point limit, the first object can only be the model of the direct attribute, cannot make ${person.baseinfo}, second, th:object child tags can no longer use Th:object. Inputfield use: <input type= "text" th:field= "*{dateplanted}"/>.
12, checkbox label:

<div>
<label th:for= "${#ids. Next (' Covered ')}" th:text= "#{seedstarter.covered}" >Covered</label>
<input type= "checkbox" th:field= "*{covered}"/>
</div>
CheckBox array:
<ul>
<li th:each= "feat: ${allfeatures}" >
<input type= "checkbox" th:field= "*{features}" th:value= "${feat}"/>
<label th:for= "${#ids. Prev (' Features ')} ' th:text= ' #{${' seedstarter.feature. ' + feat} ' >Heating</label>
</li>
</ul>

13, Radios:
<ul>
<li th:each= "ty: ${alltypes}" >
<input type= "Radio" th:field= "*{type}" th:value= "${ty}"/>
<label th:for= "${#ids. Prev (' type ')}" th:text= "#{${' seedstarter.type. ' + ty}}" >Wireframe</label>
</li>
</ul>
14, DropDownList or select.
<select th:field= "*{type}" >
<option th:each= "type: ${alltypes}"
Th:value= "${type}"
th:text= "#{${' seedstarter.type. ' + type} ' >Wireframe</option>
</select>

15, preprocessing: <select th:field= "*{rows[__${rowstat.index}__].variety}" > instead of using <select th:field= "*{rows[ Rowstat.index].variety} ", because spring El does not evaluate variables or expressions in the array index.
16, Error display: <input type= "text" th:field= "*{dateplanted}" th:class= "${#fields. HasErrors (' dateplanted ')}? Fielderror "/>
<ul>
<li th:each= "ERR: ${#fields. Errors (' dateplanted ')}" th:text= "${err}"/>
</ul>
<input type= "text" th:field= "*{dateplanted}"/>
<p th:if= "${#fields. HasErrors (' dateplanted ')}" th:errors= "*{dateplanted}" >incorrect date</p>
<input type= "text" th:field= "*{dateplanted}" class= "small" th:errorclass= "Fielderror"/>
<ul th:if= "${#fields. HasErrors (' * ')}" >
<li th:each= "ERR: ${#fields. Errors (' * ')}" th:text= "${err}" >input is incorrect</li>
</ul> Global Error:
<ul th:if= "${#fields. HasErrors (' Global ')}" >
<li th:each= "ERR: ${#fields. Errors (' Global ')}" th:text= "${err}" >input is incorrect</li>
</ul> display errors outside the form:
<div th:errors= "${myform}" >...</div>
<div th:errors= "${myform.date}" >...</div>
<div th:errors= "${myform.*}" >...</div>

<div th:if= "${#fields. HasErrors (' ${myform} ')}" >...</div>
<div th:if= "${#fields. HasErrors (' ${myform.date} ')}" >...</div>
<div th:if= "${#fields. HasErrors (' ${myform.*} ')}" >...</div>

<form th:object= "${myform}" >
...
&LT;/FORM&GT;17, using the function class conversion: #conversions. CONVERT (Object,class), #conversions. CONVERT (object,string)
18, the fragment of the rendering template, commonly used in Ajax, return a part of the text to replace the use.
To specify a fragment in Viewbean:
<bean name= "Content-part" class= "Org.thymeleaf.spring3.view.ThymeleafView" >
<property name= "templatename" value= "index"/>
<property name= "Fragmentspec" >
<bean class= "Org.thymeleaf.standard.fragment.StandardDOMSelectorFragmentSpec"
c:selectorexpression= "Content"/>
</property>
</bean>
@RequestMapping ("/showcontentpart")
Public String Showcontentpart () {
...
return "Content-part";//returns the bean name defined above.
}
c:selectorexpression= "Content": you need to add th:fragment to the content node. C:selectorexpression= "#content": completely based on the HTML DOM selector, without th:fragment.
Specify the fragment in the controller:
@RequestMapping ("/showcontentpart")
Public String Showcontentpart () {
...
Return "Index:: Content";
The difference between "index:: Content" and "Index:: #content" is the same.
You can also return a fragment with parameters:
@RequestMapping ("/showcontentpart")
Public String Showcontentpart () {
...
Return "index:: #content (' myvalue ')";
}

SPRING + THYMELEAF Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.