freemarker->避免对象为空的操作

导言:freemarker使用后端对象通过标记${name}处理,name为后台传递的参数名,java一般利用如下代码传递参数 model.addAttribute("name",name);

1:但如果name为null,freemarker就会报错。这时候需要判断参数是不是空,可以通过如下语句:

<#if name??>
……
</#if>

2:当然也可以通过设置默认值${name!''}来避免对象为空的错误。如果name为空,就以默认值(“!”后的字符)显示。

3:对象user,name为user的属性的情况,user,name都有可能为空,那么可以写成${(user.name)!''}表示user或者name为null,都显示为空。判断为空(注意有个小括号,很多人会忘记)

<#if (user.name)??>
……
</#if>




<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:applicationContext*.xml classpath*:*Context.xml </param-value> </context-param> <context-param> <param-name>webAppRootKey</param-name> <param-value>rms.root</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>com.hg.filter.UrlAuthListen</listener-class> </listener> <listener> <listener-class>com.hg.filter.WebConfigListener</listener-class> </listener> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter </filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>XssFilter</filter-name> <filter-class>com.hg.aop.XssFilter</filter-class> </filter> <filter-mapping> <filter-name>XssFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- dwr 配置 --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <description>How quickly do scriptSessions timeout?</description> <param-name>scriptSessionTimeout</param-name> <param-value>1800000</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextAttributeListener</listener-class> </listener> <!-- dwr 配置 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>具体解释一下web.xml的配置
07-08
<section class="content"> <div class="container-fluid"> <div class="modal fade" id="uploadModal"> <div class="modal-dialog"> <form id="fileForm" enctype="multipart/form-data" method="post" action="${ctx}/cdp/uploadCdp"> <div class="modal-content"> <div class="modal-header"> <h6 class="modal-title">File Upload</h6> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <div class="card-body"> <table class="table table-hover table-striped formtable"> <tbody> <#-- <tr class="row col-md-12">--> <#-- <td class="col-md-5 flex align-center">--> <#-- Template Download:--> <#-- </td>--> <#-- <td class="col-md-7 align-center" id="gds01">--> <#-- <a href="${ctx}/static/templates/GdsNoSampleTemplate.xls"--> <#-- target="downloadFile">GDS NO Sample Template</a>--> <#-- <iframe style="display: none;" name="downloadFile"></iframe>--> <#-- </td>--> <#-- </tr>--> <tr class="row col-md-12"> <td class="col-md-5 flex align-center"> File input: </td> <td class="col-md-7 align-center"> <div class="input-group"> <input type="file" id="file" name="file" multiple=""/> <input type="hidden" name="etoUuidStr" value="${(tapeout.uuid)?if_exists}" id="mastUuidStr"> <input type="hidden" name="gdsUuid" value="" id="gdsUuid"> </div> </td> </tr> </tbody> </table> </div> </div> <div class="modal-footer col-md-12 pt-3"> <div class="col-lg-12 text-center"> <input type="button" class="btn btn-primary w-120" onclick="fileUpload()" value="Upload"/> <button type="button" class="btn btn-default w-120 ml-4" data-dismiss="modal" id="closeFileUpload">Close </button> </div> </div> </div> <!-- /.modal-content --> </form> </div> <!-- /.modal-dialog --> </div> </div> </section> 解释一下
最新发布
12-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值