我不记得它叫什么,但我需要在 CFSet 语句内执行某种条件语句。我的意思是类似下面的内容
siteSettings = {
mailserversmtpport = resourceBean.getValue('mailsmtp'), // SMTP Port (If the method returns no len() then default to 25)
mailserverpopport = resourceBean.getValue('mailpop'), // POP port (If the method returns no len() then default to 110)
};
所以我正在为邮件服务器构建一个带有 smtp 和 pop 端口的结构。我有一个从 bean 获取值的方法调用。如果该值不存在,那么它将返回一个 0 长度的字符串。如果返回值没有长度而没有 do cfif 语句,是否可以(在 ColdFusion 8 中)将值设置为 25 和 110?
最佳答案
最简洁且我认为最好的方法是向您的 bean 添加两个方法:getMailSmtp() 和 getMailPop()。
将条件逻辑放在那里 - 以便该方法在未指定时返回默认值。
类似这样的事情:
<cffunction name="getMailSmtp" returntype="string" output="false">
<cfif len(getValue("mailsmtp"))>
<cfreturn getValue("mailsmtp") />
<cfelse>
<cfreturn 25 />
</cfif>
</cffunction>
或者,您可以更改 getValue() 方法以接受第二个参数 - 默认值。然后,如果该值不存在,它将返回默认值:
resourceBean.getValue("mailsmtp", 25)
我个人会选择第一种方法,因为这意味着每当您在应用程序中调用 getMailSmtp() 时,都会应用该逻辑。
您甚至可以组合这些方法,以便您的 getMailSmtp() 方法返回 getValue("mailsmtp", 25)
。
关于coldfusion - CFSet 中的条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6838659/