开发者

Switch over enum in freemarker

I thought that switching over an enum would be something very basic in FreeMarker, so that I could write something like:

<!-- Doesn't work -->
Dear
<#switch gender>
    <#case MALE>
        sir
 开发者_开发知识库       <#break>
    <#case FEMALE>
        madam
        <#break>
    <#default>
        sir/madam
<#/switch>

But it seems that accessing enums is an ugly and complicated matter. Is there a proper, clean way to switch on the values of an enum? Should I store it differently in my model?


You could just use the string value of the enums (at least with the default object wrapper, and basically with any other BeansWrapper variation too):

<#switch gender>
   <#case "MALE"> <#-- note the quotes -->
      sir
      <#break>
...

Or if depending on toString() is a problem (and it could be), use this:

<#switch gender.name()>
   <#case "MALE">
      sir
      <#break>
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜