You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Paul Goepfert <pa...@gmail.com> on 2005/06/24 01:40:55 UTC

Can't seem to get information to print to a web page.

I have been trying to get the data that I collected in my webapp to
dispaly on this one page that I am trying to render.  I can't seem to
figure it out.  This is what I do.  In my ResultsAction class I make a
call to a method that I created called createContext(InfoBean []
array, DB data).  In this method I populate a variable "context" with
the values in my array and then return the StringWriter to my
ResultsAction Class.  I was hopping to replace the tewmplate that I
have for this particular webpage with what I returned to the Action
class.  I haven't been able to do that yet.

Below you will find my createContext method, part of my resultsAction
class as well as my .vm template.  Please if anyone has an idea of how
I can get the output to appear on the web page let me know.

         #*All start and end tags of .vm file have been removed to
save space except for the form tag*#
            <h1>$text.get("resultsHeading")</h1>
            <form name="resultsForm" method="post"
action="$link.setAction("Results")">
                <input type="hidden" name="action" value="done">
                 #if($Array) 
                    <table width="380" border="0" cellspacing="0"
cellspadding="0">
                        <tr>
                            <th width="107" scope="col">
                            <div align="center">$text.get("last")</div>
                            </th>
                            <th width="108" scope="col">
                                <div align="center">$text.get("first")</div>
                            </th>
                            <th width="165" scope="col">
                                <div align="center">$text.get("Address")</div>
                            </th>
                        </tr>
                        #foreach($person in $Array)
                            <tr>
                                <td>$person.last</td>
                                <td>$person.first</td>
                                <td>
                                    <table width="342" border="0"
cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td width="386">$person.street</td>
                                        </tr>
                                        <tr>
                                            <td>$person.city,
$person.state $person.zip</td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            #end
                    </table>
                    <input type="submit" name="Submit" value="
$text.get("done") " onclick="resultsForm.action.value='done'" >
                   
                    #else
                        <p>Sorry but there is no data in the database
to sort.  Please
                            enter information in the databse </p>
                    <input type="submit" name="Submit2" value="
$text.get("enter")" onclick="resultsForm.action.value='enterInfo'">
                    #end
         </form>

//All try/Catch blocks have been removed to save space.  Located in
the DB class in the logic packagae.

    public StringWriter createContext(InfoBean [] array, DB data)
    {
            Velocity.init();
        
        Template template = null;

            template = Velocity.getTemplate("/sortResults.vm");
        VelocityContext context = new VelocityContext();
        
        data.removeInsert(array);
        List <Map<String, String>> vecInfo = data.getInformation();
        context.put("Array",  vecInfo);
        
        StringWriter writer = new StringWriter ();
            template.merge(context, writer);
        
        return writer;
    }

//data is an object reference to DB class.
else if(action.equals("default"))
            {
                InfoBean sorted [] = new InfoBean
[data.getInformation().size()];
                sorted = data.createSortedArray("ascending", "first", data);
                data.removeInsert(sorted);
                StringWriter writer = data.createContext(sorted, data);
                    //response.getWriter().println(template.toString());
                
                try
                {
                     response.setContentType("text/html");
                    PrintWriter out = response.getWriter();
                    out.println(writer.toString());
                }
                catch(IOException e)
                {e.printStackTrace();}
                             
                return (mapping.findForward("default"));
            }

-Paul

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Can't seem to get information to print to a web page.

Posted by Paul Goepfert <pa...@gmail.com>.
Thank you.  I finally figured out that all I had to do was set an
attribute into the request and use a #set to put the values into the
template.  I had origionally thought that I needed to have the
velocity programming(Velocity.init() etc.).  Once I realized I didn't
need it the problem went away and the solution just came to me.

Again thanks

-Paul

On 6/26/05, Nathan Bubna <nb...@gmail.com> wrote:
> the VelocityTools project
> (http://jakarta.apache.org/velocity/tools/index.html) is designed to
> make web publishing with Velocity (and Struts, if you want) much
> easier.  i've never heard of the VMServlet, but the VelocityServlet
> that ships with the core velocity library is deprecated in favor of
> the VelocityViewServlet in the VelocityTools project.  the
> VelocityTools project also ships with several example applications for
> using Velocity (and Struts, optionally) to create dynamic web
> applications.  use the examples as your starting point.  play with
> them and/or copy them.  they should show what you need to know.  if
> you don't understand how they work, ask us and we'll try to fill you
> in.
> 
> On 6/25/05, Paul Goepfert <pa...@gmail.com> wrote:
> > I have been searching online all day for an example of how to display
> > the dynamic content on my template.  I haven't found any site that
> > would help.  I did find one site that tolds me to use a VMServlet,
> > however I have learned from a previous posting that the VMServlet
> > class was depreciated.
> >
> > How do I output the writer object to the web page.   If I were doing
> > this for a JSP I would create a PrintWriter object out and use the
> > println() method to output the template.  Since I'm not I am a bit
> > confused about how to do this from within my code.  I can't do it in
> > the method where I create the context because there is no access to
> > the response or request variables.  I can't do it in the Action class
> > because its just a forwarding class to other pages.  So if anyone can
> > tell me where and how to put it into my program that would be great.
> > My code for everything for this feature of my program is in the quoted
> > text.
> >
> > thanks,
> >
> > -Paul
> >
> > On 6/23/05, Paul Goepfert <pa...@gmail.com> wrote:
> > > I have been trying to get the data that I collected in my webapp to
> > > dispaly on this one page that I am trying to render.  I can't seem to
> > > figure it out.  This is what I do.  In my ResultsAction class I make a
> > > call to a method that I created called createContext(InfoBean []
> > > array, DB data).  In this method I populate a variable "context" with
> > > the values in my array and then return the StringWriter to my
> > > ResultsAction Class.  I was hopping to replace the tewmplate that I
> > > have for this particular webpage with what I returned to the Action
> > > class.  I haven't been able to do that yet.
> > >
> > > Below you will find my createContext method, part of my resultsAction
> > > class as well as my .vm template.  Please if anyone has an idea of how
> > > I can get the output to appear on the web page let me know.
> > >
> > >          #*All start and end tags of .vm file have been removed to
> > > save space except for the form tag*#
> > >             <h1>$text.get("resultsHeading")</h1>
> > >             <form name="resultsForm" method="post"
> > > action="$link.setAction("Results")">
> > >                 <input type="hidden" name="action" value="done">
> > >                  #if($Array)
> > >                     <table width="380" border="0" cellspacing="0"
> > > cellspadding="0">
> > >                         <tr>
> > >                             <th width="107" scope="col">
> > >                             <div align="center">$text.get("last")</div>
> > >                             </th>
> > >                             <th width="108" scope="col">
> > >                                 <div align="center">$text.get("first")</div>
> > >                             </th>
> > >                             <th width="165" scope="col">
> > >                                 <div align="center">$text.get("Address")</div>
> > >                             </th>
> > >                         </tr>
> > >                         #foreach($person in $Array)
> > >                             <tr>
> > >                                 <td>$person.last</td>
> > >                                 <td>$person.first</td>
> > >                                 <td>
> > >                                     <table width="342" border="0"
> > > cellspacing="0" cellpadding="0">
> > >                                         <tr>
> > >                                             <td width="386">$person.street</td>
> > >                                         </tr>
> > >                                         <tr>
> > >                                             <td>$person.city,
> > > $person.state $person.zip</td>
> > >                                         </tr>
> > >                                     </table>
> > >                                 </td>
> > >                             </tr>
> > >                             #end
> > >                     </table>
> > >                     <input type="submit" name="Submit" value="
> > > $text.get("done") " onclick="resultsForm.action.value='done'" >
> > >
> > >                     #else
> > >                         <p>Sorry but there is no data in the database
> > > to sort.  Please
> > >                             enter information in the databse </p>
> > >                     <input type="submit" name="Submit2" value="
> > > $text.get("enter")" onclick="resultsForm.action.value='enterInfo'">
> > >                     #end
> > >          </form>
> > >
> > > //All try/Catch blocks have been removed to save space.  Located in
> > > the DB class in the logic packagae.
> > >
> > >     public StringWriter createContext(InfoBean [] array, DB data)
> > >     {
> > >             Velocity.init();
> > >
> > >         Template template = null;
> > >
> > >             template = Velocity.getTemplate("/sortResults.vm");
> > >         VelocityContext context = new VelocityContext();
> > >
> > >         data.removeInsert(array);
> > >         List <Map<String, String>> vecInfo = data.getInformation();
> > >         context.put("Array",  vecInfo);
> > >
> > >         StringWriter writer = new StringWriter ();
> > >             template.merge(context, writer);
> > >
> > >         return writer;
> > >     }
> > >
> > > //data is an object reference to DB class.
> > > else if(action.equals("default"))
> > >             {
> > >                 InfoBean sorted [] = new InfoBean
> > > [data.getInformation().size()];
> > >                 sorted = data.createSortedArray("ascending", "first", data);
> > >                 data.removeInsert(sorted);
> > >                 StringWriter writer = data.createContext(sorted, data);
> > >                     //response.getWriter().println(template.toString());
> > >
> > >                 try
> > >                 {
> > >                      response.setContentType("text/html");
> > >                     PrintWriter out = response.getWriter();
> > >                     out.println(writer.toString());
> > >                 }
> > >                 catch(IOException e)
> > >                 {e.printStackTrace();}
> > >
> > >                 return (mapping.findForward("default"));
> > >             }
> > >
> > > -Paul
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >
> >
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Can't seem to get information to print to a web page.

Posted by Nathan Bubna <nb...@gmail.com>.
the VelocityTools project
(http://jakarta.apache.org/velocity/tools/index.html) is designed to
make web publishing with Velocity (and Struts, if you want) much
easier.  i've never heard of the VMServlet, but the VelocityServlet
that ships with the core velocity library is deprecated in favor of
the VelocityViewServlet in the VelocityTools project.  the
VelocityTools project also ships with several example applications for
using Velocity (and Struts, optionally) to create dynamic web
applications.  use the examples as your starting point.  play with
them and/or copy them.  they should show what you need to know.  if
you don't understand how they work, ask us and we'll try to fill you
in.

On 6/25/05, Paul Goepfert <pa...@gmail.com> wrote:
> I have been searching online all day for an example of how to display
> the dynamic content on my template.  I haven't found any site that
> would help.  I did find one site that tolds me to use a VMServlet,
> however I have learned from a previous posting that the VMServlet
> class was depreciated.
> 
> How do I output the writer object to the web page.   If I were doing
> this for a JSP I would create a PrintWriter object out and use the
> println() method to output the template.  Since I'm not I am a bit
> confused about how to do this from within my code.  I can't do it in
> the method where I create the context because there is no access to
> the response or request variables.  I can't do it in the Action class
> because its just a forwarding class to other pages.  So if anyone can
> tell me where and how to put it into my program that would be great.
> My code for everything for this feature of my program is in the quoted
> text.
> 
> thanks,
> 
> -Paul
> 
> On 6/23/05, Paul Goepfert <pa...@gmail.com> wrote:
> > I have been trying to get the data that I collected in my webapp to
> > dispaly on this one page that I am trying to render.  I can't seem to
> > figure it out.  This is what I do.  In my ResultsAction class I make a
> > call to a method that I created called createContext(InfoBean []
> > array, DB data).  In this method I populate a variable "context" with
> > the values in my array and then return the StringWriter to my
> > ResultsAction Class.  I was hopping to replace the tewmplate that I
> > have for this particular webpage with what I returned to the Action
> > class.  I haven't been able to do that yet.
> >
> > Below you will find my createContext method, part of my resultsAction
> > class as well as my .vm template.  Please if anyone has an idea of how
> > I can get the output to appear on the web page let me know.
> >
> >          #*All start and end tags of .vm file have been removed to
> > save space except for the form tag*#
> >             <h1>$text.get("resultsHeading")</h1>
> >             <form name="resultsForm" method="post"
> > action="$link.setAction("Results")">
> >                 <input type="hidden" name="action" value="done">
> >                  #if($Array)
> >                     <table width="380" border="0" cellspacing="0"
> > cellspadding="0">
> >                         <tr>
> >                             <th width="107" scope="col">
> >                             <div align="center">$text.get("last")</div>
> >                             </th>
> >                             <th width="108" scope="col">
> >                                 <div align="center">$text.get("first")</div>
> >                             </th>
> >                             <th width="165" scope="col">
> >                                 <div align="center">$text.get("Address")</div>
> >                             </th>
> >                         </tr>
> >                         #foreach($person in $Array)
> >                             <tr>
> >                                 <td>$person.last</td>
> >                                 <td>$person.first</td>
> >                                 <td>
> >                                     <table width="342" border="0"
> > cellspacing="0" cellpadding="0">
> >                                         <tr>
> >                                             <td width="386">$person.street</td>
> >                                         </tr>
> >                                         <tr>
> >                                             <td>$person.city,
> > $person.state $person.zip</td>
> >                                         </tr>
> >                                     </table>
> >                                 </td>
> >                             </tr>
> >                             #end
> >                     </table>
> >                     <input type="submit" name="Submit" value="
> > $text.get("done") " onclick="resultsForm.action.value='done'" >
> >
> >                     #else
> >                         <p>Sorry but there is no data in the database
> > to sort.  Please
> >                             enter information in the databse </p>
> >                     <input type="submit" name="Submit2" value="
> > $text.get("enter")" onclick="resultsForm.action.value='enterInfo'">
> >                     #end
> >          </form>
> >
> > //All try/Catch blocks have been removed to save space.  Located in
> > the DB class in the logic packagae.
> >
> >     public StringWriter createContext(InfoBean [] array, DB data)
> >     {
> >             Velocity.init();
> >
> >         Template template = null;
> >
> >             template = Velocity.getTemplate("/sortResults.vm");
> >         VelocityContext context = new VelocityContext();
> >
> >         data.removeInsert(array);
> >         List <Map<String, String>> vecInfo = data.getInformation();
> >         context.put("Array",  vecInfo);
> >
> >         StringWriter writer = new StringWriter ();
> >             template.merge(context, writer);
> >
> >         return writer;
> >     }
> >
> > //data is an object reference to DB class.
> > else if(action.equals("default"))
> >             {
> >                 InfoBean sorted [] = new InfoBean
> > [data.getInformation().size()];
> >                 sorted = data.createSortedArray("ascending", "first", data);
> >                 data.removeInsert(sorted);
> >                 StringWriter writer = data.createContext(sorted, data);
> >                     //response.getWriter().println(template.toString());
> >
> >                 try
> >                 {
> >                      response.setContentType("text/html");
> >                     PrintWriter out = response.getWriter();
> >                     out.println(writer.toString());
> >                 }
> >                 catch(IOException e)
> >                 {e.printStackTrace();}
> >
> >                 return (mapping.findForward("default"));
> >             }
> >
> > -Paul
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Can't seem to get information to print to a web page.

Posted by Paul Goepfert <pa...@gmail.com>.
I have been searching online all day for an example of how to display
the dynamic content on my template.  I haven't found any site that
would help.  I did find one site that tolds me to use a VMServlet,
however I have learned from a previous posting that the VMServlet
class was depreciated.

How do I output the writer object to the web page.   If I were doing
this for a JSP I would create a PrintWriter object out and use the
println() method to output the template.  Since I'm not I am a bit
confused about how to do this from within my code.  I can't do it in
the method where I create the context because there is no access to
the response or request variables.  I can't do it in the Action class
because its just a forwarding class to other pages.  So if anyone can
tell me where and how to put it into my program that would be great.
My code for everything for this feature of my program is in the quoted
text.

thanks,

-Paul   

On 6/23/05, Paul Goepfert <pa...@gmail.com> wrote:
> I have been trying to get the data that I collected in my webapp to
> dispaly on this one page that I am trying to render.  I can't seem to
> figure it out.  This is what I do.  In my ResultsAction class I make a
> call to a method that I created called createContext(InfoBean []
> array, DB data).  In this method I populate a variable "context" with
> the values in my array and then return the StringWriter to my
> ResultsAction Class.  I was hopping to replace the tewmplate that I
> have for this particular webpage with what I returned to the Action
> class.  I haven't been able to do that yet.
> 
> Below you will find my createContext method, part of my resultsAction
> class as well as my .vm template.  Please if anyone has an idea of how
> I can get the output to appear on the web page let me know.
> 
>          #*All start and end tags of .vm file have been removed to
> save space except for the form tag*#
>             <h1>$text.get("resultsHeading")</h1>
>             <form name="resultsForm" method="post"
> action="$link.setAction("Results")">
>                 <input type="hidden" name="action" value="done">
>                  #if($Array)
>                     <table width="380" border="0" cellspacing="0"
> cellspadding="0">
>                         <tr>
>                             <th width="107" scope="col">
>                             <div align="center">$text.get("last")</div>
>                             </th>
>                             <th width="108" scope="col">
>                                 <div align="center">$text.get("first")</div>
>                             </th>
>                             <th width="165" scope="col">
>                                 <div align="center">$text.get("Address")</div>
>                             </th>
>                         </tr>
>                         #foreach($person in $Array)
>                             <tr>
>                                 <td>$person.last</td>
>                                 <td>$person.first</td>
>                                 <td>
>                                     <table width="342" border="0"
> cellspacing="0" cellpadding="0">
>                                         <tr>
>                                             <td width="386">$person.street</td>
>                                         </tr>
>                                         <tr>
>                                             <td>$person.city,
> $person.state $person.zip</td>
>                                         </tr>
>                                     </table>
>                                 </td>
>                             </tr>
>                             #end
>                     </table>
>                     <input type="submit" name="Submit" value="
> $text.get("done") " onclick="resultsForm.action.value='done'" >
> 
>                     #else
>                         <p>Sorry but there is no data in the database
> to sort.  Please
>                             enter information in the databse </p>
>                     <input type="submit" name="Submit2" value="
> $text.get("enter")" onclick="resultsForm.action.value='enterInfo'">
>                     #end
>          </form>
> 
> //All try/Catch blocks have been removed to save space.  Located in
> the DB class in the logic packagae.
> 
>     public StringWriter createContext(InfoBean [] array, DB data)
>     {
>             Velocity.init();
> 
>         Template template = null;
> 
>             template = Velocity.getTemplate("/sortResults.vm");
>         VelocityContext context = new VelocityContext();
> 
>         data.removeInsert(array);
>         List <Map<String, String>> vecInfo = data.getInformation();
>         context.put("Array",  vecInfo);
> 
>         StringWriter writer = new StringWriter ();
>             template.merge(context, writer);
> 
>         return writer;
>     }
> 
> //data is an object reference to DB class.
> else if(action.equals("default"))
>             {
>                 InfoBean sorted [] = new InfoBean
> [data.getInformation().size()];
>                 sorted = data.createSortedArray("ascending", "first", data);
>                 data.removeInsert(sorted);
>                 StringWriter writer = data.createContext(sorted, data);
>                     //response.getWriter().println(template.toString());
> 
>                 try
>                 {
>                      response.setContentType("text/html");
>                     PrintWriter out = response.getWriter();
>                     out.println(writer.toString());
>                 }
>                 catch(IOException e)
>                 {e.printStackTrace();}
> 
>                 return (mapping.findForward("default"));
>             }
> 
> -Paul
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org