Enum as key in hashmap to display map values and assign into a local variable

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Enum as key in hashmap to display map values and assign into a local variable

bhupadmini
Hi,

I have Map with Enum as key and Object as value pair. I have to use map values and list the objects in freemarker and assign into a local variable.

Say,

I have EnumType.java
public enum EnumType {
       
        INSURED1("1"), INSURED2("2"), SHARED("S");
       
        private final String code;
       
        EnumType(String aCode) {
                this.code=aCode;
        }
       
        public String getCode() {
                return code;
        }

}

User.java
public class User {
        private String userfname;
        private String userlname;
        private String userid;
        private String useraddress;
       
       
        public String getUserfname() {
                return userfname;
        }
        public void setUserfname(String userfname) {
                this.userfname = userfname;
        }
        public String getUserlname() {
                return userlname;
        }
        public void setUserlname(String userlname) {
                this.userlname = userlname;
        }
        public String getUserid() {
                return userid;
        }
        public void setUserid(String userid) {
                this.userid = userid;
        }
        public String getUseraddress() {
                return useraddress;
        }
        public void setUseraddress(String useraddress) {
                this.useraddress = useraddress;
        }
       
       
}

AppController.java
public class AppController {
....
public ModelAndView getView() {
..
ModelAndView model = new ModelAndView("/index");
               
                User user1=new User();
                user1.setUserfname("Muthuvel");
                user1.setUserlname("Selvakumar");
                user1.setUserid("M2345");
                user1.setUseraddress("Chennai");
               
                User user2=new User();
                user2.setUserfname("Selvakumar");
                user2.setUserlname("Vaiyapuri");
                user2.setUserid("M2346");
                user2.setUseraddress("Chennai");
               
                /*List<User> listUsers = new ArrayList<User>();
                listUsers.add(user1);
                listUsers.add(user2);*/
               
                Map<EnumType, User> enumMap = new HashMap<EnumType, User>();
                enumMap.put(EnumType.INSURED1, user1);
                enumMap.put(EnumType.INSURED2, user2);
               
                model.addObject("enumMap", enumMap);
..
}
..
}

index.ftl
<html>
<head><title>My freemarker page</title></head>
<body>

Welcome to my page

<#list enumMap?keys as key>
<#if (key=="INSURED1")>
<#assign enumUsers>${enumMap[key]}</#assign>
</#if>
</#list>
</body>
</html>

I am getting the following error


freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> enumMap[key]  [in template "index.ftl" at line 7, column 22]

Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??

The failing instruction (print stack trace for 1 more):
==> ${enumMap[key]}  [in template "index.ftl" at line 7, column 20]
        freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:98)
        freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:382)
        freemarker.core.Expression.evalAndCoerceToString(Expression.java:115)
        freemarker.core.DollarVariable.accept(DollarVariable.java:76)
        freemarker.core.Environment.visitByHiddingParent(Environment.java:286)
        freemarker.core.Environment.visitAndTransform(Environment.java:377)
        freemarker.core.BlockAssignment.accept(BlockAssignment.java:83)
        freemarker.core.Environment.visitByHiddingParent(Environment.java:286)
        freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:86)
        freemarker.core.Environment.visit(Environment.java:265)
        freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:181)
        freemarker.core.Environment.visitIteratorBlock(Environment.java:509)
        freemarker.core.IteratorBlock.accept(IteratorBlock.java:103)
        freemarker.core.Environment.visit(Environment.java:265)
        freemarker.core.MixedContent.accept(MixedContent.java:93)
        freemarker.core.Environment.visit(Environment.java:265)
        freemarker.core.Environment.process(Environment.java:243)
        freemarker.template.Template.process(Template.java:277)
        org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:366)
        org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:283)
        org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:233)
        org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167)
        org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
        org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
        org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
        org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
        org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
        org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


Please help me in resolving the issue.
Reply | Threaded
Open this post in threaded view
|

Re: Enum as key in hashmap to display map values and assign into a local variable

Daniel Dekany
Hi,

There's the still unfixed design legacy that FTL's "hash" type and
hence the [] operator only supports string keys (variable names, as it
was intended originally). Although FTL sees enums as strings, the net
result on the Java-level will be that the Map lookup will be with a
String key instead of the original Enum key, and hence it won't find
anything.

Possible workarounds for now:

- Use the enum *names* as keys instead of the enums themselves in enumMap

- Use an ObjectWrapper (a FreeMarker configuration setting) that supports
  myMap(myKey) additionally to of myMap[myKey]. The earlier supports
  any type of keys. You can just try if it works. If it doesn't,
  switching the ObjectWrapper under an existing app is quite a risk,
  so you are probably out of luck... But otherwise
  freemarker.ext.beans.BeansWrapper with simpleMapWrapper set to true
  could be used as a such object wrapper.

- Under some object wrappers myMap.get(myKey) is supported. Although such
  wrappers are horrendous because then the method names pollute the
  key space... so I don't recommend that, but when your app already uses
  a such wrapper anyway, you can use this trick.

- As the last chance, you can always write a TemplateModelEx that gets
  the wrapped map and does what it have to...

--
Thanks,
 Daniel Dekany


Wednesday, December 25, 2013, 7:53:02 PM, bhupadmini wrote:

> Hi,
>
> I have Map with Enum as key and Object as value pair. I have to use map
> values and list the objects in freemarker and assign into a local variable.
>
> Say,
>
> I have EnumType.java
> public enum EnumType {
>        
>         INSURED1("1"), INSURED2("2"), SHARED("S");
>        
>         private final String code;
>        
>         EnumType(String aCode) {
>                 this.code=aCode;
>         }
>        
>         public String getCode() {
>                 return code;
>         }
>
> }
>
> User.java
> public class User {
>         private String userfname;
>         private String userlname;
>         private String userid;
>         private String useraddress;
>        
>        
>         public String getUserfname() {
>                 return userfname;
>         }
>         public void setUserfname(String userfname) {
>                 this.userfname = userfname;
>         }
>         public String getUserlname() {
>                 return userlname;
>         }
>         public void setUserlname(String userlname) {
>                 this.userlname = userlname;
>         }
>         public String getUserid() {
>                 return userid;
>         }
>         public void setUserid(String userid) {
>                 this.userid = userid;
>         }
>         public String getUseraddress() {
>                 return useraddress;
>         }
>         public void setUseraddress(String useraddress) {
>                 this.useraddress = useraddress;
>         }
>        
>        
> }
>
> AppController.java
> public class AppController {
> ....
> public ModelAndView getView() {
> ..
> ModelAndView model = new ModelAndView("/index");
>                
>                 User user1=new User();
>                 user1.setUserfname("Muthuvel");
>                 user1.setUserlname("Selvakumar");
>                 user1.setUserid("M2345");
>                 user1.setUseraddress("Chennai");
>                
>                 User user2=new User();
>                 user2.setUserfname("Selvakumar");
>                 user2.setUserlname("Vaiyapuri");
>                 user2.setUserid("M2346");
>                 user2.setUseraddress("Chennai");
>                
>                 /*List<User> listUsers = new ArrayList<User>();
>                 listUsers.add(user1);
>                 listUsers.add(user2);*/
>                
>                 Map<EnumType, User> enumMap = new HashMap<EnumType, User>();
>                 enumMap.put(EnumType.INSURED1, user1);
>                 enumMap.put(EnumType.INSURED2, user2);
>                
>                 model.addObject("enumMap", enumMap);
> ..
> }
> ..
> }
>
> index.ftl
> <html>
> <head><title>My freemarker page</title></head>
> <body>
> Welcome to my page
>
> <#list enumMap?keys as key>
> <#if (key=="INSURED1")>
> <#assign enumUsers>${enumMap[key]}</#assign>
> </#if>
> </#list>
> </body>
> </html>
>
> I am getting the following error
>
>
> freemarker.core.InvalidReferenceException: The following has evaluated to
> null or missing:
==>> enumMap[key]  [in template "index.ftl" at line 7, column 22]
>
> Tip: If the failing expression is known to be legally null/missing, either
> specify a default value with myOptionalVar!myDefault, or use <#if
myOptionalVar??>>when-present<#else>when-missing</#if>. (These only cover the
> last step of the expression; to cover the whole expression, use
> parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
>
> The failing instruction (print stack trace for 1 more):
==>> ${enumMap[key]}  [in template "index.ftl" at line 7, column 20]

>
> freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:98)
>        
> freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:382)
>        
> freemarker.core.Expression.evalAndCoerceToString(Expression.java:115)
>         freemarker.core.DollarVariable.accept(DollarVariable.java:76)
>        
> freemarker.core.Environment.visitByHiddingParent(Environment.java:286)
>        
> freemarker.core.Environment.visitAndTransform(Environment.java:377)
>        
> freemarker.core.BlockAssignment.accept(BlockAssignment.java:83)
>        
> freemarker.core.Environment.visitByHiddingParent(Environment.java:286)
>        
> freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:86)
>         freemarker.core.Environment.visit(Environment.java:265)
>        
> freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:181)
>        
> freemarker.core.Environment.visitIteratorBlock(Environment.java:509)
>         freemarker.core.IteratorBlock.accept(IteratorBlock.java:103)
>         freemarker.core.Environment.visit(Environment.java:265)
>         freemarker.core.MixedContent.accept(MixedContent.java:93)
>         freemarker.core.Environment.visit(Environment.java:265)
>         freemarker.core.Environment.process(Environment.java:243)
>         freemarker.template.Template.process(Template.java:277)
>
> org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:366)
>
> org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:283)
>
> org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:233)
>
> org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167)
>
> org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
>
> org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
>
> org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
>
> org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
>
> org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
>
> org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
>         javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
>         javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
>
> org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
>
>
> Please help me in resolving the issue.
>
>
>
> --
> View this message in context:
> http://freemarker.624813.n4.nabble.com/Enum-as-key-in-hashmap-to-display-map-values-and-assign-into-a-local-variable-tp4654905.html
> Sent from the freemarker-user mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Rapidly troubleshoot problems before they affect your business. Most IT
> organizations don't have a clear picture of how application performance
> affects their revenue. With AppDynamics, you get 100% visibility into your
> Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
> http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>


------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user