You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by sunil goyal <su...@gmail.com> on 2006/01/16 16:05:34 UTC

VelocityEngine thread-safe

Hello all,

I am using VelocityEngine within a servlet. I want to know weather I
should create instance of VelocityEngine everytime (within doPost()
method) or just initialize the instance once in the init() method of
the servlet.

If VelocityEngine is thread-safe then I can perhaps do the following:

>>>>>>>>
VelocityEngine ve;

public void init(ServletConfig arg0) throws ServletException {
ve = new VelocityEngine();
                try {
                        Properties p = new Properties();
                       
p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, path);
                        p.setProperty("runtime.log", path + "velocity.log");
                        ve.init(p);
                } catch (Exception ex) {
                        log.error("Error configuring the VelocityEngine: "
                                        + ex.getMessage());
                        ex.printStackTrace();
                }
        }


        public void doPost(HttpServletRequest request,
HttpServletResponse response)
                        throws ServletException, IOException {
                VelocityContext ctx = new VelocityContext();
// depending on request parameters, a particular template is chosen.
                Template template = ve.getTemplate("test.xml");
                ctx.put("hello","Hello World");

                StringWriter strWriter = new StringWriter();
                response.setContentType("text/xml; charset=UTF-8");

// template data and context is merged in a stringbuffer
                try {
                        if (template != null) {
                                template.merge(ctx, strWriter);
                        }
                } catch (Exception ex) {
                }

// http response is sent..
                PrintWriter writer = new PrintWriter(new
OutputStreamWriter(response
                                .getOutputStream(), "UTF8"), true);
                writer.println(strWriter);
                response.setContentLength(strWriter.getBuffer().length());
}

Is the above code alright? Or do I need to create and initialize
VelocityEngine() within the doPost() method.

Thanks

Regards
Sunil

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


Re: VelocityFAQ and Velocity-tools-1.2 NullPointerException

Posted by David Sperling <ve...@hakodateit.com>.
I downloaded a snapshot:
http://svn.apache.org/snapshots/jakarta-velocity/

built it:
ant jar

put the new velocity-1.5.jar in my classpath and deleted velocity-1.4 jar.

And no more NPE stack traces.  :-)


Thank You,

David

Nathan Bubna wrote:

>hmm.  no, this isn't the NPE mentioned in the FAQ.   i think you've
>run afoul of a bug that was fixed, unfixed, and refixed.
>
>Fixed: http://svn.apache.org/viewcvs.cgi/jakarta/velocity/trunk/src/java/org/apache/velocity/io/VelocityWriter.java?rev=75937&r1=75805&r2=75937
>
>Unfixed: http://svn.apache.org/viewcvs.cgi/jakarta/velocity/trunk/src/java/org/apache/velocity/io/VelocityWriter.java?rev=75959&r1=75949&r2=75959
>
>Refixed: http://svn.apache.org/viewcvs.cgi/jakarta/velocity/trunk/src/java/org/apache/velocity/io/VelocityWriter.java?rev=75960&r1=75959&r2=75960
>
>unfortunately, it appears that Velocity 1.4 was released after the
>"unfixed" revision and before the refixed revision, since the problem
>occurs on line 319 in both the 1.4 tag and the unfixed revision on the
>trunk.  :(
>
>so, all i can recommend is that you check out the last SVN code for
>Velocity 1.5 and use that.  hopefully we'll get it released soon, but
>in the meantime, it is quite stable and definitely superior to the 1.4
>version.  if you are unable to build it yourself, let me know and i
>will upload a nightly build that you can use.
>
>On 2/5/06, David Sperling <zz...@yahoo.com> wrote:
>  
>
>>My velocity-struts-1.2 message tool is throwing NPEs,
>>even though it looks to me that my action servlet
>>is being loaded before the velocity servlet.
>>
>>The velocity faq says that the MessageTool will throw
>>a NullPointerException if the ActionServlet isn't
>>initialized before trying to use struts-tools.
>>I set my ActionServlet to load 1st and my
>>Velocity Servlet to load 3rd.  But I still get
>>NullPointerException when I don't have a resource
>>in my resource bundle.
>>
>>So $text.resource.in.bundle
>>displays fine but
>>   $text.resource.not.in.bundle
>>gives a null pointer stack trace.
>>
>>
>>
>>Below I've listed my web.xml and a sample stack trace.
>> I'd like to know what I need to do to get
>>the text "$text.resource.not.in.bundle" in my pages
>>instead of a stack trace.
>>
>>
>>
>>Cheers,
>>
>>
>><?xml version="1.0" encoding="UTF-8"?>
>><!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
>>Inc.//DTD Web Application 2.3//EN"
>>"http://java.sun.com/dtd/web-app_2_3.dtd">
>><web-app>
>>  <display-name>Reservations Web App</display-name>
>>  <filter>
>>    <filter-name>Set Character Encoding</filter-name>
>>
>><filter-class>mydomain.reservations.filters.SetCharacterEncodingFilter</filter-class>
>>    <init-param>
>>      <param-name>encoding</param-name>
>>      <param-value>UTF-8</param-value>
>>    </init-param>
>>  </filter>
>>  <filter-mapping>
>>    <filter-name>Set Character Encoding</filter-name>
>>    <url-pattern>/*</url-pattern>
>>  </filter-mapping>
>>
>>  <servlet>
>>    <servlet-name>action</servlet-name>
>>
>><servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
>>    <init-param>
>>      <param-name>config</param-name>
>>      <param-value>/WEB-INF/main/strutsConfig.xml,
>>                                /WEB-INF/hotel/hotelConfig.xml,
>>                                /WEB-INF/rate/rateConfig.xml,
>>                                /WEB-INF/room/roomMasterConfig.xml,
>>                                /WEB-INF/glance/atAGlanceConfig.xml,
>>                            /WEB-INF/user/userConfig.xml,
>>                                /WEB-INF/reservation/reservationDetailConfig.xml,
>>                                /WEB-INF/calendar/calendarConfig.xml,
>>                                /WEB-INF/plan/planConfig.xml,
>>                                /WEB-INF/feature/featureConfig.xml,
>>                                /WEB-INF/member/memberConfig.xml,
>>                                /WEB-INF/external/externalConfig.xml,
>>                                /WEB-INF/report/reportConfig.xml
>>        </param-value>
>>    </init-param>
>>    <init-param>
>>      <param-name>debug</param-name>
>>      <param-value>2</param-value>
>>    </init-param>
>>    <init-param>
>>      <param-name>detail</param-name>
>>      <param-value>2</param-value>
>>    </init-param>
>>    <load-on-startup>1</load-on-startup>
>>  </servlet>
>>
>>
>>
>>  <servlet>
>>    <servlet-name>exportServlet</servlet-name>
>>
>><servlet-class>mydomain.reservations.export.servlet.ExportServlet</servlet-class>
>>    <load-on-startup>2</load-on-startup>
>>  </servlet>
>>
>>  <servlet>
>>    <servlet-name>velocity</servlet-name>
>>
>><servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
>>    <init-param>
>>
>><param-name>org.apache.velocity.toolbox</param-name>
>>      <param-value>/WEB-INF/toolbox.xml</param-value>
>>    </init-param>
>>    <init-param>
>>
>><param-name>org.apache.velocity.properties</param-name>
>>
>><param-value>/WEB-INF/velocity.properties</param-value>
>>    </init-param>
>>    <load-on-startup>3</load-on-startup>
>>  </servlet>
>>
>>
>>
>>  <servlet-mapping>
>>    <servlet-name>exportServlet</servlet-name>
>>    <url-pattern>/export</url-pattern>
>>  </servlet-mapping>
>>  <servlet-mapping>
>>    <servlet-name>action</servlet-name>
>>    <url-pattern>*.do</url-pattern>
>>  </servlet-mapping>
>>  <servlet-mapping>
>>    <servlet-name>velocity</servlet-name>
>>    <url-pattern>*.vm</url-pattern>
>>  </servlet-mapping>
>>  <welcome-file-list>
>>    <welcome-file>index.vm</welcome-file>
>>  </welcome-file-list>
>>  <resource-ref>
>>    <description>DB Connection</description>
>>    <res-ref-name>jdbc/reservationDB</res-ref-name>
>>    <res-type>javax.sql.DataSource</res-type>
>>    <res-auth>Container</res-auth>
>>  </resource-ref>
>>  <security-constraint>
>>    <web-resource-collection>
>>      <web-resource-name>Admin
>>Services</web-resource-name>
>>      <url-pattern>/admin/*</url-pattern>
>>    </web-resource-collection>
>>    <auth-constraint>
>>      <role-name>hotelAdmin</role-name>
>>  <!--Engineer will be employees companies that
>>re-sell the reservation system to hotels, -->
>>  <!--   they have access to hotels the posses-->
>>      <role-name>hotelEngineer</role-name>
>>  <!--Manager can update plans, room, rate, options,
>>and everything else in an owned hotel
>>          internal admin section -->
>>      <role-name>hotelManager</role-name>
>>  <!--Hotel operators can only access ataglance,
>>report and the order page for owned hotels-->
>>      <role-name>hotelOperator</role-name>
>>    </auth-constraint>
>>  </security-constraint>
>>  <login-config>
>>    <auth-method>FORM</auth-method>
>>    <form-login-config>
>>
>><form-login-page>/WEB-INF/main/login.vm</form-login-page>
>>
>><form-error-page>/WEB-INF/main/notAuthenticated.vm</form-error-page>
>>    </form-login-config>
>>  </login-config>
>>  <security-role>
>>    <role-name>hotelAdmin</role-name>
>>  </security-role>
>>  <security-role>
>>    <role-name>hotelManager</role-name>
>>  </security-role>
>>  <security-role>
>>    <role-name>hotelOperator</role-name>
>>  </security-role>
>>  <security-role>
>>    <role-name>hotelEngineer</role-name>
>>  </security-role>
>></web-app>
>>
>>
>>VelocityViewServlet : Error processing the template
>>
>>java.lang.NullPointerException
>>        at
>>org.apache.velocity.io.VelocityWriter.write(VelocityWriter.java:319)
>>        at
>>org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:291)
>>        at
>>org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:230)
>>        at
>>org.apache.velocity.Template.merge(Template.java:256)
>>        at
>>org.apache.velocity.tools.view.servlet.VelocityViewServlet.performMerge(Unknown
>>Source)
>>        at
>>org.apache.velocity.tools.view.servlet.VelocityViewServlet.mergeTemplate(Unknown
>>Source)
>>        at
>>org.apache.velocity.tools.view.servlet.VelocityViewServlet.doRequest(Unknown
>>Source)
>>        at
>>org.apache.velocity.tools.view.servlet.VelocityViewServlet.doGet(Unknown
>>Source)
>>        at
>>javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
>>        at
>>javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
>>        at
>>org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
>>        at
>>org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
>>        at
>>hakodatecgi.reservations.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:112)
>>        at
>>org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
>>        at
>>org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
>>        at
>>org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
>>        at
>>org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
>>        at
>>org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
>>        at
>>org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
>>        at
>>org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
>>        at
>>org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
>>        at
>>org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
>>        at
>>org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
>>        at
>>org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
>>        at
>>org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
>>        at
>>org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
>>        at
>>org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
>>        at
>>org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
>>        at
>>org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
>>        at
>>org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
>>        at
>>org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
>>        at
>>org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
>>        at
>>org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
>>        at
>>org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
>>        at
>>org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
>>        at
>>org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
>>        at
>>org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
>>        at
>>org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
>>        at java.lang.Thread.run(Thread.java:534)
>>
>>
>>---------------------------------------------------------------------
>>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
>
>  
>


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


Re: VelocityFAQ and Velocity-tools-1.2 NullPointerException

Posted by Nathan Bubna <nb...@gmail.com>.
hmm.  no, this isn't the NPE mentioned in the FAQ.   i think you've
run afoul of a bug that was fixed, unfixed, and refixed.

Fixed: http://svn.apache.org/viewcvs.cgi/jakarta/velocity/trunk/src/java/org/apache/velocity/io/VelocityWriter.java?rev=75937&r1=75805&r2=75937

Unfixed: http://svn.apache.org/viewcvs.cgi/jakarta/velocity/trunk/src/java/org/apache/velocity/io/VelocityWriter.java?rev=75959&r1=75949&r2=75959

Refixed: http://svn.apache.org/viewcvs.cgi/jakarta/velocity/trunk/src/java/org/apache/velocity/io/VelocityWriter.java?rev=75960&r1=75959&r2=75960

unfortunately, it appears that Velocity 1.4 was released after the
"unfixed" revision and before the refixed revision, since the problem
occurs on line 319 in both the 1.4 tag and the unfixed revision on the
trunk.  :(

so, all i can recommend is that you check out the last SVN code for
Velocity 1.5 and use that.  hopefully we'll get it released soon, but
in the meantime, it is quite stable and definitely superior to the 1.4
version.  if you are unable to build it yourself, let me know and i
will upload a nightly build that you can use.

On 2/5/06, David Sperling <zz...@yahoo.com> wrote:
> My velocity-struts-1.2 message tool is throwing NPEs,
> even though it looks to me that my action servlet
> is being loaded before the velocity servlet.
>
> The velocity faq says that the MessageTool will throw
> a NullPointerException if the ActionServlet isn't
> initialized before trying to use struts-tools.
> I set my ActionServlet to load 1st and my
> Velocity Servlet to load 3rd.  But I still get
> NullPointerException when I don't have a resource
> in my resource bundle.
>
> So $text.resource.in.bundle
> displays fine but
>    $text.resource.not.in.bundle
> gives a null pointer stack trace.
>
>
>
> Below I've listed my web.xml and a sample stack trace.
>  I'd like to know what I need to do to get
> the text "$text.resource.not.in.bundle" in my pages
> instead of a stack trace.
>
>
>
> Cheers,
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
> Inc.//DTD Web Application 2.3//EN"
> "http://java.sun.com/dtd/web-app_2_3.dtd">
> <web-app>
>   <display-name>Reservations Web App</display-name>
>   <filter>
>     <filter-name>Set Character Encoding</filter-name>
>
> <filter-class>mydomain.reservations.filters.SetCharacterEncodingFilter</filter-class>
>     <init-param>
>       <param-name>encoding</param-name>
>       <param-value>UTF-8</param-value>
>     </init-param>
>   </filter>
>   <filter-mapping>
>     <filter-name>Set Character Encoding</filter-name>
>     <url-pattern>/*</url-pattern>
>   </filter-mapping>
>
>   <servlet>
>     <servlet-name>action</servlet-name>
>
> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
>     <init-param>
>       <param-name>config</param-name>
>       <param-value>/WEB-INF/main/strutsConfig.xml,
>                                 /WEB-INF/hotel/hotelConfig.xml,
>                                 /WEB-INF/rate/rateConfig.xml,
>                                 /WEB-INF/room/roomMasterConfig.xml,
>                                 /WEB-INF/glance/atAGlanceConfig.xml,
>                             /WEB-INF/user/userConfig.xml,
>                                 /WEB-INF/reservation/reservationDetailConfig.xml,
>                                 /WEB-INF/calendar/calendarConfig.xml,
>                                 /WEB-INF/plan/planConfig.xml,
>                                 /WEB-INF/feature/featureConfig.xml,
>                                 /WEB-INF/member/memberConfig.xml,
>                                 /WEB-INF/external/externalConfig.xml,
>                                 /WEB-INF/report/reportConfig.xml
>         </param-value>
>     </init-param>
>     <init-param>
>       <param-name>debug</param-name>
>       <param-value>2</param-value>
>     </init-param>
>     <init-param>
>       <param-name>detail</param-name>
>       <param-value>2</param-value>
>     </init-param>
>     <load-on-startup>1</load-on-startup>
>   </servlet>
>
>
>
>   <servlet>
>     <servlet-name>exportServlet</servlet-name>
>
> <servlet-class>mydomain.reservations.export.servlet.ExportServlet</servlet-class>
>     <load-on-startup>2</load-on-startup>
>   </servlet>
>
>   <servlet>
>     <servlet-name>velocity</servlet-name>
>
> <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
>     <init-param>
>
> <param-name>org.apache.velocity.toolbox</param-name>
>       <param-value>/WEB-INF/toolbox.xml</param-value>
>     </init-param>
>     <init-param>
>
> <param-name>org.apache.velocity.properties</param-name>
>
> <param-value>/WEB-INF/velocity.properties</param-value>
>     </init-param>
>     <load-on-startup>3</load-on-startup>
>   </servlet>
>
>
>
>   <servlet-mapping>
>     <servlet-name>exportServlet</servlet-name>
>     <url-pattern>/export</url-pattern>
>   </servlet-mapping>
>   <servlet-mapping>
>     <servlet-name>action</servlet-name>
>     <url-pattern>*.do</url-pattern>
>   </servlet-mapping>
>   <servlet-mapping>
>     <servlet-name>velocity</servlet-name>
>     <url-pattern>*.vm</url-pattern>
>   </servlet-mapping>
>   <welcome-file-list>
>     <welcome-file>index.vm</welcome-file>
>   </welcome-file-list>
>   <resource-ref>
>     <description>DB Connection</description>
>     <res-ref-name>jdbc/reservationDB</res-ref-name>
>     <res-type>javax.sql.DataSource</res-type>
>     <res-auth>Container</res-auth>
>   </resource-ref>
>   <security-constraint>
>     <web-resource-collection>
>       <web-resource-name>Admin
> Services</web-resource-name>
>       <url-pattern>/admin/*</url-pattern>
>     </web-resource-collection>
>     <auth-constraint>
>       <role-name>hotelAdmin</role-name>
>   <!--Engineer will be employees companies that
> re-sell the reservation system to hotels, -->
>   <!--   they have access to hotels the posses-->
>       <role-name>hotelEngineer</role-name>
>   <!--Manager can update plans, room, rate, options,
> and everything else in an owned hotel
>           internal admin section -->
>       <role-name>hotelManager</role-name>
>   <!--Hotel operators can only access ataglance,
> report and the order page for owned hotels-->
>       <role-name>hotelOperator</role-name>
>     </auth-constraint>
>   </security-constraint>
>   <login-config>
>     <auth-method>FORM</auth-method>
>     <form-login-config>
>
> <form-login-page>/WEB-INF/main/login.vm</form-login-page>
>
> <form-error-page>/WEB-INF/main/notAuthenticated.vm</form-error-page>
>     </form-login-config>
>   </login-config>
>   <security-role>
>     <role-name>hotelAdmin</role-name>
>   </security-role>
>   <security-role>
>     <role-name>hotelManager</role-name>
>   </security-role>
>   <security-role>
>     <role-name>hotelOperator</role-name>
>   </security-role>
>   <security-role>
>     <role-name>hotelEngineer</role-name>
>   </security-role>
> </web-app>
>
>
> VelocityViewServlet : Error processing the template
>
> java.lang.NullPointerException
>         at
> org.apache.velocity.io.VelocityWriter.write(VelocityWriter.java:319)
>         at
> org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:291)
>         at
> org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:230)
>         at
> org.apache.velocity.Template.merge(Template.java:256)
>         at
> org.apache.velocity.tools.view.servlet.VelocityViewServlet.performMerge(Unknown
> Source)
>         at
> org.apache.velocity.tools.view.servlet.VelocityViewServlet.mergeTemplate(Unknown
> Source)
>         at
> org.apache.velocity.tools.view.servlet.VelocityViewServlet.doRequest(Unknown
> Source)
>         at
> org.apache.velocity.tools.view.servlet.VelocityViewServlet.doGet(Unknown
> Source)
>         at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
>         at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
>         at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
>         at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
>         at
> hakodatecgi.reservations.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:112)
>         at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
>         at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
>         at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
>         at
> org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
>         at
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
>         at
> org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
>         at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
>         at
> org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
>         at
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
>         at
> org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
>         at
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
>         at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
>         at
> org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
>         at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
>         at
> org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
>         at
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
>         at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
>         at
> org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
>         at
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
>         at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
>         at
> org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
>         at
> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
>         at
> org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
>         at
> org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
>         at
> org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
>         at java.lang.Thread.run(Thread.java:534)
>
>
> ---------------------------------------------------------------------
> 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


VelocityFAQ and Velocity-tools-1.2 NullPointerException

Posted by David Sperling <zz...@yahoo.com>.
My velocity-struts-1.2 message tool is throwing NPEs,
even though it looks to me that my action servlet
is being loaded before the velocity servlet.

The velocity faq says that the MessageTool will throw
a NullPointerException if the ActionServlet isn't
initialized before trying to use struts-tools. 
I set my ActionServlet to load 1st and my
Velocity Servlet to load 3rd.  But I still get 
NullPointerException when I don't have a resource
in my resource bundle.

So $text.resource.in.bundle
displays fine but 
   $text.resource.not.in.bundle
gives a null pointer stack trace.



Below I've listed my web.xml and a sample stack trace.
 I'd like to know what I need to do to get 
the text "$text.resource.not.in.bundle" in my pages
instead of a stack trace.



Cheers,


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <display-name>Reservations Web App</display-name>
  <filter>
    <filter-name>Set Character Encoding</filter-name>
   
<filter-class>mydomain.reservations.filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>action</servlet-name>
   
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/main/strutsConfig.xml,
				/WEB-INF/hotel/hotelConfig.xml,
				/WEB-INF/rate/rateConfig.xml,
				/WEB-INF/room/roomMasterConfig.xml,
				/WEB-INF/glance/atAGlanceConfig.xml,
  			    /WEB-INF/user/userConfig.xml,
				/WEB-INF/reservation/reservationDetailConfig.xml,
				/WEB-INF/calendar/calendarConfig.xml,
				/WEB-INF/plan/planConfig.xml,
				/WEB-INF/feature/featureConfig.xml,
				/WEB-INF/member/memberConfig.xml,
				/WEB-INF/external/externalConfig.xml,
				/WEB-INF/report/reportConfig.xml
	</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  

  
  <servlet>
    <servlet-name>exportServlet</servlet-name>
   
<servlet-class>mydomain.reservations.export.servlet.ExportServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  
  <servlet>
    <servlet-name>velocity</servlet-name>
   
<servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
    <init-param>
     
<param-name>org.apache.velocity.toolbox</param-name>
      <param-value>/WEB-INF/toolbox.xml</param-value>
    </init-param>
    <init-param>
     
<param-name>org.apache.velocity.properties</param-name>
     
<param-value>/WEB-INF/velocity.properties</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
  </servlet>
  
  
  
  <servlet-mapping>
    <servlet-name>exportServlet</servlet-name>
    <url-pattern>/export</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>velocity</servlet-name>
    <url-pattern>*.vm</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.vm</welcome-file>
  </welcome-file-list>
  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/reservationDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Admin
Services</web-resource-name>
      <url-pattern>/admin/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>		
      <role-name>hotelAdmin</role-name>
  <!--Engineer will be employees companies that
re-sell the reservation system to hotels, -->
  <!--   they have access to hotels the posses-->		
      <role-name>hotelEngineer</role-name>
  <!--Manager can update plans, room, rate, options,
and everything else in an owned hotel 
	  internal admin section -->	
      <role-name>hotelManager</role-name>
  <!--Hotel operators can only access ataglance,
report and the order page for owned hotels-->	
      <role-name>hotelOperator</role-name>
    </auth-constraint>
  </security-constraint>
  <login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
     
<form-login-page>/WEB-INF/main/login.vm</form-login-page>
     
<form-error-page>/WEB-INF/main/notAuthenticated.vm</form-error-page>
    </form-login-config>
  </login-config>
  <security-role>
    <role-name>hotelAdmin</role-name>
  </security-role>
  <security-role>
    <role-name>hotelManager</role-name>
  </security-role>
  <security-role>
    <role-name>hotelOperator</role-name>
  </security-role>
  <security-role>
    <role-name>hotelEngineer</role-name>
  </security-role>
</web-app>


VelocityViewServlet : Error processing the template

java.lang.NullPointerException
	at
org.apache.velocity.io.VelocityWriter.write(VelocityWriter.java:319)
	at
org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:291)
	at
org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:230)
	at
org.apache.velocity.Template.merge(Template.java:256)
	at
org.apache.velocity.tools.view.servlet.VelocityViewServlet.performMerge(Unknown
Source)
	at
org.apache.velocity.tools.view.servlet.VelocityViewServlet.mergeTemplate(Unknown
Source)
	at
org.apache.velocity.tools.view.servlet.VelocityViewServlet.doRequest(Unknown
Source)
	at
org.apache.velocity.tools.view.servlet.VelocityViewServlet.doGet(Unknown
Source)
	at
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
	at
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
	at
hakodatecgi.reservations.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:112)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
	at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
	at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
	at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
	at
org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
	at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
	at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
	at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
	at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
	at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
	at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
	at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
	at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
	at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
	at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
	at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
	at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
	at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
	at
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
	at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
	at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
	at
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
	at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
	at java.lang.Thread.run(Thread.java:534)


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


RE: Date Formatting

Posted by Edward Song <es...@royalmedia.com>.
Thanks Mike for your help,
It worked.


-----Original Message-----
From: Mike Kienenberger [mailto:mkienenb@gmail.com] 
Sent: Tuesday, January 17, 2006 2:11 PM
To: Velocity Users List
Subject: Re: Date Formatting

If you're using a standalone-application, you can just manually add the tool
to your context.

context.put("dateTool", new DateTool());

or you can write your own toolbox loader.

Note that I used the following in a struts application to create an email
toolbox, and it's also for v1.1, so it's only good as a
reference:

	public VelocityContext getEmailToolboxContext() throws
ChainedException
	{
        try
        {
    		ServletContext servletContext = getServletContext();

    		/* check the servlet config for a toolbox */
    		String toolboxFile = "/WEB-INF/email.toolbox.xml";
    		
            XMLToolboxManager mgr = null;
            InputStream is = null;
            try
            {
                // get the bits
                is = servletContext.getResourceAsStream(toolboxFile);
                if (toolboxFile != null)
                {
                    // little fix up
                    if (!toolboxFile.startsWith("/"))
                    {
                        toolboxFile = "/" + toolboxFile;
                    }

                    // get config file pathname
                    String pathname =
servletContext.getRealPath(toolboxFile);

                }
                else
                {
                    Velocity.info("VelocityViewServlet: No toolbox entry in
configuration.");
                }


                InputStream inputStreamFromToolboxFile = is;
                // InputStream inputStreamFromToolboxFile = new
FileInputStream("toolbox.xml");

                class MyToolboxManager extends XMLToolboxManager{};

                mgr = new MyToolboxManager();
                mgr.load(inputStreamFromToolboxFile);
            }
            finally
            {
                try
                {
                    if (is != null)
                    {
                        is.close();
                    }
                }
                catch(Exception ee) {}
            }
    		ToolboxContext toolCtx = mgr.getToolboxContext(null);
    		return new VelocityContext(toolCtx);
        }
        catch (Exception anException)
        {
            ChainedException newChainedException = new
ChainedException(anException, "Unable to create Toolbox Context ");
            throw newChainedException;
        }
	}


On 1/17/06, Edward Song <es...@royalmedia.com> wrote:
> Hi,
>
> I have been trying to get the generic velocity tools to work with my 
> java application.
> The application sends out emails, and I am using Velocity to generate 
> the HTML string to be sent in the email.
>
> The one thing I am looking to do is to have a java.util.Date object 
> formatted for presentation.
> I saw and downloaded the velocity tools packages and added 
> velocity-tools-generic-1.2.jar to my classpath.
>
> I then restarted my app and found that the DateTool still does not 
> format the date.
>
> I take a look at the examples, and a lot of the examples use the 
> VelocityViewServlet and pass in a parameter pointing to toolbox.xml.  
> My application is standalone and has no use for struts.  Without this 
> configuration it seems that VelocityTools will not know where to look 
> for toolbox.xml.
> How do I specify this location in a standalone application?
>
> Ed
>
>
> -----Original Message-----
> From: Edward Song [mailto:esong@royalmedia.com]
> Sent: Tuesday, January 17, 2006 11:44 AM
> To: 'Velocity Users List'
> Subject: RE: Date Formatting
>
> Nevermind, found the DateTools.
>
> http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/t
> ools/g
> eneric/DateTool.html
>
>
> -----Original Message-----
> From: Edward Song [mailto:esong@royalmedia.com]
> Sent: Tuesday, January 17, 2006 11:37 AM
> To: 'Velocity Users List'
> Subject: Date Formatting
>
> Hi all,
> Is there a way to perform simple date formatting from a date object 
> within a velocity template?
> I haven't found anything in the user guide and developer guide about 
> date formatting.
>
> I don't think that the developer should have to format the date and 
> add it to the context.
> Shouldn't template editors have this capability?
>
> I would think that it exists, I just don't know where to look, can 
> someone point me to the correct resource.
>
> Thanks for your time,
> Ed
>
>
> ---------------------------------------------------------------------
> 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
>
>
> ---------------------------------------------------------------------
> 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


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


Re: Date Formatting

Posted by Mike Kienenberger <mk...@gmail.com>.
If you're using a standalone-application, you can just manually add
the tool to your context.

context.put("dateTool", new DateTool());

or you can write your own toolbox loader.

Note that I used the following in a struts application to create an
email toolbox, and it's also for v1.1, so it's only good as a
reference:

	public VelocityContext getEmailToolboxContext() throws ChainedException
	{
        try
        {
    		ServletContext servletContext = getServletContext();

    		/* check the servlet config for a toolbox */
    		String toolboxFile = "/WEB-INF/email.toolbox.xml";
    		
            XMLToolboxManager mgr = null;
            InputStream is = null;
            try
            {
                // get the bits
                is = servletContext.getResourceAsStream(toolboxFile);
                if (toolboxFile != null)
                {
                    // little fix up
                    if (!toolboxFile.startsWith("/"))
                    {
                        toolboxFile = "/" + toolboxFile;
                    }

                    // get config file pathname
                    String pathname = servletContext.getRealPath(toolboxFile);

                }
                else
                {
                    Velocity.info("VelocityViewServlet: No toolbox
entry in configuration.");
                }


                InputStream inputStreamFromToolboxFile = is;
                // InputStream inputStreamFromToolboxFile = new
FileInputStream("toolbox.xml");

                class MyToolboxManager extends XMLToolboxManager{};

                mgr = new MyToolboxManager();
                mgr.load(inputStreamFromToolboxFile);
            }
            finally
            {
                try
                {
                    if (is != null)
                    {
                        is.close();
                    }
                }
                catch(Exception ee) {}
            }
    		ToolboxContext toolCtx = mgr.getToolboxContext(null);
    		return new VelocityContext(toolCtx);
        }
        catch (Exception anException)
        {
            ChainedException newChainedException = new
ChainedException(anException, "Unable to create Toolbox Context ");
            throw newChainedException;
        }
	}


On 1/17/06, Edward Song <es...@royalmedia.com> wrote:
> Hi,
>
> I have been trying to get the generic velocity tools to work with my java
> application.
> The application sends out emails, and I am using Velocity to generate the
> HTML string to be sent in the email.
>
> The one thing I am looking to do is to have a java.util.Date object
> formatted for presentation.
> I saw and downloaded the velocity tools packages and added
> velocity-tools-generic-1.2.jar to my classpath.
>
> I then restarted my app and found that the DateTool still does not format
> the date.
>
> I take a look at the examples, and a lot of the examples use the
> VelocityViewServlet and pass in a parameter pointing to toolbox.xml.  My
> application is standalone and has no use for struts.  Without this
> configuration it seems that VelocityTools will not know where to look for
> toolbox.xml.
> How do I specify this location in a standalone application?
>
> Ed
>
>
> -----Original Message-----
> From: Edward Song [mailto:esong@royalmedia.com]
> Sent: Tuesday, January 17, 2006 11:44 AM
> To: 'Velocity Users List'
> Subject: RE: Date Formatting
>
> Nevermind, found the DateTools.
>
> http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/tools/g
> eneric/DateTool.html
>
>
> -----Original Message-----
> From: Edward Song [mailto:esong@royalmedia.com]
> Sent: Tuesday, January 17, 2006 11:37 AM
> To: 'Velocity Users List'
> Subject: Date Formatting
>
> Hi all,
> Is there a way to perform simple date formatting from a date object within a
> velocity template?
> I haven't found anything in the user guide and developer guide about date
> formatting.
>
> I don't think that the developer should have to format the date and add it
> to the context.
> Shouldn't template editors have this capability?
>
> I would think that it exists, I just don't know where to look, can someone
> point me to the correct resource.
>
> Thanks for your time,
> Ed
>
>
> ---------------------------------------------------------------------
> 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
>
>
> ---------------------------------------------------------------------
> 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: Date Formatting

Posted by Edward Song <es...@royalmedia.com>.
Hi,

I have been trying to get the generic velocity tools to work with my java
application.
The application sends out emails, and I am using Velocity to generate the
HTML string to be sent in the email.

The one thing I am looking to do is to have a java.util.Date object
formatted for presentation.
I saw and downloaded the velocity tools packages and added
velocity-tools-generic-1.2.jar to my classpath.

I then restarted my app and found that the DateTool still does not format
the date.

I take a look at the examples, and a lot of the examples use the
VelocityViewServlet and pass in a parameter pointing to toolbox.xml.  My
application is standalone and has no use for struts.  Without this
configuration it seems that VelocityTools will not know where to look for
toolbox.xml.
How do I specify this location in a standalone application?

Ed


-----Original Message-----
From: Edward Song [mailto:esong@royalmedia.com] 
Sent: Tuesday, January 17, 2006 11:44 AM
To: 'Velocity Users List'
Subject: RE: Date Formatting

Nevermind, found the DateTools.

http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/tools/g
eneric/DateTool.html


-----Original Message-----
From: Edward Song [mailto:esong@royalmedia.com]
Sent: Tuesday, January 17, 2006 11:37 AM
To: 'Velocity Users List'
Subject: Date Formatting

Hi all,
Is there a way to perform simple date formatting from a date object within a
velocity template?
I haven't found anything in the user guide and developer guide about date
formatting.

I don't think that the developer should have to format the date and add it
to the context.  
Shouldn't template editors have this capability?

I would think that it exists, I just don't know where to look, can someone
point me to the correct resource.

Thanks for your time,
Ed


---------------------------------------------------------------------
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


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


RE: Date Formatting

Posted by Edward Song <es...@royalmedia.com>.
Nevermind, found the DateTools.

http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/tools/g
eneric/DateTool.html


-----Original Message-----
From: Edward Song [mailto:esong@royalmedia.com] 
Sent: Tuesday, January 17, 2006 11:37 AM
To: 'Velocity Users List'
Subject: Date Formatting

Hi all,
Is there a way to perform simple date formatting from a date object within a
velocity template?
I haven't found anything in the user guide and developer guide about date
formatting.

I don't think that the developer should have to format the date and add it
to the context.  
Shouldn't template editors have this capability?

I would think that it exists, I just don't know where to look, can someone
point me to the correct resource.

Thanks for your time,
Ed


---------------------------------------------------------------------
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


Date Formatting

Posted by Edward Song <es...@royalmedia.com>.
Hi all,
Is there a way to perform simple date formatting from a date object within a
velocity template?
I haven't found anything in the user guide and developer guide about date
formatting.

I don't think that the developer should have to format the date and add it
to the context.  
Shouldn't template editors have this capability?

I would think that it exists, I just don't know where to look, can someone
point me to the correct resource.

Thanks for your time,
Ed


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


Re: VelocityEngine thread-safe

Posted by Will Glass-Husain <wg...@forio.com>.
VelocityEngine is threadsafe.  Note that context objects are not, so be sure 
to create a new one for each request.

WILL

----- Original Message ----- 
From: "sunil goyal" <su...@gmail.com>
To: <ve...@jakarta.apache.org>
Sent: Monday, January 16, 2006 7:05 AM
Subject: VelocityEngine thread-safe


Hello all,

I am using VelocityEngine within a servlet. I want to know weather I
should create instance of VelocityEngine everytime (within doPost()
method) or just initialize the instance once in the init() method of
the servlet.

If VelocityEngine is thread-safe then I can perhaps do the following:

>>>>>>>>
VelocityEngine ve;

public void init(ServletConfig arg0) throws ServletException {
ve = new VelocityEngine();
                try {
                        Properties p = new Properties();

p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, path);
                        p.setProperty("runtime.log", path + "velocity.log");
                        ve.init(p);
                } catch (Exception ex) {
                        log.error("Error configuring the VelocityEngine: "
                                        + ex.getMessage());
                        ex.printStackTrace();
                }
        }


        public void doPost(HttpServletRequest request,
HttpServletResponse response)
                        throws ServletException, IOException {
                VelocityContext ctx = new VelocityContext();
// depending on request parameters, a particular template is chosen.
                Template template = ve.getTemplate("test.xml");
                ctx.put("hello","Hello World");

                StringWriter strWriter = new StringWriter();
                response.setContentType("text/xml; charset=UTF-8");

// template data and context is merged in a stringbuffer
                try {
                        if (template != null) {
                                template.merge(ctx, strWriter);
                        }
                } catch (Exception ex) {
                }

// http response is sent..
                PrintWriter writer = new PrintWriter(new
OutputStreamWriter(response
                                .getOutputStream(), "UTF8"), true);
                writer.println(strWriter);
                response.setContentLength(strWriter.getBuffer().length());
}

Is the above code alright? Or do I need to create and initialize
VelocityEngine() within the doPost() method.

Thanks

Regards
Sunil

---------------------------------------------------------------------
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