Clarification needed about using a null model attribute with th:unless and th:if

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Clarification needed about using a null model attribute with th:unless and th:if

balteo
Hello,

I use Spring MVC and thymeleaf. I have the following controller method:
	@RequestMapping(method = RequestMethod.GET, produces = "text/html")
	public String curriculumForm(@ModelAttribute CurriculumInfo curriculumInfo, @CurrentMember Member member) {
		curriculumInfo.setCurriculum(member.getCurriculum());
		return "curriculum";
	}

Here is the dto which is a simple wrapper for a Curriculum object:
public class CurriculumInfo {

	@Valid
	private Curriculum curriculum;
}

Here is the relevant part of the thymeleaf template:

<a th:unless="${curriculumInfo.curriculum}" data-toggle="modal" href="#newCV" class="btn btn-primary btn-lg">new cv</a>
<a th:if="${curriculumInfo.curriculum}" data-toggle="modal" href="#editCV" class="btn btn-primary btn-lg">edit cv</a>

Now I am puzzled about the behavior of my app: when the current member's curriculum object is not null, it displays the edit link only (so far so good) but  when the curriculum object is null, it displays both the new and the edit links!!

Can anyone please help?

Regards,

J.
Reply | Threaded
Open this post in threaded view
|

Re: Clarification needed about using a null model attribute with th:unless and th:if

Zemi
Administrator
Your rationale about th:if and th:unless is correct, so you should look for the problem in another place.

Have you debug curriculumInfo.curriculum?

I've been puzzled sometimes with the use of th:if when in the same element you have another processor with higher precedence (which executes before th:if and modifies the element).

Regards,
    Zemi
Reply | Threaded
Open this post in threaded view
|

Re: Clarification needed about using a null model attribute with th:unless and th:if

balteo
You're right Zemi!

I had a form with a CurriculumInfo model attribute between the two links. Hence the problem.

Regards,

J.