Sunday, December 2, 2012

Spring and Thymeleaf with JavaConfig (Part 5)

Review


In the previous section, we have discussed the Domain, Repository, Service, and Controller classes. In this section, we will build and run our app. We will also study how to import the project in Eclipse.

Table of Contents

Click on a link to jump to that section:
  1. Functional Specs
  2. Creating the View
    • HTML Mockup
    • Thymeleaf Integration
  3. JavaConfig
    • ApplicationContext.java
    • SpringDataConfig.java
    • ThymeleafConfig.java
    • ApplicationInitializer.java
  4. Layers
    • Domain
    • Service
    • Controller
  5. Running the application
    • Clone from GitHub
    • Create the Database
    • Run with Maven and Tomcat 7
    • Run with Maven and Jetty 8
    • Import to Eclipse
    • Validate with W3C

Running the Application


Clone from GitHub

To clone from GitHub, follow these instructions:
  1. Open a Git terminal
  2. Enter the following command:
    git clone https://github.com/krams915/spring-thymeleaf-javaconfig.git
    This will clone the JavaConfig-based application.

    If you prefer the XML-based version,
    git clone https://github.com/krams915/spring-thymeleaf-xmlconfig.git

    Remember:

    There are two versions of the application: a JavaConfig-based and an XML config-based app. Both versions are identical in their feature set.

Create the Database

  1. Run MySQL
  2. Create a new database:
    spring_thymeleaf_tutorial
  3. Import the spring_thymeleaf_tutorial.sql from the src/main/resources path

Run with Maven and Tomcat 7

Ensure Maven is installed first, and you have created the MySQL database
  1. Open a terminal
  2. Browse to the directory where you've cloned the project
  3. Enter the following command:
    mvn tomcat7:run
  4. You should see the following output:
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] -------------------------------------------------------------
    [INFO] Building spring-thymeleaf-tutorial Maven Webapp 0.0.1-SNAPSHOT
    [INFO] -------------------------------------------------------------
    [INFO] 
    [INFO] >>> tomcat7-maven-plugin:2.0:run (default-cli) @ spring-thymeleaf-tutorial >>>
    ...
    ...
    ...
    Dec 2, 2012 5:38:31 PM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-bio-8080"]
    Dec 2, 2012 5:38:31 PM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service Tomcat
    Dec 2, 2012 5:38:31 PM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/7.0.30
    Dec 2, 2012 5:38:42 PM org.apache.catalina.core.ApplicationContext log
    INFO: Spring WebApplicationInitializers detected on classpath: [org.krams.config.ApplicationInitializer@1cc33893]
    Dec 2, 2012 5:38:43 PM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring root WebApplicationContext
    Dec 2, 2012 5:38:52 PM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet 'dispatcher'
    Dec 2, 2012 5:38:52 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-bio-8080"]
    
  5. Open a browser
  6. Visit the following URL:
    http://localhost:8080/spring-thymeleaf-tutorial/users

Run with Maven and Jetty 8

Ensure Maven is installed first, and you have created the MySQL database.
  1. Open a terminal
  2. Browse to the directory where you've cloned the project
  3. Enter the following command:
    mvn jetty:run
  4. You should see the following output:
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] -------------------------------------------------------------
    [INFO] Building spring-thymeleaf-tutorial Maven Webapp 0.0.1-SNAPSHOT
    [INFO] -------------------------------------------------------------
    [INFO] 
    [INFO] >>> jetty-maven-plugin:8.1.5.v20120716:run (default-cli) @ spring-thymeleaf-tutorial >>>
    ...
    ...
    ...
    2012-12-02 17:42:56.556:INFO:/spring-thymeleaf-tutorial:Initializing Spring FrameworkServlet 'dispatcher'
    2012-12-02 17:42:56.760:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
    [INFO] Started Jetty Server
    
  5. Open a browser
  6. Visit the following URL:
    http://localhost:8080/spring-thymeleaf-tutorial/users

Import to Eclipse

Ensure Maven is installed first
  1. Open a terminal
  2. Enter the following command:
    mvn eclipse:eclipse -Dwtpversion=2.0
  3. You should see the following output:
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] -------------------------------------------------------------
    [INFO] Building spring-thymeleaf-tutorial Maven Webapp 0.0.1-SNAPSHOT
    [INFO] -------------------------------------------------------------
    [INFO] 
    [INFO] >>> maven-eclipse-plugin:2.9:eclipse (default-cli) @ spring-thymeleaf-tutorial >>>
    ...
    ...
    ... 
    [INFO] -------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] -------------------------------------------------------------
    [INFO] Total time: 9.356s
    [INFO] Finished at: Sun Dec 02 17:46:43 PHT 2012
    [INFO] Final Memory: 15M/81M
    [INFO] -------------------------------------------------------------
    

    This command will add the following files to your project:
    .classpath
    .project
    .settings
    target
    You may have to enable "show hidden files" in your file explorer to view them.
  4. Run Eclipse and import the application as Maven project

Validate with W3C Markup Validation Service

One of the promises of Thymeleaf is it produces valid HTML pages. Let's test that out using W3C validation service.
  1. Run the application
  2. Open a browser
  3. Visit the following URL:
    http://localhost:8080/spring-thymeleaf-tutorial/users
  4. View the HTML source (right-click or go to menu)
  5. Copy the HTML source
  6. Open W3C Markup Validation Service at http://validator.w3.org/#validate_by_input
  7. Paste the HTML source and wait for the validation result

    You should see a similar output:

Conclusion


We've have completed our Spring MVC application with Thymeleaf as our template engine . We've studied how to convert our HTML mockup into a Thymeleaf template that validates with W3C validator service. We've also discussed how to create a JavaConfig-based application. As a bonus, we've also provided an XML-based application.

I hope you've enjoyed this tutorial. Don't forget to check my other tutorials at the Tutorials section.

Revision History

Revision Date Description
1 Dec 2 2012 Uploaded tutorial and GitHub repositories
2 Dec 3 2012 Updated table of contents
3 Dec 12 2012 Updated Part 2

StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring and Thymeleaf with JavaConfig (Part 5) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

16 comments:

  1. I've followed your other tutorials and gotten a lot out of them. This one seemed to be pretty utilitarian based, though. There was no presentation of the DTO objects and how they interact with Thymeleaf. I am assuming the objects are built corresponding with the data elements shown in the thymeleaf template. Are additional annotations needed there? You also did not touch on the jackson JSON-related annotations in the entity classes. Are these needed for thymeleaf?

    ReplyDelete
    Replies
    1. Chris, I don't know what you meant by being "utilitarian". I'm interested to know what you actually meant by that. I admit the DTO wasn't mention and how they are used in the Thymeleaf template because I assumed it was obvious when we're referring to Model attributes in the context of an MVC application. Also, on my previous articles, no one has asked about them, so I thought it was obvious. The existence of the JSON annotation, i.e @JsonManagedReference was partly accidental and partly intentional. Intentional because I was planning to make this as a continuation of my previous guide "Spring Data Rest Tutorial" where I had to use such JSON annotation (which was explained in that guide). Accidental because I was planning to remove it anyway but forgot to do it because when running the project it didn't affect the application, so I just said, "I can delete it anyway later and push the updates to GitHub". Anyway, give me some time this week, and I will clarify those points and explain further the DTO relationship with the Thymeleaf template. Also, I will remove those extraneous JSON annotations. I just need to finish some projects first.

      Delete
    2. Thanks for the reply, Mark. By utilitarian I meant something along the lines of it "it's good and working, but has less explanation/less of a teaching aspect to it than some of your other tutorials". Might just have been my own bias, but it just seemed there was a bit more voodoo/hand-waving, but that might just be the nature of thymeleaf rather than a shortcoming of the tutorial.

      Thanks again for your tutorials and taking the time to work on these for us!

      Delete
  2. Fantastic tutorial ... I've purchased several books on Spring 3 and the examples either don't work or are too localized to be platform / tooling agnostic ...

    You should write a book on spring ... I'd buy it for sure.

    ReplyDelete
  3. Small errata ---

    change spring.properties to ...

    app.jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_thymeleaf_tutorial

    to work everywhere ;-)

    ReplyDelete



  4. process


    target/generated-sources/java
    com.mysema.query.apt.jpa.JPAAnnotationProcessor






    Hi
    Mark
    I am not able to run pom file is showing error on tag.
    I am new on maven also Please tell me what is this for and why is showing error

    ReplyDelete
  5. Add before and after ,
    that will work!

    ReplyDelete
  6. Thanks for your tutorials Mark.
    I'm a follower of you.

    ReplyDelete
  7. One of the best Spring blogger. Thx for sharing.

    ReplyDelete
  8. running when added this


    org.apache.maven.plugins
    maven-war-plugin
    2.1.1

    false




    Thanks A lot

    ReplyDelete
  9. I am new to the Spring framework and although I find it amazing, I seem to hit a wall..
    I am working with this tutorial (https://github.com/spring-guides/tut-web/tree/master/6)
    and I want to add localized strings. I see that your tutorial and the one I am working with have similar setup (Jconfig). But my localized strings won't work.. I've tried placing the messages_en.properties file literaly everywhere in my project with no luck.
    Oh I am deploying my war on a glassfish server.
    I would appreciate any help..

    ReplyDelete
  10. Really one of the best blogs on the net about spring and other techs!! It is a pity that there is no activity since a while...

    ReplyDelete
  11. Did anyone got the project to run via mvn tomcat7:run? I hit a context initialization error. Any advice?

    ReplyDelete
  12. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

    ReplyDelete