Foren

Not able to locate Freemarker Template In Liferay

Seeya S Kudtarker, geändert vor 11 Jahren.

Not able to locate Freemarker Template In Liferay

Regular Member Beiträge: 187 Beitrittsdatum: 16.01.13 Neueste Beiträge
I am using Liferay for developing my application and want to use freemarker to develop template.

I was just testing freemarker. When I deploy my application, it says that Template cannot be found. I know that template file should be in the src folder. So I have created helloworld.ftl in docroot.WEB-INF/src folder and in the code I try to access it using the statements- Configuration cfg = new Configuration(); Template template = cfg.getTemplate("src/helloworld.ftl"); The error is as follows: java.io.FileNotFoundException: Template src/helloworld.ftl not found.

How should I give my path of the folder?
thumbnail
David H Nebinger, geändert vor 11 Jahren.

RE: Not able to locate Freemarker Template In Liferay

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
If it's in the source folder, it gets copied to the classes folder and becomes a class path resource. Try just using "helloworld.ftl" as the template.

If you use a nested folder, it should be a relative class path in the form of "com/example/templates/helloworld.ftl".
Seeya S Kudtarker, geändert vor 11 Jahren.

RE: Not able to locate Freemarker Template In Liferay

Regular Member Beiträge: 187 Beitrittsdatum: 16.01.13 Neueste Beiträge
Hi David,
I had tried "helloworld.ftl" earlier but it gave the same error which I mentioned above.
I have it under docroot/WEB-INF/src folder. I haven't included it under any class folder/package.
I tried in the form you suggested:
Template template = cfg.getTemplate("com/test/mis/portal/helloworld.ftl");

I still get the follwing error.
java.io.FileNotFoundException: Template com/test/mis/portal/helloworld.ftl not found.
at freemarker.template.Configuration.getTemplate(Configuration.java:580)
at freemarker.template.Configuration.getTemplate(Configuration.java:543)
at com.test.mis.portal.MISPortalActionUtil.calculateLeaveDuration(MISPortalActionUtil.java:669)

Oh by the way, I am using it to send email. What I mean is, when a form is submitted an email alert should be sent to the concerned person. It works fine. I am using freemarker for email template.
thumbnail
David H Nebinger, geändert vor 11 Jahren.

RE: Not able to locate Freemarker Template In Liferay

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
To load freemarker templates from the classpath, you need to use a ClassTemplateLoader() as the template loader...

Configuration config = new Configuration();
config.setTemplateLoader(new ClassTemplateLoader(getClass(), "/");
Template template = config.getTemplate("helloworld.ftl");
template = config.getTemplate("com/test/mis/portal/helloworld.ftl");
Seeya S Kudtarker, geändert vor 11 Jahren.

RE: Not able to locate Freemarker Template In Liferay

Regular Member Beiträge: 187 Beitrittsdatum: 16.01.13 Neueste Beiträge
Hi David,
Thanks! emoticon
I wasn't loading the template from the classpath.
Like you had said earlier I can now use -
Template template = config.getTemplate("helloworld.ftl"); and do not need to include the entire path "com/test/mis/portal/helloworld.ftl" since it is under src folder. It is successfully generating the template for me.
Now I can use it for sending mail alerts.
Thanks again!! emoticon

Before I got your solution I was going through the following link which is when I realised I wasn't loading the template. Here is the link I want to share. May be it's helpful for you and others:
http://www.vogella.com/articles/FreeMarker/article.html#freemarkertest

Regards

Seeya
Seeya S Kudtarker, geändert vor 11 Jahren.

RE: Not able to locate Freemarker Template In Liferay

Regular Member Beiträge: 187 Beitrittsdatum: 16.01.13 Neueste Beiträge
Hi David,

I have tried sample codes using freemarker and was able to create templates in txt and html formats.

My initial code for sending email (without using templates is as follows):

InternetAddress from = new InternetAddress(fromAddress, fromName);
InternetAddress to = new InternetAddress(toAddress, toName);

MailMessage mailMessage = new MailMessage(from, to, subject, body, true);
MailServiceUtil.sendEmail(mailMessage);


The imports are:
import javax.mail.internet.InternetAddress;
import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.mail.MailMessage;

Like I have said I created templates and was able to generate html,txt format files using template by following the code you had given me.

I am not able to figure out how should I pass the "File" to MailMessage and MailServiceUtil.sendEmail since they do not accept "File" type.

How should I go about solving this?

By the way, should I create a separate thread for this?
I thought of continuing here..
thumbnail
David H Nebinger, geändert vor 11 Jahren.

RE: Not able to locate Freemarker Template In Liferay

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
What file? An attachment? If this is the case, then use mailMessage.addFileAttachment().

If you're saying you want the template to be used for creating the body, then you would just invoke the template and use the result as the body for the constructor.
Seeya S Kudtarker, geändert vor 11 Jahren.

RE: Not able to locate Freemarker Template In Liferay

Regular Member Beiträge: 187 Beitrittsdatum: 16.01.13 Neueste Beiträge
Hey David,

I did not understand what you meant by "invoke the template and use the result as the body for the constructor."

Here is the code where i put my parameters to create the template:

Configuration config = new Configuration();
config.setTemplateLoader(new ClassTemplateLoader(getClass(), "/"));

Template template = config.getTemplate("helloworld.ftl");
// Build the data-model
Map<String, Object> data = new HashMap<String, Object>();
data.put("message", "Hello!! You have got a new approval mail!");
//List parsing
List<String> mailDetails = new ArrayList<String>();
mailDetails.add(fromAddress);
mailDetails.add(fromName);
mailDetails.add(toAddress);
mailDetails.add(toName);
mailDetails.add(subject);
mailDetails.add(body);

data.put("mailDetails", mailDetails);
// Console output
Writer out = new OutputStreamWriter(System.out);
template.process(data, out);
out.flush();

// File output
Writer file = new FileWriter (new File("D:\\mail.html"));
template.process(data, file);

In the above code I create the template for email alerts. I send the parameters and they are displayed in the file that gets created.

How do I send this new file which is created in .txt/.html format to the intended recipient as email?
How should I send the file as body to the mail?
I have shown my code to send mail in my precious comment. How should I pass the generated template as parameter while sending mail?
Seeya S Kudtarker, geändert vor 11 Jahren.

RE: Not able to locate Freemarker Template In Liferay

Regular Member Beiträge: 187 Beitrittsdatum: 16.01.13 Neueste Beiträge
Hey David,

I figured out the solution. Instead of writing the output to a file, I used StringWriter to write the template output to a String and then Passed this parameter as follows:

MailMessage mailMessage = new MailMessage(from, to, subject, finishedMessage, true);
In the above code finishedMessage is the String which carries output from the template.

Thanks David for your guidance with my module with mail notifications.

Regards

Seeya