Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Use a freemarker

Back-end management projects, which do not separate the front and back ends, usually use freemarker and Thymeleaf. Here are some ways to use Freemarker.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
Copy the code

The default suffix is.ftlh

spring:
  freemarker:
    suffix: .ftl
Copy the code

Go straight to code

<! DOCTYPEhtml>
<html>
<head lang="en">
  <meta charset="UTF-8"/>
  <title></title>
</head>
<body>FreeMarker template engine<br/>
<p>nullList:<#if nullList?? >true<#else>false</#if></p>
<p>nullList size:<#if nullList?? && nullList? size gt 0>true<#else>false</#if></p>

<p>emptyList:<#if emptyList?? >true<#else>false</#if></p>

<p>emptyList has_content:<#if emptyList? has_content>true<#else>false</#if></p>



<p>emptyList size:<#if emptyList? size gt 0>true<#else>false</#if></p>

<p>dataList: <#if dataList?? > <#list dataList as item> ${item! } </#list> </#if></p>

<p>nullStr: ${nullStr! }</p>
<p>nullStr ! : ${nullStr! }</p>
<p>nullStr Default: ${nullStr! 'Default'}</p>
<p>emptyStr: ${emptyStr! "'}</p>
<p>demoStr: ${demoStr! "'}</p>< # -<p>nullStr? length: ${nullStr? length}</p>-->
<p>emptyStr? length: ${emptyStr? length}</p>
<p>demoStr? length: ${demoStr? length}</p>

<p>nullStr ?? :<#if nullStr?? >true<#else>false</#if></p>
<p>emptyStr ?? :<#if emptyStr?? >true<#else>false</#if></p>
<p>nullStr has_content:<#if nullStr?? && nullStr? has_content>true<#else>false</#if></p>
<p>emptyStr has_content:<#if emptyStr? has_content>true<#else>false</#if></p>
<p>emptyStr? length :<#if emptyStr? length gt 0>${emptyStr}</#if></p>


<p>userName: ${userName! "'}</p>
<p>useName length: ${userName? length}</p>
<p>useName upper_case: ${userName? upper_case}</p>
<p>userName uncap_first: ${userName? uncap_first}</p>
<p>userName cap_first: ${userName? cap_first}</p>



</body>
</html>
Copy the code

The output

nullList:false nullList size:false emptyList:true emptyList has_content:false emptyList size:false dataList: 1 2 3 nullStr: nullStr ! : nullStr Default: Default emptyStr: demoStr: demoStr emptyStr? length: 0 demoStr? length: 7 nullStr ?? :false emptyStr ?? :true nullStr has_content:false emptyStr has_content:false emptyStr? length : userName: userName useName length: 8 useName upper_case: USERNAME userName uncap_first: userName userName cap_first: UserNameCopy the code

Conclusion:

Check that the string is not empty

<#if str?? && str? has_content> // TODO </#if> <#if str?? && str? length gt 0> // TODO </#if>Copy the code

Check that the list is not empty

<#if dataList?? && dataList? has_content> <#list dataList as item> // TODO ${item! } </#list> </#if> <#if dataList?? && dataList? size gt 0> <#list dataList as item> // TODO ${item! } </#list> </#if>Copy the code
  • ?? Determining whether the variable on the left is missing is the Equivalent of null in Java, or whether the variable is undefined.
  • ? Represents a method that calls an object that must exist. STR? length, list? size
  • ! To give the variable a default value, STR!” DefaultValue”