Using Thymeleaf when the value is null - html

I have some values in my database which can be null if they have not already been entered.
But when I use Thymeleaf in my html, it gives an error when parsing null values.
Is there any way to handle this?

The shortest way is using '?' operator. If you have User entity with embedded Address entity in order to access fields of Address entity and print them if address is not null, otherwise here will be an empty column:
<td th:text="${user?.address?.city}"></td>

Sure there is. You can for example use the conditional expressions. For example:
<span th:text="${someObject.someProperty != null} ? ${someObject.someProperty} : 'null value!'">someValue</span>
You can even omit the "else" expression:
<span th:text="${someObject.someProperty != null} ? ${someObject.someProperty}">someValue</span>
You can also take a look at the Elvis operator to display default values like this:-
<span th:text="${someObject.someProperty} ?: 'default value'">someValue</span>

This can also be handled using the elvis operator ?: which will add a default value when the field is null:
<span th:text="${object.property} ?: 'default value'"></span>

You can use 'th:if' together with 'th:text'
<span th:if="${someObject.someProperty != null}" th:text="${someObject.someProperty}">someValue</span>

Also worth to look at documentation for #objects build-in helper:
https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#objects
There is useful: ${#objects.nullSafe(obj, default)}

You've done twice the checking when you create
${someObject.someProperty != null} ? ${someObject.someProperty}
You should do it clean and simple as below.
<td th:text="${someObject.someProperty} ? ${someObject.someProperty} : 'null value!'"></td>

<p data-th-text ="${#strings.defaultString(yourNullable,'defaultValueIfYourValueIsNull')}"></p>

you can use this solution it is working for me
<span th:text="${#objects.nullSafe(doctor?.cabinet?.name,'')}"></span>

I use
<div th:text ="${variable != null} ? (${variable != ''} ? ${variable} : 'empty string message') : 'null message' "></div>

The shortest way! it's working for me,
Where NA is my default value.
<td th:text="${ins.eValue!=null}? ${ins.eValue}:'NA'" />

Related

Angular display conditional text in html including conditional data

I know in Angular2+ we can create condition in html like this:
<p>{{ dynamicData.value? dynamicData.value : dynamicData.default}}</p>
Also we can do something like this:
<p>{{ dynamicData.value? 'text 1' : 'text 2'}}</p>
But I would like to combine this two solutions and I have no idea how to do this. In general I would like to do something like this:
<p>{{ dynamicData.value? 'Dynamic data value is equal: {{dynamicData.value}}' : 'no dynamic data'</p>
Have you any idea how to handle this text interpolation?
You do not need to use interpolation within interpolation, you already have access to variables and can try something as below:
<p>{{ dynamicData?.value ? ('Dynamic data value is equal: ' + dynamicData?.value) : 'no dynamic data'}}</p>
<p *ngIf = "dynamicData.value"> 'Dynamic data value is equal' {{dynamicData.value}} </p>
<p *ngIf = "!dynamicData.value"> 'no dynamic data' </p>
You can use variable in your .ts file or you can use + interpolation 'Dynamic data value is equal: ' + dynamicData.default.
But I like more answer posted by FedMice ;)
I hope this works.
<p>{{ dynamicData.value ? (`Dynamic data value is equal: ${dynamicData?.value}`) : `no dynamic data`}}</p>

Add a class by checking a condition in angular 6

I need to add a class 'test1' if the values in 'emp_code' and 'emp' is same.
I had tried with the code,
class="{{ emp_empcode == emp ? 'test1' : 'test2' }}"
I had tried with this and this
Any other way? Thanks a lot in advance.
[class.test1]="emp_code == emp"
should definitively work. You can also take a look at https://toddmotto.com/ng-class-angular-classes for more varieties

JSP variables concatenated as string at output by checking the null fields

I want to print the address in the following order and I was able to do that. My problem is for some users some address fields are null then it is printing like this (1804 E Broadway, , 223344) and also I want divide the address in to lines.
<td>${addr1 }, ${addr2 }, ${postalCode }</td>
You can achieve your requirement is 3 ways:
1. JSP EL
<!-- here I am using adjacent EL expression because string concat will not work here and if we put EL expression in new line one space is adding-->
<td>${addr1 }${empty addr1? '': ','}
${addr2 }${empty addr2? '': ','}
${postalCode }</td>
2. JSTL
<c:if test="${not empty addr1}">
${addr1},
</c:if>
3. Using java code in JSP
I will not recommend this method because writing java code in JSP is not encouraged.
<%= (addr1 != null && addr1 !="") ? addr1 +",": "" %>
You can use a ternary operator in your JSP like this:
<%= (field != null && field !="") ? field +",": "" %>

how to use [ngClass] for multiple class conditions Angular4

currently my div looks like this:
<div class="class-a" [ngClass]="{'class-b': !person.something}">
now I want to have another condition...
so now I want this div to be of class-a If something class-b If something else class-c
how should I do this?
im using angular 4.
thanks!
Add it like properties to an object literal:
[ngClass]="{'class-b': !person.something, 'other-condition': isOther }"
Another option is to return a string from the component if you think you need more complex logic, or know there will only be one. This might be more testable.
Whatever string you return will be rendered as a class(es)
[ngClass]="renderClass()"
renderClass() {
switch(this.user.theme){
case "dark":
return "dark-theme"
case "light":
return "light-theme"
}
}
The better way for use this Syntax ngStyle Because,
it's Not Completed Answer.
If you want to toggle some classes like text-info Or text-danger for <i> tag (
some exp ? 'text-info' : 'text-danger'
).
The best answer is array not object.
[ngClass] = "[some exp ? 'text-info' : 'text-danger', ...]"
GoodLuck

Why does Eval statement show the wrong image

SQL (Active column is of type bit):
id Question Active
1 Weather today 1
ASP.net Eval:
<img src='<%# Eval("Active") == "1" ? "images/active.png" : "images/inactive.png" %>' />
HTML:
<img src="images/inactive.png">
Why is the inactive.png image showing and not the active.
Bit fields correspond to boolean. Also you need to do a type conversion to ensure right comparison is done, as Eval outputs just object. So:
(bool)Eval("Active") == true
You could try to cast the result:
((int)Eval("Active")) == 1 ? [...]
or as mentioned in the comments to a bool:
((bool)Eval("Active")) == true ? [...]