Fórumok

Use velocity in a hook

Sandra del Moral Lazo, módosítva 10 év-val korábban

Use velocity in a hook

New Member Bejegyzések: 23 Csatlakozás dátuma: 2013.08.07. Legújabb bejegyzések
Hi,

I have a login hook and I wanna generate a code with a velocity template, but when try to get the template's file give me this error:
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'C:/Users/sdelmora/template.vm'


but the file exists because if I do it with a File and a FileReader works. ( I try it with absolute path and relative path too)

The code of function works too, because in other portlet the template is generate correctly.
This is my velocity code:

		VelocityContext velocityContext = new VelocityContext();
				
		Velocity.setProperty("resource.loader", "webapp");
		Velocity.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");
		Velocity.setApplicationAttribute("javax.servlet.ServletContext", httpRequest.getSession().getServletContext());
		Velocity.init();
				
		Template tpl;

                tpl = Velocity.getTemplate("C:/Users/sdelmora/template.vm");


Any idea about the problem?

Thanks!
thumbnail
David H Nebinger, módosítva 10 év-val korábban

RE: Use velocity in a hook

Liferay Legend Bejegyzések: 14915 Csatlakozás dátuma: 2006.09.02. Legújabb bejegyzések
getTemplate() does not take a whole file path. Instead it takes a single name that represents a resource to load.

From the Velocity doco, I think you'd have to do something like:

Properties p = new Properties();
p.setProperty("file.resource.loader.path", "C:/Users/sdelmora");
Velocity.init( p );


You're basically setting up velocity to use a resource from your servlet, yet you're not pulling the resource from the servlet. If you're going to use a file resource, then use a file resource.
Sandra del Moral Lazo, módosítva 10 év-val korábban

RE: Use velocity in a hook

New Member Bejegyzések: 23 Csatlakozás dátuma: 2013.08.07. Legújabb bejegyzések
But I need put some parameters into the template, and for it I was doing getTemplate.

This is my code:

    VelocityContext velocityContext = new VelocityContext();
    Velocity.setProperty("resource.loader", "webapp");
    Velocity.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");			             
    Velocity.setApplicationAttribute("javax.servlet.ServletContext",httpRequest.getSession().getServletContext());
    Velocity.init();
    Template tpl;
    tpl = Velocity.getTemplate("templatesl/template.vm");
    velocityContext.put("initializes", initializes);
    Writer writer = new StringWriter();
    tpl.merge(velocityContext, writer);	
    System.out.println(writer.toString());
    return writer.toString();


Do you know some documentation about Velocity in Liferay?
Exists some way to do this? Because if I do it with property I don't know how do pass the parameter and get a string.


Thank you very much.
thumbnail
David H Nebinger, módosítva 10 év-val korábban

RE: Use velocity in a hook

Liferay Legend Bejegyzések: 14915 Csatlakozás dátuma: 2006.09.02. Legújabb bejegyzések
Your issue is not a Liferay issue, it's a developer issue. It has nothing to do with Liferay, you're just using velocity incorrectly.
Sandra del Moral Lazo, módosítva 10 év-val korábban

RE: Use velocity in a hook

New Member Bejegyzések: 23 Csatlakozás dátuma: 2013.08.07. Legújabb bejegyzések
What would you say with I am not use correctly velocity?
I want generate a code dinamically with some variables and for this I made getTemplate and pass the parameter. (the code that I put in the before comment)
Exist any other way to do this? Or what I should do for use correctly velocity, because I don't understand you.
My knowledge is that velocity let create a template and put parameter I this is wanna do, I belive that is use more for theme but I think it can be correct too.

Thanks
thumbnail
David H Nebinger, módosítva 10 év-val korábban

RE: Use velocity in a hook

Liferay Legend Bejegyzések: 14915 Csatlakozás dátuma: 2006.09.02. Legújabb bejegyzések
Okay, if you actually read the velocity doco, you'll find that the getTemplate() method does not take a filesystem path. All it takes is a name. You use it like:

tpl = Velocity.getTemplate("mytemplate.vm");


That's all.

The properties that you set control how the mytemplate.vm file is loaded.

If you use the file resource loader, the mytemplate.vm file will be searched for in the path you specify.

However, you are using the web resource loader. This attempts to find the mytemplate.vm file in the web app itself, and does not look externally in the filesystem for the template.

So you're not using it correctly because:

a) you're trying to pass a full path as the argument, and that's not supported.
b) you want the template to come from the filesystem, but the properties you're using say to load it from the web app itself.
Sandra del Moral Lazo, módosítva 10 év-val korábban

RE: Use velocity in a hook

New Member Bejegyzések: 23 Csatlakozás dátuma: 2013.08.07. Legújabb bejegyzések
Thanks! I understand and it works.