Announcement Announcement Module
Collapse
No announcement yet.
Binding a map in velocity template Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding a map in velocity template

    Very novice question. My form backing object is a map. I am using velocity templates. I tried run through the debugger. I can see that my model in RequestContext has MyMap (has a bunch of key-value entries) in it. But, I am not sure how to bind the map in my .tmpl file. I tried

    Code:
     #springFormInput("MyMap['key1']" "class=fil maxlength=60")
    But the when my template is rendered I am seeing a exception.
    Code:
    Caused by: org.apache.velocity.exception.MethodInvocationException: Invocation of method 'getBindStatus' in  class org.springframework.web.servlet.support.RequestContext threw exception class java.lang.IllegalStateException : Neither Errors instance nor plain target object for bean name 'MyMap['key1']' available as request attribute
            at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:246)
            at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:182)
            at org.apache.velocity.runtime.parser.node.ASTReference.value(ASTReference.java:378)
            at org.apache.velocity.runtime.parser.node.ASTExpression.value(ASTExpression.java:51)
            at org.apache.velocity.runtime.parser.node.ASTSetDirective.render(ASTSetDirective.java:104)
            at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:55)
            at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:241)
            at org.apache.velocity.runtime.parser.node.ASTIfStatement.render(ASTIfStatement.java:89)
            at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:241)
            at org.apache.velocity.runtime.directive.VelocimacroProxy.render(VelocimacroProxy.java:172)
            at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:128)
            at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:241)
            at org.apache.velocity.runtime.directive.VelocimacroProxy.render(VelocimacroProxy.java:172)
            at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:128)
            at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:241)
            at org.apache.velocity.Template.merge(Template.java:256)
            at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:450)
           ...
    Am I missing something? I didn't find any useful velocity related examples. Would appreciate links to appropriate docs.

    Thanks in advance,
    Sre
    Last edited by sre786; Jan 9th, 2007, 05:18 PM.

  • #2
    How to bind a map or map entry in velocity templates?

    I am lost... would really appreciate some help. I tried
    Code:
     #springFormInput("MyMap.key1" "class=fil maxlength=60")
    and got different exception...

    How to bind a map or map entry in velocity templates?

    Need help please
    ~Sre

    Comment


    • #3
      Velocity - springBind with a map

      Take a look at this thread: #springBind with a map (velocity)

      It shows how to bind to a map using velocity. If you get this to work, I'd be interested to see your code.

      Comment


      • #4
        Thanks for the advice.... Sorry for the delayed response... got side tracked with other issues. I could get the Map bound as nested object as suggested in the thread you mentioned.
        Code:
        public class MapCommand implements Serializable {
            private HashMap<String,Object> _map = new HashMap<String,Object>();
        
            public HashMap getMap() {
                return _map;
            }
        
            public void setMap(HashMap<String,Object> map) {
                this._map = map;
            }
        
        }
        I can bind in my velocity templates using spring macros like this
        Code:
        #springFormInput("command.map["keyName"]" "maxlength=128")
        But I am surprised by exceptions thrown when using Map as my command object. I have seen many threads with Map binding issues.... but did anyone use Map as their formBackingObject and able to bind using bind paths like
        Code:
        command["key"]  -- ( This seems to look for a bean named "command[key]" instead of invoking command.get("key") )
        or
        command.key -- ( This seems to be invoking command.getKey() instead of invoking command.get("key") )
        1) Is spring binding same for all view technologies (I mean using JSP tags or spring macros wouldn't change binding logic... right??)
        2) Is there an example or has anyone used Map as command object..... would you be kind enough to share your binding code

        Thanks,
        Sre

        Comment

        Working...
        X