You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Bruce Altner <ba...@hq.nasa.gov> on 2002/01/15 20:07:02 UTC

variable set by template, read in servlet

Although the templates usually pull data out of the context that were 
placed there by a servlet, the Velocity Developer's guide implies that a 
servlet can also use a value placed in the context by a template. However, 
I haven't gotten this to work yet. Not sure what I'm doing wrong:
In template FileLister.vm:
         #set($confirmedDelete="false")
         ...
         <form name="form1" action="$link.setAction("FileDeleter")">

In FileDeleter.java:

         context.put("confirmedDelete", (String) 
context.get("confirmedDelete"));
         data.setScreenTemplate("DeleteSuccess.vm") ;

In template DeleteSuccess.vm:
         Debug: confirmedDelete=${confirmedDelete}

   comes out as
         Debug: confirmedDelete=${confirmedDelete}

   not the expected

         Debug: confirmedDelete=false



This may appear like a strange thing to do but never mind...how do you get 
the servlet to know about a value set in the calling template? Do I need to 
put it into the request object via data.getRequest().setParameter(...) etc.?

Thanks,
Bruce

Re: variable set by template, read in servlet

Posted by Nick Bauman <ni...@cortexity.com>.
in interface HttpServletRequest setAttribute("foo", ...) won't do it for 
you?

> Although the templates usually pull data out of the context that were 
> placed there by a servlet, the Velocity Developer's guide implies that
> a  servlet can also use a value placed in the context by a template.
> However,  I haven't gotten this to work yet. Not sure what I'm doing
> wrong:
> In template FileLister.vm:
>          #set($confirmedDelete="false")
>          ...
>          <form name="form1" action="$link.setAction("FileDeleter")">
> 
> In FileDeleter.java:
> 
>          context.put("confirmedDelete", (String) 
> context.get("confirmedDelete"));
>          data.setScreenTemplate("DeleteSuccess.vm") ;
> 
> In template DeleteSuccess.vm:
>          Debug: confirmedDelete=${confirmedDelete}
> 
>    comes out as
>          Debug: confirmedDelete=${confirmedDelete}
> 
>    not the expected
> 
>          Debug: confirmedDelete=false
> 
> 
> 
> This may appear like a strange thing to do but never mind...how do you
> get  the servlet to know about a value set in the calling template? Do
> I need to  put it into the request object via
> data.getRequest().setParameter(...) etc.?
> 
> Thanks,
> Bruce


-- 
Nick Bauman 
Cortexity Development
Intellectual Process is more important than
Intellectual Property -- you'll see.
http://www.cortexity.com/


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: variable set by template, read in servlet

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 1/15/02 2:07 PM, "Bruce Altner" <ba...@hq.nasa.gov> wrote:

> Although the templates usually pull data out of the context that were
> placed there by a servlet, the Velocity Developer's guide implies that a
> servlet can also use a value placed in the context by a template. However,
> I haven't gotten this to work yet. Not sure what I'm doing wrong:
> In template FileLister.vm:
>        #set($confirmedDelete="false")
>        ...
>        <form name="form1" action="$link.setAction("FileDeleter")">
> 
> In FileDeleter.java:
> 
>        context.put("confirmedDelete", (String)
> context.get("confirmedDelete"));
>        data.setScreenTemplate("DeleteSuccess.vm") ;
> 
> In template DeleteSuccess.vm:
>        Debug: confirmedDelete=${confirmedDelete}
> 
>  comes out as
>        Debug: confirmedDelete=${confirmedDelete}
> 
>  not the expected
> 
>        Debug: confirmedDelete=false
> 
> 
> 
> This may appear like a strange thing to do but never mind...how do you get
> the servlet to know about a value set in the calling template? Do I need to
> put it into the request object via data.getRequest().setParameter(...) etc.?
> 
> Thanks,
> Bruce
> 


This looks vaguely turbine-ish.  I can answer the question generally though:

Anytime you do a 

  #set( $foo = "bar" )

In a template, you can look into the context after the merging and get the
new values for keys.

So

  context.put("foo", "boink");
  template.merge( context, .... )  // whatever

  String foo = (String) context.get("foo");

Will allow you to set the string foo to whatever the template set the value
of "foo" in the context to...

Is that a clear explanation?

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>