Sunday, December 2, 2012

Spring and Thymeleaf with JavaConfig (Part 1)

In this tutorial, we will create a CRUD application based on Spring MVC 3.x and Spring Data JPA. We will utilize JavaConfig instead of XML to configure our application. For the view layer, we will use Thymeleaf as our template engine instead of JSP to process our html pages.

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

Dependencies

These are the main Maven dependencies:
  • Spring 3.2.0.RC1
  • Spring Data JPA 1.2.0.RELEASE
  • Thymeleaf 2.0.14
  • Hibernate 3.6.3.Final
  • See pom.xml for full details

Required Tools

These are the minimum required tools:
  • Git
  • Maven 3.0.4
  • MySQL
  • Eclipse IDE or SpringSource Tool Suite (STS)

GitHub Repository

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

Functional Specs


Our application's requirements are quite straightforward:
  • Create a simple form to manage user information
  • Provide the following fields: first name, last name, username, role
  • Username must be unique
  • Provide CRUD operations
  • Provide table to view all users

Here's our Use Case diagram:


[User]-(Add)
[User]-(View)
[User]-(Update)
[User]-(Delete)

//http://yuml.me/

Here's a screenshot of our working application:


Next

In the next section, we will focus on the view layer. We'll start writing the HTML mockup template; then we'll integrate it with Thymeleaf. Click here to proceed.
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring and Thymeleaf with JavaConfig (Part 1) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

82 comments:

  1. Thanks for the nice and comprehensive example.
    If possible, please give a sample for Java Config based Spring Security 3.2 or let me know, how to configure Spring Security 3 using Java annotation.

    Thanks in advanced....

    ReplyDelete
    Replies
    1. Rahi, you're welcome. I'm actually on the process of writing a guide that covers the latest Spring Security in a Spring Social app. It uses JavaConfig and no web.xml. Unfortunately, the declaration of the rules (i.e spring-security.xml) will remain in XML because it's easier and less verbose than with JavaConfig.

      Delete
    2. Yeah...Thanks a lot Mark..Your Spring Social example is very comprehensive and helpful.

      Delete
  2. Nice example. While playing with it, and incorporating this pagination example

    http://www.jiwhiz.com/post/2013/2/Implement_Bootstrap_Pagination_With_SpringData_And_Thymeleaf

    I noticed that the users.html template would not update after saving changes when running in STS tcServer (integrated into STS).

    For development purposes, you might consider adding "resolver.setCacheable(false);" to the ServletContextTemplateResolver bean definition in your ThymeleafConfig class. The value (true or false) could be externalized to a properties file, so that for dev it would be false; for production, set the property to true.

    If you are developing and running in STS and you make changes to the template, you will see the update almost immediately -- much better than restarting the server.

    ReplyDelete
  3. Thanks for sharing fabulous information. It' s my pleasure to read it.I have also bookmarked you for checking out new posts. by HRM 531 Week 1

    ReplyDelete
  4. how to display the image using this javaconfig

    ReplyDelete
  5. thank u for giving this best information..we are offering the best spring online training

    ReplyDelete
  6. generate OAuth keys for Facebook and Twitter (Online Java Training) Generate OAuth keys Java Training in Chennai Consumer key and Consumer secret values: J2EE Training in Chennai

    ReplyDelete
  7. Hey amazing tutorial!
    However I would strongly suggest to update it to Spring 4 as Spring 3 is obsolete now.
    Thanks
    David Mayer @ https://www.springmockexams.com/

    ReplyDelete
  8. 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
  9. nice blog. thanks for sharing valuable information. It’s really good.Any one looking software courses nareshit is the best offline and online software training institute in Hyderabad and Chennai.

    ReplyDelete
  10. Thanks for your article .its very useful for everyone.Spring and java config was really useful. best aws training in chennai | aws course fees details

    ReplyDelete
  11. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    core java training in Electronic City

    Hibernate Training in electronic city

    spring training in electronic city

    java j2ee training in electronic city

    ReplyDelete
  12. Nice blog. thanks for sharing valuable information. It’s really good.Any one looking software courses. hit the below links
    BEST SAS BI TRAINING
    BEST PTC WINDCHILL TRAINING
    BEST ANAPLAN TRAINING

    ReplyDelete
  13. I am Here to Get Learn Good Stuff About Windows Azure, Thanks For Sharing Windows Azure.Windows Azure Training in Bangalore

    ReplyDelete
  14. Its help me to improve my knowledge and skills also.im really satisfied in this Python session.Python Training in Bangalore

    ReplyDelete
  15. Wow it is really wonderful and awesome thus it is veWow, it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot.Power BI Training in Bangalore

    ReplyDelete
  16. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information.MSBI Training in Bangalore

    ReplyDelete
  17. It is very good and useful for students and developer.Learned a lot of new things from your post Good creation,thanks for give a good information at Pega.Pega Training in Bangalore

    ReplyDelete
  18. Thank you for valuable information.I am privilaged to read this post.best SAP SD training in bangalore

    ReplyDelete
  19. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.best SAP MM training in bangalore

    ReplyDelete
  20. I really enjoy reading this article.Hope that you would do great in upcoming time.A perfect post.Thanks for sharing.best SAP FICO training in bangalore

    ReplyDelete
  21. I must appreciate you for providing such a valuable content for us. This is one amazing piece of article.Helped a lot in increasing my knowledge.best SAP ABAP training in bangalore

    ReplyDelete
  22. Thanks For sharing a nice post about Oracle Apps Training Course.It is very helpful and Oracle Apps useful for us.Oracle Apps training in bangalore

    ReplyDelete
  23. Excellent information with unique content and it is very useful to know about the oracle 11g DBA.oracle 11g training in bangalore

    ReplyDelete
  24. It has been great for me to read such great information about devops.devops training

    ReplyDelete
  25. Excellent information with unique content and it is very useful to know about the information.salesforce training in bangalore

    ReplyDelete
  26. I think there is a need to look for some more information and resources about Informatica to study more about its crucial aspects.Amazon web services Training in Bangalore

    ReplyDelete
  27. It is really explainable very well and i got more information from your site.Very much useful for me to understand many concepts and helped me a lot.SAP ABAP Training in Bangalore

    ReplyDelete
  28. Congratulations! This is the great things. Thanks to giving the time to share such a nice information.sap s4 hana simple logistics training institutes in bangalore

    ReplyDelete
  29. The Information which you provided is very much useful for Agile Training Learners. Thank You for Sharing Valuable Information.sap s4 hana simple logistics training institutes in bangalore

    ReplyDelete
  30. The Information which you provided is very much useful for Agile Training Learners. Thank You for Sharing Valuable Information.sap s4 hana simple finance training institutes in bangalore

    ReplyDelete
  31. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    microservices online training
    best microservices online training
    top microservices online training

    ReplyDelete
  32. Nice blog..! I really loved reading through this article. Thanks for sharing.You done a great job.SAP Training in Bangalore

    ReplyDelete
  33. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    microservices online training

    ReplyDelete
  34. Thanks for sharing amazing information.Gain the knowledge and hands-on experience. sap hana training in bangalore


    ReplyDelete
  35. This is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me. sap hr training in bangalore

    ReplyDelete
  36. Thank you for sharing .The data that you provided in the blog is informative and effective. sap mm training in bangalore

    ReplyDelete
  37. Thanks for this. I really like what you've posted here and wish you the best of luck with this blog and thanks for sharing.sap fico training in bangalore

    ReplyDelete
  38. I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge. sap abap training in bangalore

    ReplyDelete
  39. Its really helpful for the users of this site. I am also searching about these type of sites now a days. So your site really helps me for searching the new and great stuff.

    aws training in bangalore

    aws courses in bangalore

    aws classes in bangalore

    aws training institute in bangalore

    aws course syllabus

    best aws training

    aws training centers

    ReplyDelete
  40. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information.

    mulesoft training in bangalore

    mulesoft courses in bangalore

    mulesoft classes in bangalore

    mulesoft training institute in bangalore

    mulesoft course syllabus

    best mulesoft training

    mulesoft training centers

    ReplyDelete
  41. Hello Admin!

    Thanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuffs like this. If you are looking for the Advertising Agency in Chennai | Printing in Chennai , Visit Inoventic Creative Agency Today..

    ReplyDelete
  42. Top Chauffeur service in Melbourne
    Whether you need a last minute chauffeur car or a planned vehicle for your outing, book with us and get served on time. With well-mannered chauffeurs and finest vehicles, we arrange to pick and drop our customers with great punctuality. A hassle-free traveling experience waits at Silver Executive Cab for every customer

    ReplyDelete
  43. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    servicenow online training
    best servicenow online training
    top servicenow online training

    ReplyDelete
  44. I cannot thank lemeridian funding service enough and letting people know how grateful I am for all the assistance that you and your team staff have provided and I look forward to recommending friends and family should they need financial advice or assistance @ 1,9% Rate for Business Loan .Via Contact : . lfdsloans@lemeridianfds.com / lfdsloans@outlook.com. WhatsApp...+ 19893943740. Keep up the great work.
    Thanks, Busarakham.

    ReplyDelete