不做浮躁的人
正在行走的人...
posts - 171,  comments - 51,  trackbacks - 0
1、使用的是Spring EL而不是Ognl。
2、访问上下文的Bean用${@myBean.doSomething()}
3、th:field,th:errors,th:errorclass用于form processing。
4、要采用SpringTemplateEngine。
5、基本配置:
<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、被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,如果@ModelAttribute注释的方法有返回值,则表示在model中存放隐含名称的属性对象,比如返回Account,则相当于model.addAttribute("account",account),如果@ModelAttribute(value="aname")注释方法,则表示在model中增加aname的属性值。@ModelAttribute注释一个方法的参数则表示从model中或者从Form表单或者url中获取。
7、 @RequestMapping("/hello")public void novoid() { },返回视图为前缀+/hello+后缀,当方法返回Map,ModelMap等时都是相当于Request.setAttribute()。
8、<td th:text="${{sb.datePlanted}}">13/01/2011</td>双括号表示自动使用转换,常用于格式转换。
9、<td th:text="${#strings.arrayJoin(#messages.arrayMsg(#strings.arrayPrepend(sb.features,'seedstarter.feature.')),', ')}">Electric Heating, Turf</td>,首先将数组feathers都加上前缀,然后利用messages翻译国际化,最终组合成一个字符串。
10、用th:object指定command object,比如:<form action="#" th:action="@{/save}" th:object="${person}" method="post">,两点限制,第一object只能是model 的直接attribute,不能使${person.baseInfo},第二,th:object的子级标签内不能再使用th:object。inputField使用:<input type="text" th:field="*{datePlanted}" />。
12、checkbox标签:
<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、预处理:<select th:field="*{rows[__${rowStat.index}__].variety}">而不使用<select th:field="*{rows[rowStat.index].variety}">,因为spring el不会计算数组索引中的变量或者表达式。
16、错误显示:<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>
全局错误:
<ul th:if="${#fields.hasErrors('global')}">
  <li th:each="err : ${#fields.errors('global')}" th:text="${err}">Input is incorrect</li>
</ul>
在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}">
    ...
</form>
17、利用功能类转换:#conversions.convert(Object,Class),#conversions.convert(Object,String)
18、渲染模板的片段,常用于ajax,返回一部分文本做替换使用。
在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";//返回上面定义的bean名称。
}
c:selectorExpression="content":需要在content节点加上th:fragment。
c:selectorExpression="#content" :完全基于html dom selector,无需th:fragment。
在controller中指定片段:
@RequestMapping("/showContentPart")
public String showContentPart() {
    ...
    return "index :: content";
}
"index :: content"和"index ::#content"区别一样。
还可以返回带参数的片段:
@RequestMapping("/showContentPart")
public String showContentPart() {
    ...
    return "index :: #content ('myvalue')";
}








posted on 2014-02-11 16:16 不做浮躁的人 阅读(23402) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2014年2月>
2627282930311
2345678
9101112131415
16171819202122
2324252627281
2345678

常用链接

留言簿(9)

随笔分类(31)

随笔档案(75)

文章分类(1)

文章档案(3)

搜索

  •  

最新评论

阅读排行榜

评论排行榜