JSP as a View
This Spring boot tutorial will help us to load JSP file as a view after hitting controller path.
e.g. Hitting http://localhost:8080/users url will load hello.jsp in response.
Below is the Spring Boot and JPA project code structure which we are going to build as part of this article. We will be making changes inside highlighted package and files.
As part of this article we will be using Spring tool kit and to download and setup Spring Tool Kit please click on this link.
Follow below steps to create our very first Spring application project.
- Open Spring Tool kit we installed here.
- Click on File in top navigation menu and than click on new. There will be an option Spring Starter Project start appearing once we click on create new option.
Below window will start appearing.
Provide below highlighted value for Group and Artifact for the project and click on Next.
3. In the below screen select open Web option and select Spring Web. Click on Finish will create Spring project.
4. Add below dependencies are required to work with Form, JPA and MySQL
tomcat-embed-jasper dependency is require to load jsp as part of controller response.
5. Create UserController.java class as mentioned in below screenshot tp handle http://localhost:8080/users request.
6. Create below hierarchy to place jsp files inside src/main/resources folder as shown below.
7. Place below content inside hello.jsp file.
8. Below code inside application.properties file is required to inform spring framework to locate view files and its extension.
Note: Complete path is not required to give as prefix /src/main/resources/META-INF/resources/WEB-INF/jsp/hello.jsp
Spring understands /src/main/resources/META-INF/resources as a default path.
9. Right click on the project and run it as Spring Boot App. Hit http://localhost:8080/users url with specified port number in console once server is up.
Note: It is returning “hello” as a content to display on the page in place of loading hello.jsp file. It is because of @ResponseBody annotation in UserController.java file. Remove @ResponseBody from UserController.java file as mentioned shown below.
10. Right click on the project and run it as Spring Boot App. Hit http://localhost:8080/users url with specified port number in console once server is up.
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.