简体   繁体   中英

freemarker how print variable if it not null and not print if it is null?

I have this model

ModelAndView modelAndView = new ModelAndView("login");
        String msisdn = request.getParameter("msisdn");
        modelAndView.addObject("msisdn", msisdn); //may be NULL
        return modelAndView;

and page where

<#if msisdn??>
   <input type="text" class="form-control" placeholder="phone" value="${msisdn}">
 <#else>
    <input type="text" class="form-control" placeholder="phone">
 </#if>

If msisdn == null I want show placeholder="phone" but if msisdn not null I want show it.

It is work but I think it is bad practic. I not want copy all string and dublicate it in code. Can I write like this?

<input type="text" class="form-control" placeholder="phone" value="<#if msisdn != null>${msisdn}</#if>">

or

or something else in freemarker?

use ! to do this:

<input type="text" class="form-control" placeholder="phone" value="${msisdn!}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM