3

I have the packages, but I am not sure how to use it. Do I just use them as I would .jsp files?

I have tried something like this:

test.hbs

<p>{{message}}</p>

in my controller:

private static class M {
    private final String message;

    public M(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

@RequestMapping("/test")
public ModelAndView testView() {
    ModelAndView mav = new ModelAndView("test.hbs");
    M m = new M("Hello, world!");

    mav.addObject("m", m);

    return mav;
}

I am getting the error: javax.servlet.ServletException: Could not resolve view with name 'test.hbs' in servlet with name 'dispatcher'

I have put test.hbs normally in /WEB-INF/views/test.hbs. If I put any .jsp file there, it works. But from some reason .hbs doesnt work. Any ideas?

zwiebl
  • 685
  • 2
  • 11
  • 24

1 Answers1

12

Spring MVC does not have out-of-the-box support for Handlebars (see official documentation for the list of supported view technologies).

Having said that, it is straightforward to add support for any JVM based view technology to Spring MVC. At a high level, this requires implementing a org.springframework.web.servlet.View and its corresponding org.springframework.web.servlet.ViewResolver.

Luckily, there already exists an open source project that provides this integration. The following steps can be followed to integrate this project in an existing Spring MVC application.

Step 1: Add the library to the build system (assuming Maven)

<dependency>
  <groupId>com.github.jknack</groupId>
  <artifactId>handlebars-springmvc</artifactId>
  <version>4.0.6</version>
</dependency>

Step 2: Configure a Handlebars ViewResolver for the Spring MVC application in dispatcher-servlet.xml (or equivalent Java configuration)

<bean class="com.github.jknack.handlebars.springmvc.HandlebarsViewResolver">
  <property name="prefix" value="/WEB-INF/views/"/>
  <property name="suffix" value=".hbs"/>
</bean>

Step 3: Add Handlebars views to the application

Given the configuration above, Handlebars views should be added under the /WEB-INF/views/ folder.

Step 4: Load Handlebars views

@RequestMapping("/test")
public ModelAndView testView() {
  ModelAndView mav = new ModelAndView("test");
  M m = new M("Hello, world!");

  mav.addObject("m", m);

  return mav;
}

Note that the view name should not contain .hbs as the suffix has already been added to the configuration.

manish
  • 19,695
  • 5
  • 67
  • 91
  • Hey, thanks for the explanation - however I need that in Spring MVC 4 (with annotations and not the XML). I know how to create such bean, but how do you add dependency? – zwiebl Dec 11 '16 at 13:22
  • 1
    The dependency needs to be added to your build file (Maven or Gradle). The code will work with Spring 3/4 alike. XML and annotations based configurations are equivalent so just translate the XML snippets to Java. If you don't know how, consult the official Spring documentation which shows side-by-side examples for both and learn from that. – manish Dec 11 '16 at 14:33
  • For Spring 5 the list of supported view technologies is here: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-view – Paul Dec 04 '17 at 18:19