2

Not to be confused with this question!

So, I'm trying to pass a bean in a Thymeleaf fragment and to bind its properties to some fields.

I.e. Let's say I have a fragment called myFragment:

<div th:fragment="myFragment(myBean)"> <select class="selectpicker" th:field="${myBean.myProperty}"> <option th:each="myProperty : ${myBean.myProperties}" th:value="${myProperty}" th:text="${myProperty}"></option> </select> </div> 

And fragment is being called like this:

<div th:include="fragments/myFragment:: myFragment(myBean=${myBean})"> ... </div> 

And that works for some reason because the name of the variable and the name of the bean are the same (myBean). But if the name of the bean is different, i.e. like this:

<div th:include="fragments/myFragment:: myFragment(myBean=${someOtherBean})"> ... </div> 

I get an exception:

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

Everything works fine for other attributes like th:text, th:href... However, the exception occurs if I use th:field attribute (if I try to bind a property to some specific field). How to properly perform binding in this case?

1
  • But, your pattern works for me. Thanks. <div th:include="fragments/init :: pagination (page=${listPage})"></div> Commented Dec 17, 2015 at 20:10

1 Answer 1

1

Can you try to remove th:field="..." in select element and then add th:selected="${<your_logic_when_is_selected>}" in the <option> element.

One more thing. From my point of view you don't need the variable name declared in the <div th:include=" element.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.