User Authentication
This Spring boot tutorial will help us to load JSP file as a view after hitting controller path.
e.g. Hitting http://localhost:8080/login url will load form.jsp in response.
Below is the complete flow chart of application. User will land on login screen and on successful login it will redirect to welcome page.
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/login request.
@PostMapping annotation to handle POST request.
@GetMapping annotation to handle GET request.
6. Create below hierarchy to place jsp files inside src/main/resources folder as shown below.
7. Create form.jsp file and place below content inside the same file.
8. Create welcome.jsp file and place below content inside the same file.
9. 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/form.jsp
Spring understands /src/main/resources/META-INF/resources as a default path.
10. Right click on the project and run it as Spring Boot App. Hit http://localhost:8080/login url with specified port number in console once server is up.
Note: According to below code snippet written in UserController.java. It will load welcome.jsp if name and password gets match. It will load login page back if name and password didn’t get match.
Fill name and password as admin. Click on submit will load below welcome page:
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.