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:
Add the Maven dependency for JSTL in
pom.xml
:<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
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; }
Create a controller method:
@RequestMapping("/user/list") public void userList() { ... }
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,...