{"id":2628,"date":"2016-05-27T23:26:36","date_gmt":"2016-05-28T03:26:36","guid":{"rendered":"http:\/\/springframework.guru\/?p=2628"},"modified":"2019-05-11T14:10:26","modified_gmt":"2019-05-11T18:10:26","slug":"accessing-database-thymeleaf-template","status":"publish","type":"post","link":"https:\/\/springframework.guru\/accessing-database-thymeleaf-template\/","title":{"rendered":"Accessing the Database from a Thymeleaf Template"},"content":{"rendered":"<p>This week, I had a student ask me how they could use a Java function to access the database from a Thymeleaf template. Thymeleaf is a modern templating engine. And accessing the database directly from Thymeleaf is certainly something you could do. But is it really something you want to do?<\/p>\n<p>I&#8217;ve worked for a couple major retailers which had JSP templates accessing the database with static Java methods. It was not a good thing.<\/p>\n<p>When you&#8217;re using Thymeleaf with Spring MVC, you&#8217;re following the MVC design pattern. MVC is Model, View, Controller. The Thymeleaf template is the &#8216;View&#8217;. The Spring controller will return a model to the Thymeleaf template engine. The model should be a data structure. Nothing more.<\/p>\n<p><a href=\"http:\/\/springframework.guru\/spring-courses\/spring-mvc-online-course\/\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2629 size-full\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2016\/05\/ThumbSpringMVC_.png\" alt=\"Spring MVC Course\" width=\"506\" height=\"290\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2016\/05\/ThumbSpringMVC_.png 506w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/05\/ThumbSpringMVC_-300x172.png 300w\" sizes=\"(max-width: 506px) 100vw, 506px\" \/><\/a>The MVC design pattern is elegant and widely accepted design pattern. I personally like how the MVC design pattern is in alignment with the <a href=\"http:\/\/springframework.guru\/solid-principles-object-oriented-programming\/\">SOLID Principles of Object Oriented Design<\/a>.<\/p>\n<p>If you choose to access the database directly from Thymeleaf, you break the separation of concerns. You&#8217;re violating the SOLID principles of Object Oriented Design.<\/p>\n<p>I took the time to record a brief video about why accessing the database from Thymeleaf is a very bad idea. You can see the video here on YouTube.<\/p>\n<div class=\"lyte-wrapper\" style=\"width:640px;max-width:100%;margin:5px auto;\"><div class=\"lyMe\" id=\"WYL_qFNuIw11_Ko\"><div id=\"lyte_qFNuIw11_Ko\" data-src=\"\/\/i.ytimg.com\/vi\/qFNuIw11_Ko\/hqdefault.jpg\" class=\"pL\"><div class=\"tC\"><div class=\"tT\"><\/div><\/div><div class=\"play\"><\/div><div class=\"ctrl\"><div class=\"Lctrl\"><\/div><div class=\"Rctrl\"><\/div><\/div><\/div><noscript><a href=\"https:\/\/youtu.be\/qFNuIw11_Ko\" rel=\"nofollow\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i.ytimg.com\/vi\/qFNuIw11_Ko\/0.jpg\" alt=\"\" width=\"640\" height=\"340\" \/><br \/>Watch this video on YouTube<\/a><\/noscript><\/div><\/div><div class=\"lL\" style=\"max-width:100%;width:640px;margin:5px auto;\"><\/div>\n<p>While I could certainly show you how to access the database from a Thymeleaf template, I won&#8217;t. I hope you watch the above video to see why this is such a poor programming practice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week, I had a student ask me how they could use a Java function to access the database from a Thymeleaf template. Thymeleaf is a modern templating engine. And accessing the database directly from Thymeleaf is certainly something you could do. But is it really something you want to do? I&#8217;ve worked for a [&hellip;]<a href=\"https:\/\/springframework.guru\/accessing-database-thymeleaf-template\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":4590,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Accessing the Database from a Thymeleaf Template #antipattern","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2628","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"jt","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/03\/Banner560x292_06aweb.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-Go","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2628"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=2628"}],"version-history":[{"count":1,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2628\/revisions"}],"predecessor-version":[{"id":2630,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2628\/revisions\/2630"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/4590"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=2628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=2628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=2628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}