2014-06-05 3 views
1

사용 JSP 우리는 (크기 6 span6된다) HTML 엘리먼트를 렌더링 할 수있다. 내 필드 목록 6, 3, 8, 4와 같은 크기의 4 개 요소가있는 경우 내가 thymeleaf하는 처음 몇 부분을 변환하려고Thymeleaf 렌더링

<div class="row"> 
    <div class="span6"> 
    <input /> 
    <div> 
    <div class="span3"> 
    <input /> 
    <div> 
</div> 
<div class="row"> 
    <div class="span8"> 
    <input /> 
    <div> 
    <div class="span4"> 
    <input /> 
    <div> 
</div> 

로는 HTML을 렌더링과 같은

<th:block th:with="size=0"> 
    <th:block th:each="field, iterStat : ${fields}"> 
     <th:block if="${iterStat.index == 0}"> 
      <div class="row"> 
     </th:block> 
      <th:block if="${iterStat.index == (#lists.size(${fields}) - 1)}"> 
      </div> 
     </th:block> 
    </th:block> 
</th:block> 

그러나 div 태그 <div class="row">이 시작된 <th:block> 내에서 닫히지 않으므로 thymeleaf에서 렌더링 할 수 없습니다. 위의 jsp 코드를 thymeleaf로 변환 할 수있는 방법이 있습니까?

나는 thymeleaf 2.0.17 및 spring3를 사용하고

답변

6

내가 당신이라면 나는 문제의 분리를 달성하는 것 또한 당신이 뭘 하려는지 달성 할 다음 코드를 사용합니다 :

먼저 만듭니다 크기 목록에서 하위 목록을 만드는 일을 처리하는 클래스입니다. Thymeleaf에서이 클래스를 쉽게 사용할 수 있도록이 클래스를 Spring 빈으로 만들 수 있습니다.

그것은 작동
<div class="row" th:each="subLists : ${@boostrapUtility.createSubLists(${fields})}"> 
    <div th:class="span+${field}" th:each="field : ${subLists}"> 
    <!-- whatever in here --> 
    </div> 
</div> 
+0

... 대단히 감사합니다 :) – faizi

+0

을 :

@Component public class BoostrapUtility { public List<List<Integer>> createSubLists(Collection<Integer> sizeList) { final List<List<Integer>> results = new ArrayList<>(); final int maxSize = 12; int rowSize = 0; List<Integer> subList = new ArrayList<>(); for (Integer size : sizeList) { if(rowSize + size <= maxSize) { subList.add(size); rowSize += size; } else { results.add(subList); subList = new ArrayList<>(); subList.add(size); rowSize = size; } } if(!subList.isEmpty()) { results.add(subList); } return results; } } 

그런 다음 Thymeleaf 코드는 같을 것이다 : 그 일을 끝낼 것입니다 코드의 원유 조각되어 다음 @ faizi 환영합니다! – geoand