DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

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

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
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Integration of Spring MVC and Mustache

Integration of Spring MVC and Mustache

Rahman Usta user avatar by
Rahman Usta
·
Aug. 07, 13 · Interview
Like (0)
Save
Tweet
Share
4.69K Views

Join the DZone community and get the full member experience.

Join For Free

spring 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

spring-mvc-archetype

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:

mvc-mustache

hope to see you again, good bye…

Spring Framework Mustache (template system) Integration

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: