How to make thymeleaf to ignore the null object?

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

How to make thymeleaf to ignore the null object?

shuan
I have a spring mvc project project with jsp as view. I need to use thymeleaf now. The old jsp project will auto ignore the manager object, if I don't put the manager object to the ModelMap.
<form class="form-horizontal" method="post" action="<c:url value=" /${action} " />">
<input type="hidden" name="id" value="${manager.id}">
....
<div class="form-group">
    <label class="col-sm-2 control-label">email</label>
    <div class="col-sm-8">
        <input type="text" name="email" value="${manager.email}" class="form-control1" placeholder="Email">
    </div>
</div>
<div class="form-group">
    <label class="col-sm-2 control-label">phone</label>
    <div class="col-sm-8">
        <input type="text" name="mobile" value="${manager.mobile}" class="form-control1" placeholder="phone number">
    </div>
</div>
...
<div class="form-group">
    <div class="col-sm-8 col-sm-offset-2 ">
        <button class="btn btn-success1 btn-block">sumbit</button>
    </div>
</div>

</form>

However Thymeleaf will give a error if I write as
     <input type="text" name="email" th:value="${manager.email}" class="form-control1" placeholder="Email">
I knew we can check the object as
<input type="text" name="mobile" th:value="${manager? manager.mobile : ''}" class="form-control1" placeholder="phone number">
Do I have to writte check each of them ?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to make thymeleaf to ignore the null object?

didier
This is not really an answer to your question, but you can simplify your statement from:
th:value="${manager? manager.mobile : ''}"
to
th:value="${manager?.mobile''}"

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to make thymeleaf to ignore the null object?

shuan
It solve my problem, thanks !
Loading...