<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 119  文章 - 3173  trackbacks - 0
    <2007年5月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    交友莫獨酒,茅臺西鳳游。
    口干古井貢,心徜洋河流。
    稱多情杜康,趟無量雙溝。
    贊中華巍巍,無此不銷愁。

    常用鏈接

    留言簿(68)

    隨筆分類(136)

    隨筆檔案(122)

    最新隨筆

    搜索

    •  

    積分與排名

    • 積分 - 525135
    • 排名 - 93

    最新評論

    Welcome ${user!}!
    Welcome ${user!'your name'}!

    如果user找不到值,會輸出
    Welcome !
    Welcome your name!



    官方說法:
    3.??Why is FreeMarker so picky about null-s and missing variables, and what to do with it?
    ?

    To recapitulate what's this entry is about: FreeMarker by default treats an attempt to access a non-existent variable or a null value (this two is the same for FreeMarker) as error, which aborts the template execution.

    First of all, you should understand the reason of being picky. Most scripting languages and template languages are rather forgiving with missing variables (and with null-s), and they usually treat them as empty string and/or 0 and/or logical false. This behavior has several problems:

    • It potentially hiders accidental mistakes, like a typo in a variable name, or when the template author refers to a variable that the programmer doesn't put into the data model, or for which the programmer uses a different name. Human is prone to do such accidental mistakes, computers are not, so missing this opportunity that the template engine can show these errors is a bad business. Even if you very carefully check the output of the templates during development, it is easy to look over mistakes like <#if hasDetp>print dept here...</#if>, which would then silently never print the dept of the visitor, since you have mistyped the variable name (it should be hasDept). Also think about maintenance, when you later modify your application... most probably you will not re-check templates that carefully each time.

    • Makes dangerous assumptions. The script language or template engine knows nothing about the application domain, so when it decides the value of something it don't know to be 0/false, it is a quite irresponsible and arbitrary thing. Just because it is not know what's your current bank account balance, can we just say it is $0 (and how easy it is to accidentally write Balance: ${balanace})? Just because it is not known if a patient has penicillin allergy, we can just say he/she doesn't have (and how easy it is to accidentally write <#if hasPenicilinAllergy>Warning...<#else>Allow...</#if>; there is a typo in this, if you didn't see)? Just consider the implications of such mistakes for a moment. They can be quite severe and troubling. Showing an error page is often better than showing incorrect information that formally looks good.

    Being not picky is mostly sweeping under the carpet in this case (not facing with the problems), which of course most people feels more convenient, but still... we believe that in most cases being strict will save your time and increase your software quality in the long run.

    On the other hand, we recognize that there are cases where you don't want FreeMarker to be that picky with good reason, and there is solution for them:

    • It's often normal that your data model contains null-s or have optional variables. In such cases use these operators. If you use them too often, try to rethink your data model, because depending on them too much is not just results in awkward verbose templates, but increases the probability of hiding errors and printing arbitrary incorrect output (for the reasons described earlier).

    • On a production server you may rather want to show an incomplete/damaged page than an error page. In this case you can use other error handler than the default. Error handlers can be made that rather skip the problematic part than abort the whole page rendering. Note, however, that although the error handlers don't give arbitrary default values to variables, for pages that show critical information it's maybe still better to show an error page. (Another feature you may interested in: the attempt/recover directives)

    15.??What about null and the FreeMarker template language?
    ?

    The FreeMarker template language doesn't know the Java language null at all. It doesn't have null keyword, and it can't test if something is null or not. When it technically faces with a null, it treats it exactly as a missing variable. For example, both if x is null in the data model and if it's not present at all, ${x!'missing'} will print ``missing'', you can't tell the difference. Also, if for example you want to test if a Java method has returned null, just write something like <#if foo.bar()??>.

    You may interested in the rationale behind this. From the viewpoint of the presentation layer a null and non-existent thing is almost always the same. The difference between this two is usually just a technical detail, which is rather the result of implementation details than of the application logic. That you can't compare something to null (unlike in Java); it doesn't make sense to compare something with null in a template, since the template language doesn't do identity comparison (like the Java == operator when you compare two objects) but the more common sense value comparison (like Java's Object.equals(Object); that doesn't work with null either). And how could FreeMarker tell if something concrete equals with something that is missing and thus unknown? Or if two missing (unknown) things are equal? Of course these questions can't be answered.

    There is at least one problem with this null-unaware approach. When you call a Java method from a template, you may want to pass a null value as argument (since the method was designed to be used in Java language, where the concept of null is known). In this case you can exploit a bug of FreeMarker (that we will not fix until we provide a correct solution for passing null values to a method): if you specify a missing variable as the argument, then it will not cause an error, but a null will be passed to the method instead. Like foo.bar(nullArg) will call the bar method with null as argument, assuming that there is no varaible exists with ``nullArg'' name.

    posted on 2007-05-09 10:26 交口稱贊 閱讀(11760) 評論(5)  編輯  收藏 所屬分類: freemarker

    FeedBack:
    # re: freemarker的空值和默認值 2007-05-09 10:29 交口稱贊
    Welcome ${user!}!
    Welcome ${user!'your name'}!

    但是這兩種寫法在freemarker editor中都提示錯誤。。。。。。。。  回復  更多評論
      
    # re: freemarker的空值和默認值 2007-05-09 14:32 mmwy
    ${user?if_exists}

    ${user?default('your name')}
      回復  更多評論
      
    # re: freemarker的空值和默認值 2007-05-09 15:41 交口稱贊
    呵呵
    多謝樓上

    !比較方便

    樓上的寫法比較規范,大家各取所需吧



      回復  更多評論
      
    # re: freemarker的空值和默認值 2007-05-09 15:42 交口稱贊
    還有誰知道怎么在ftl模板里面直接用資源文件?  回復  更多評論
      
    # re: freemarker的空值和默認值 2014-09-13 12:34 zuidaima
    freemarker demo教程源代碼下載地址:http://zuidaima.com/share/kfreemarker-p1-s1.htm  回復  更多評論
      
    主站蜘蛛池模板: 无码国产精品一区二区免费模式 | 亚洲?V无码乱码国产精品| 亚洲综合图片小说区热久久| a级精品九九九大片免费看| 亚洲一区精品伊人久久伊人| 国产成人不卡亚洲精品91| 日本免费人成黄页在线观看视频| 亚洲一级大黄大色毛片| 成年在线观看免费人视频草莓| 亚洲乱码一区二区三区国产精品| 午夜性色一区二区三区免费不卡视频| 亚洲制服丝袜一区二区三区| 无码国产精品一区二区免费式直播| 亚洲中文字幕人成乱码| 毛片免费全部播放一级| 亚洲人成欧美中文字幕| 免费人成视频x8x8入口| XXX2高清在线观看免费视频| 久久精品国产精品亚洲色婷婷| 成年黄网站色大免费全看| 亚洲av永久无码| 亚洲国产精品无码专区在线观看| 99精品视频在线视频免费观看| 亚洲人成电影网站久久| 亚洲国产日韩在线观频| 久久国产免费一区| 亚洲av日韩精品久久久久久a| 亚洲综合色自拍一区| 无码国产精品一区二区免费虚拟VR | 精品多毛少妇人妻AV免费久久| 911精品国产亚洲日本美国韩国| 在线播放免费播放av片| 香蕉视频在线免费看| 亚洲色丰满少妇高潮18p| 亚洲色欲色欲www在线丝 | 亚洲精品一级无码鲁丝片| 97无码免费人妻超级碰碰碰碰| 两个人看的www视频免费完整版| 中文字幕无码亚洲欧洲日韩| 亚洲人成网站在线观看播放| 在线a毛片免费视频观看|