Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using a JSP view


In this recipe, you'll learn how to render and return a JSP view after the execution of a controller method.

How to do it…

Here are the steps to create a JSP view:

  1. Add the Maven dependency for JSTL in pom.xml:

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
  2. Add a JSP view resolver to the Spring configuration class:

    @Bean
    public ViewResolver jspViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
  3. Create a controller method:

    @RequestMapping("/user/list")
    public void userList() {
      ...
    }
  4. Create the /WEB-INF/jsp/user/list.jsp JSP:

    <html>
    <body>
      There are many users.
    </body>
    </html>

How it works…

The controller method path is /user/list. Using the JSP view resolver,...