On 9/13/07, Somogyi Arnold <[EMAIL PROTECTED]> wrote:
> Hello People,
>
> I have a porblem with Velocity Template Engine. Please, help me.
>
> I have maked a general html render servlet. It is receiving the all
> *.html requests (GET or POST) and they have a parameter. For example:
> http://localhost:8080/oxigen/welcome.html?class=com.remal.oxigen.ui.webc
> ontent.welcome.WelcomePage or
> http://localhost:8080/oxigen/login.html?class=com.remal.oxigen.ui.compon
> ents.login.LoginPage
>
>
> In processRequest method of my servlet will render current html page:
>
> ControllerServlet.java:
> private void processRequest(HttpServletRequest request,
> HttpServletResponse response)
> {
> ...
> //// get parameter from GET or POST request
> String className =
> StringUtils.defaultString(request.getParameter("class"), "null");
>
> Class instance = Class.forName(className);
> ConvertableToHTML o = (ConvertableToHTML)instance.newInstance();
> o.setRequest(request);
> o.setResponse(response);
> o.setLanguage( getLanguageCode() );
> ....
>
> response.setContentType("text/html;charset=UTF-8");
> PrintWriter out = response.getWriter();
> out.println( o.toHTML() );
> out.close();
> ...
> }
>
>
> WelcomePage.java:
> package com.remal.oxigen.ui.webcontent.welcome;
> public class WelcomePage implements ConvertableToHTML
> {
> ...
> public String toHTML()
> {
> ...
> String packageName = this.getClass().getCanonicalName();
>
> Properties p = new Properties();
> p.setProperty("resource.loader", "class");
> p.setProperty("class.resource.loader.class",
> "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
>
> VelocityEngine engine = new VelocityEngine();
> //engine.init(p);
> engine.init();
you are initializing with the default properties, which use the
FileResourceLoader and are very difficult to get working properly
(especially portably) in a webapp. why did you comment out the
engine.init(p) call? you are much better off init'ing with those
properties, since the ClasspathResourceLoader is much better in a
webapp.
> //Template template = engine.getTemplate("classes/" +
> packageName.replace('.', '/') + ".html");
i believe this one is closest, though i don't think it should begin
with the "classes" part. use your Properties above to init the engine
and then try:
Template template =
engine.getTemplate("/"+packageName.replace('.','/') +".html");
> //Template template =
> engine.getTemplate(this.getClass().getSimpleName() + ".html");
definitely won't work.
> Template template = engine.getTemplate(packageName + ".html");
nope.
>
> VelocityContext velocityContext = new VelocityContext();
> ...
> velocityContext.put(<keys>, <values>);
>
> writer = new StringWriter();
> template.merge(velocityContext, writer);
> return writer.toString();
> }
> }
>
> Part of file structure of my project:
> my.war
> / index.htm
> / WEB_INF
> /classes
> /com
> /remal
> /oxigen
> /ui
> /webcontent
> /welcome
> /WelcomePage.class
> /WelcomePage.html
>
>
> When I try to use whatever url, for example
> http://localhost:8080/oxigen/welcome.html?class=com.remal.oxigen.ui.webc
> ontent.welcome.WelcomePage I have a exception because the getTemplate
> method can not found current velocity template resource file within war
> file.
>
> org.apache.velocity.exception.ResourceNotFoundException: Unable to find
> resource 'com.remal.oxigen.ui.webcontent.welcome.WelcomePage.html' at
> org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(Re
> sourceManagerImpl.java:452) at
> org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(Res
> ourceManagerImpl.java:335) at
> org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.
> java:1102) at
> org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.
> java:1077) at
> org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:5
> 28) at com.remal.oxigen.ui.ConvertToHTML.toHTML
> ...
>
> What is the good input parameter for getTemplate(...) ?
>
> SoMa
>
>
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> Ezen uzenet es annak barmely csatolt anyaga bizalmas, jogi vedelem alatt all,
> a nyilvanos kozlestol vedett.
> Az uzenetet kizarolag a cimzett, illetve az altala meghatalmazottak
> hasznalhatjak fel. Ha On nem az uzenet cimzettje,
> ugy kerjuk, hogy telefonon, vagy e-mail-ben ertesitse errol az uzenet
> kuldojet es torolje az uzenetet,
> valamint annak osszes csatolt mellekletet a rendszerebol. Ha On nem az uzenet
> cimzettje, abban az esetben tilos az
> uzenetet vagy annak barmely csatolt mellekletet lemasolnia, elmentenie, az
> uzenet tartalmat barkivel kozolnie
> vagy azzal visszaelnie.
>
>
> This message and any attachment are confidential and are legally privileged.
> It is intended solely for the use of
> the individual or entity to whom it is addressed and others authorised to
> receive it. If you are not the intended
> recipient, please telephone or email the sender and delete this message and
> any attachment from your system.
> Please note that any dissemination, distribution, copying or use of or
> reliance upon the information contained in
> and transmitted with this e-mail by or to anyone other than the recipient
> designated above by the sender is
> unauthorised and strictly prohibited.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]