2014-11-10 3 views
0

나는 모두 추상 기본 클래스를 공유하는 양식지지 오브젝트를 사용합니다. 이 기본 클래스의 속성 중 하나는 model이지만 내 도메인 모델의 엔터티는 무엇이든 나타낼 수 있습니다.양식에서 Thymeleaf 로컬 변수를 사용하는 올바른 방법

지금 내가하고 싶은 것이 즉 curriculum, advertisement 등, 따라서 내 다른 Thymeleaf 템플릿 model보다 advertisement와 더 좋은 읽을 것입니다 내 개체의 이름으로 model 속성을 이름을 변경하기 위해 th:with를 사용합니다. 여기

내가 시도 것입니다 :

<form th:object="${advertisementForm}" th:with="advertisement=*{model}" th:action="@{${postUrl}}" method="post" class="form-horizontal"> 

(modeladvertisementForm의 한 속성입니다) 다음과 같이 그때 내 새로 만든 advertisement 변수에 액세스하려고 할 때 :

<input type="hidden" th:field="${advertisement.id}"/> 

다음과 같이 표시됩니다.

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'advertisement' available as request attribute 

로컬 변수가 사용되지 않음을 나타냅니다.

답변

0

번째의 사용 : 괜찮으로하지만, 일에 대한 : 필드에 사용되는 HTML : 필드 대신 번째 사용하는

th:field="*{model.id}" -- here * can be read as a direct replacement of the form object defined with th:object 

th:field="${advertisementForm.model.id}" --- if you are $ you need to define the complete path 

가능한 솔루션 을 사용할 : 필드 번째에 대한 잘못된

수있는 방법입니다 id & 이름. 번째 사용하여 설정 값 : http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#inputs

여기에 언급 한 바와 같이 값

<input class="form-control" 
      type="text" 
      th:value="${client.name}" id="clientName" name="clientName" /> 

하지만 강력하기 때문에하지 제안하지 것이다

관련 문제