First Project

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

First Project

Sekhar Subramanian
It would be of great help if you could share a step by step procedure on how to deploy thymeleaf applications.
I have been able to create and configure template engine but however, i am unable to understand how to deploy the application on a web server. I am using Eclipse IDE, tomcat 7.0 and not using Spring framework. Just a plain Dynamic web module, with the thymeleaf jars and servlet jars

Thanks in Advance
Sekhar
Reply | Threaded
Open this post in threaded view
|

Re: First Project

Emanuel
Administrator
I'm guessing you've got the WTP plugin as part of your Eclipse installation already since you were able to create a dynamic web module project.  If so, then I think a tutorial on how to use the WTP plugin to deploy to Tomcat would be best.  Most of the ones I found were a bit out of date, but this one from 2011 looks OK: http://www.vogella.com/articles/EclipseWTP/article.html  Particularly chapters 4 and 5.4 which shows how to configure Eclipse to use a Tomcat installation, then to run a project on that Tomcat installation.
Reply | Threaded
Open this post in threaded view
|

Re: First Project

Sekhar Subramanian
Thank you for writing, I Have been able to deploy, JSP pages on web server even before i posted this article. However i was facing difficulty in deploying those that had thymeleaf. When i run my pages in the similar fashion, i was faced with the HTTP status 404 error. This does not happen to me when i am deploying my JSP pages using the same server. Can you suggest? I have not thoroughly understood, how thymeleaf is executed as in how the templateengine gets run.
Reply | Threaded
Open this post in threaded view
|

Re: First Project

Emanuel
Administrator
This post was updated on .
Ah, sorry I misunderstood your question.  If you're coming from normal JSPs, then at the end of your servlets you may have done something like forwarding to a page doing this:

request.getRequestDispatcher("your-page.html").forward(request, response);

The equivalent in Thymeleaf is this:

templateEngine.process("your-page.html", context, response.getWriter());

Where 'templateEngine' is the Thymeleaf TemplateEngine instance that you created earlier (probably a singleton or servlet instance field since the template engine is expensive to construct), and 'context' is a Thymeleaf WebContext object that just holds any variables or objects you want to pass along to your pages.  Here's an example servlet:

private TemplateEngine templateEngine;

/**
 * Initialize the Thymeleaf template engine
 */
public void init() {

  ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
  templateResolver.setTemplateMode("HTML5");

  templateEngine = new TemplateEngine();
  templateEngine.setTemplateResolver(templateResolver);
}

/**
 * Process a request
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

  WebContext context = new WebContext(request, getServletContext());
  context.setVariable("test", "This is a string that will be mapped to the variable 'test'");

  // do your other processing

  templateEngine.process("/test.html", context, response.getWriter());
}

I think this information is in the Using Thymeleaf documentation (http://www.thymeleaf.org/doc/Tutorial%20-%20Using%20Thymeleaf%2020120717.pdf), although I notice it is spread out across chapters 2.2 and 3.1.
Reply | Threaded
Open this post in threaded view
|

Re: First Project

Sekhar Subramanian
Hey thanks for your help. I am somehow still unable to get my environment setup correctly. Can you give me an to these questions

1. What do i need in order to be able to use thymeleaf (as in softwares, plugins etc.)
2. If we are talking about Eclipse WTP, without any added plugins, then can you explain how i would create a web project and run thymeleaf templates.

Am really a beginner and i have only used JSP so far. Completely unable to figure out how thymeleaf is setup, although i am able to understand how to use it in my web page.

Your help will be greatly appreciated.

Thanks in advance
Sekhar Subramanian
Reply | Threaded
Open this post in threaded view
|

Re: First Project

Emanuel
Administrator
OK, well here's a step-by-step of what I did to create a new web project that uses Thymeleaf, using Eclipse and WTP.  I'll also attach a ZIP file of the project I put together using these instructions, so you can just import it straight into your Eclipse workspace: thymeleaf-example.zip

1) Create a new Dynamic Web Project project.  You can name it anything you like, Target Runtime of the Tomcat server that you've installed, Dynamic web module version of 3.0, Configuration of Default Configuration for Apache Tomcat.

2) Click Next, this will bring you to the screen where you can choose your Java source directory and where the compiled class files will go.  The defaults are fine for now, so Click Next again.

3) On this screen you get to pick the context root (defaults to the name of your project) and directory where you will store your HTML pages.  The defaults are fine for now too.  Tick the 'Generate web.xml deployment descriptor' box.  Click Finish.

4) You should now have a very basic, very empty dynamic web project.  To install Thymeleaf, go to the Thymeleaf Downloads page and download the latest version of Thymeleaf (2.0.11 as of writing).

5) The ZIP file contains the JARs that you'll need.  Unzip that file somewhere, then copy thymeleaf-2.0.11/dist/thymeleaf-2.0.11.jar into the WEB-INF/lib directory of your dynamic web project.  Now copy every JAR inside the thymeleaf-2.0.11/lib directory to your WEB-INF/lib directory.

6) Create a new servlet, copying all of the code in my previous post inside the servlet class.  Fix up any compilation issues like adding import statements.

7) Add a @WebServlet() annotation to your servlet so that this servlet knows to handle any request to the URL you specify.  For example, I made my servlet handle requests to "test" (full URL will be: http://localhost:8080/<your-context-root>/test ) by adding this annotation:

@WebServlet("/test")

8) Notice the line at the end of your servlet which tells Thymeleaf to process the test.html page, so create a new page called test.html inside your web content directory.  Let's make it really simple, so put the following code into it:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<body><nabble_a href="thymeleaf-example.zip">thymeleaf-example.zip</nabble_a>
  <p th:text="${test}">Test paragraph</p>
</body>
</html>

9) You should now have a very basic dynamic web project with a Thymeleaf-enabled servlet, and an HTML page using some Thymeleaf in it as well.  Add this project to your Tomcat server and start the server.

10) Visit the URL of your servlet in your browser, eg: http://localhost:8080/<your-context-root>/test  If all went well, you should now see your page :)
Reply | Threaded
Open this post in threaded view
|

Re: First Project

Sekhar Subramanian
Thanks a lot. :) Its working . :)