CFSet 中的条件语句
我不记得它叫什么,但我需要在 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?
I can't remember what it is called, but I need to do a sort of conditional statement inside of a CFSet statement. What I mean is something like the following
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)
};
So I am building a structure with the smtp and pop port for a mail server. I have a method call that gets a value from a bean. If that value doesn't exist then it is just going to return a 0 length string. Is it possible (in ColdFusion 8) to have the value be 25 and 110 if returned values have no length without do cfif statements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简洁的、在我看来最好的方法是向您的 bean 添加两个方法:getMailSmtp() 和 getMailPop()。
将条件逻辑放在那里 - 因此如果未指定,该方法将返回默认值。
像这样的事情:
或者,您可以更改 getValue() 方法以接受第二个参数 - 默认值。然后,如果该值不存在,它将返回默认值:
我个人会选择第一种方法,因为这意味着每当您在应用程序中调用 getMailSmtp() 时,都会应用该逻辑。
您甚至可以组合这些方法,以便您的 getMailSmtp() 方法返回
getValue("mailsmtp", 25)
。The neatest, and in my opinion best, way to do this would be to add two methods to your bean: getMailSmtp() and getMailPop().
Put the conditional logic in there - so the method returns your default value if it's not specified.
Something like this:
Alternatively, you could alter your getValue() method to accept a second argument - a default value. Then, if the value doesn't exist, it would return the default:
I'd personally go for the first method, as this means that any time you call getMailSmtp() in your application, the logic is applied.
You could even combine the methods, so your getMailSmtp() method returns
getValue("mailsmtp", 25)
.不是 iif 的忠实粉丝。您可能会想到三元运算符。
但这
只是CF9
Not a big fan of iif. Your probably thinking of the ternary operator.
which is
But it's CF9 only