Integration of Spring MVC and Mustache
Join the DZone community and get the full member experience.
Join For Freespring mvc, as it is known, is a web infrastructure system in the spring ecosystem.
spring mvc uses the internalresourceviewresolver class as default resolver, and this resolver class works with jsp view objects. but in spring mvc, there are also resolvers that can resolve the different view technology. here you will be introduced to the mustacheviewresolver class which resolves the mustache template architecture.
mustacheresolver does not exist as default in spring mvc libraries but every developer who wishes to do that is free to develop his own view resolver.
i want to leave the advantage / disadvantage comparison to you, precious developer, and i would like to explain the issue step by step.
step 1: prototyping spring mvc maven archetype.
the following maven command set is run on the command-line and then the prototype number, seen on console is selected.
> mvn archetype:generate
the above maven project prototype contains configuration of xml independent spring mvc. of course if it is desired, this project can be used easily in the form of xml-structured.
step 2: adding
mustacheviewresolver dependency
to pom.xml
.
this dependency is being developed by
sean scanlon
. this dependency uses jmustache library in the background.
<dependency> <groupid>com.github.sps.mustache</groupid> <artifactid>mustache-spring-view</artifactid> <version>1.1.1</version> </dependency>
step 3: adding mustacheviewresolver into mvcconfiguration constructor class.
this class is used instead of internalresourceviewresolver which is used as default in spring mvc.
@bean public viewresolver getviewresolver(resourceloader resourceloader) { mustacheviewresolver mustacheviewresolver = new mustacheviewresolver(); mustacheviewresolver.setprefix("/web-inf/views/"); mustacheviewresolver.setsuffix(".html"); mustacheviewresolver.setcache(false); mustacheviewresolver.setcontenttype("text/html;charset=utf-8"); mustachetemplateloader mustachetemplateloader = new mustachetemplateloader(); mustachetemplateloader.setresourceloader(resourceloader); mustacheviewresolver.settemplateloader(mustachetemplateloader); return mustacheviewresolver; }
step 4: creating book-model object.
this model class is read by spring mvc and then placed into mustache placeholders according to data field names.
public class book { private string name; private double price; private string author; private boolean visibility; public book(string name, double price, string author, boolean visibility) { this.name = name; this.price = price; this.author = author; this.visibility = visibility; } public book() { } public boolean getvisibility() { return visibility; } public void setvisibility(boolean visibility) { this.visibility = visibility; } public string getname() { return name; } public void setname(string name) { this.name = name; } public double getprice() { return price; } public void setprice(double price) { this.price = price; } public string getauthor() { return author; } public void setauthor(string author) { this.author = author; } }
step 5: assigning the list model data to the root ( / ) resource and then assigning this model data to the “home” page in homecontroller class.
spring mvc defragments the model and view components in the defined modelandview object and then html page which placeholders’ were filled is returned to the user.
@controller public class homecontroller { @requestmapping(value = "/") public modelandview test() { modelandview modelandview = new modelandview(); modelandview.setviewname("home"); list<book> booklist= arrays.aslist( new book("java ve yazılım tasarımı", 35d, "altuğ b. altıntaş", true), new book("java mimarisiyle kurumsal çözümler", 23d, "rahman usta", true), new book("veri yapıları ve algoritmalar", 40d, "rifat çölkesen", false), new book("veri yapıları ve algoritmalar", 40d, "rifat çölkesen", false), new book("veri yapıları ve algoritmalar", 40d, "rifat çölkesen", true), new book("mobil pazarlama - solomo", 15d, "kahraman-pelin arslan", false), new book("mobil pazarlama - solomo", 15d, "kahraman-pelin arslan", true)); modelandview.addobject("booklist", booklist); return modelandview; } }
step 6: creating the page “home(.html)” view.
neither an external xml element and a java code, nor a jsp component that is contrary to html standard can be seen when you look home.html page. so, absolute mustache placeholders that exist in absolute html and text format are the striking elements of this page.
<!doctype html> <html> <head> <title></title> <meta charset=utf-8> <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet"> </head> <body> <table class="table" border="1" bordercolor="grey" style="margin: 30px auto 0 auto;width: 500px;"> <thead> <tr> <th> <lablel>book name</lablel> </th> <th> <lablel>price</lablel> </th> <th> <lablel>author</lablel> </th> </tr> </thead> <tbody> {{#booklist}} {{#visibility}} <tr> <td> {{name}} </td> <td> {{price}} </td> <td> {{author}} </td> </tr> {{/visibility}} {{/booklist}} </tbody> </table> </body> </html>
step 7: running the project.
you can access to the application directory by” cd ” (change directory) command and then “ mvn jetty:run ” command set.
application access address: http://localhost:8080/
you can obtain the source code from here.
output:
hope to see you again, good bye…
Opinions expressed by DZone contributors are their own.
Trending
-
An Overview of Kubernetes Security Projects at KubeCon Europe 2023
-
Is Podman a Drop-in Replacement for Docker?
-
Never Use Credentials in a CI/CD Pipeline Again
-
4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
Comments