Creating a Web Application With Spring Boot
Learn how to use the Spring Boot framework to easily and quickly create web applications coded in Java. Read on for more!
Join the DZone community and get the full member experience.
Join For FreeThis guide will help you create a simple web application with Spring Boot. We will develop a simple application with login functionality as well as the functionality to show a list of todos. We will follow the MVC pattern using JSP as the view.
Overview of the Web Application
We will build a todo list page (un-formatted) with basic login features.
Files
The following screenshot shows an eclipse project with all the files we will create.
A brief overview of all files:
LoginService, TodoService
- Contains the business logic. LoginService has simple hard coded validation for user ids and passwords. TodoService contains a service method to retrieve a list of todos.login.jsp, welcome.jsp, list-todos.jsp
- The name clearly explains what each of these views contains.LoginController, TodoController
- Act as Controllers in the MVC pattern. LoginController has a little bit of flow. If the user enters a valid user id and password combination, they would be redirected to the welcome page. Otherwise, the login page will be shown with the error message.pom.xml
- Important dependencies are Spring Boot Starter Web and tomcat-embed-jasper. We will talk about these later.application.properties
- This is typically used to configure frameworks in Spring Boot. In this example, we would configure our view resolver in application.properties.
Screenshots of the Application
Login Page
Login Page if user enters invalid user id and password
Welcome Page
List Todos Page
Bootstrapping a Web Application With Spring Initializr
Creating a Web application with Spring Initializr is a cake walk. We will use Spring Web MVC as our web framework.
Spring Initializr http://start.spring.io/ is great tool to bootstrap your Spring Boot projects.
As shown in the image above, we need to perform the following steps:
- Launch Spring Initializr and:
- Choose
com.in28minutes.springboot
as the Group. - Choose
student-services
as the Artifact. - Choose from the following dependencies:
- Web
- Actuator
- DevTools
- Choose
- Click Generate Project.
- Import the project into Eclipse.
Project Dependencies
Spring Boot Starter Web provides all the dependencies and the auto configuration needed to develop web applications. We should use the first dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
We want to use JSP as the view. Default embedded servlet container for Spring Boot Starter Web is tomcat. To enable support for JSP’s, we would need to add a dependency on tomcat-embed-jasper.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
Spring Boot Starter Web Dependencies
Following screenshot shows the different dependencies that are added into our application because of Spring Boot Starter Web.
Dependencies can be classified into:
- Spring - core, beans, context, app
- Web MVC - (Spring MVC)
- Jackson - for JSON Binding
- Validation - Hibernate Validator, Validation API
- Embedded Servlet Container - Tomcat
- Logging - logback, slf4j
Any typical web application would use all these dependencies. Spring Boot Starter Web comes pre-packaged with these. As a developer, I would not need to worry about either these dependencies or their compatible versions.
Auto Configuration
Spring Boot Starter Web auto configures the basic things we needed to get started. To understand the features Spring Boot Starter Web brings in, let's run StudentServicesApplication.java as a Java Application and review the log.
Mapping servlet: 'dispatcherServlet' to [/]
Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Spring Boot Starter Web auto-configures:
- Dispatcher Servlet.
- Error Page.
- Web Jars to manage your static dependencies.
- Embedded Servlet Container - Tomcat is the default.
Configuring a View Resolver
We would have our jsp’s in /WEB-INF/jsp/. We would need to configure the view resolver with the prefix and suffix.
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
Login Controller
public String showLoginPage(ModelMap model)
: Mapped to the \login Get Method, this method shows the login page.@Autowired LoginService service
: LoginService has the validation logic.showWelcomePage(ModelMap model, @RequestParam String name, @RequestParam String password
Mapped to the \login Post Method, this method validates the user id and password. Redirects to welcome page if login is successful.
package com.in28minutes.springboot.web.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.in28minutes.springboot.web.service.LoginService;
@Controller
@SessionAttributes("name")
public class LoginController {
@Autowired
LoginService service;
@RequestMapping(value="/login", method = RequestMethod.GET)
public String showLoginPage(ModelMap model){
return "login";
}
@RequestMapping(value="/login", method = RequestMethod.POST)
public String showWelcomePage(ModelMap model, @RequestParam String name, @RequestParam String password){
boolean isValidUser = service.validateUser(name, password);
if (!isValidUser) {
model.put("errorMessage", "Invalid Credentials");
return "login";
}
model.put("name", name);
model.put("password", password);
return "welcome";
}
}
Login Service
Has the basic logic for authentication. Hardcoded business logic.
package com.in28minutes.springboot.web.service;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class LoginService {
public boolean validateUser(String userid, String password) {
// in28minutes, dummy
return userid.equalsIgnoreCase("in28minutes")
&& password.equalsIgnoreCase("dummy");
}
}
Login View - JSP
Simple login page with user id and password form fields. If an error message is populated into the model, ${errorMessage}
will show the authentication failure error message.
<html>
<head>
<title>First Web Application</title>
</head>
<body>
<font color="red">${errorMessage}</font>
<form method="post">
Name : <input type="text" name="name" />
Password : <input type="password" name="password" />
<input type="submit" />
</form>
</body>
</html>
Welcome View - JSP
The welcome page is shown on successful authentication. Shows the name of the login user and a link to manage your todos.
<html>
<head>
<title>First Web Application</title>
</head>
<body>
Welcome ${name}!! <a href="/list-todos">Click here</a> to manage your todo's.
</body>
</html>
Todo Model and Business Service
We are using a basic todo with id, user, description, and a target date.
package com.in28minutes.springboot.web.model;
import java.util.Date;
public class Todo {
private int id;
private String user;
private String desc;
private Date targetDate;
private boolean isDone;
//Getters, Setters, Constructors, toString, equals and hash code
}
Our todo service uses a simple ArrayList to store a list of todos. It offers a method to retrieve the todos.
package com.in28minutes.springboot.web.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Service;
import com.in28minutes.springboot.web.model.Todo;
@Service
public class TodoService {
private static List<Todo> todos = new ArrayList<Todo>();
private static int todoCount = 3;
static {
todos.add(new Todo(1, "in28Minutes", "Learn Spring MVC", new Date(),
false));
todos.add(new Todo(2, "in28Minutes", "Learn Struts", new Date(), false));
todos.add(new Todo(3, "in28Minutes", "Learn Hibernate", new Date(),
false));
}
public List<Todo> retrieveTodos(String user) {
List<Todo> filteredTodos = new ArrayList<Todo>();
for (Todo todo : todos) {
if (todo.getUser().equals(user)) {
filteredTodos.add(todo);
}
}
return filteredTodos;
}
}
List Todos - JSP
This is the page that will show our list of todos. This is a completely unformatted page. During the subsequent steps, we will beautify this page and create more functionalities so you can add, delete, and update todos.
<html>
<head>
<title>First Web Application</title>
</head>
<body>
Here are the list of your todos:
${todos}
<BR/>
Your Name is : ${name}
</body>
</html>
Todo Controller
The Todo Controller has a simple method to retrieve the list of todos and populate it into the model. It redirects to the list-todos view.
package com.in28minutes.springboot.web.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.in28minutes.springboot.web.service.LoginService;
import com.in28minutes.springboot.web.service.TodoService;
@Controller
@SessionAttributes("name")
public class TodoController {
@Autowired
TodoService service;
@RequestMapping(value="/list-todos", method = RequestMethod.GET)
public String showTodos(ModelMap model){
String name = (String) model.get("name");
model.put("todos", service.retrieveTodos(name));
return "list-todos";
}
}
Running the Application
We use a Spring Boot Application class to launch our application.
package com.in28minutes.springboot.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.in28minutes.springboot.web")
public class SpringBootFirstWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootFirstWebApplication.class, args);
}
}
The complete code is on my Github repository. You can download Step12.zip and import it as a Maven project to be able to run it.
You can run this as a simple Java application. When you run this you should see the application starting up. Below is some of the extracts from the log. You can see that all the request mappings are properly mapped. You can launch the application at http://localhost:8080/login and enter user id/password combination of in28Minutes/dummy.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.3.RELEASE)
2017-01-30 10:02:01.775 INFO 6070 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-01-30 10:02:01.798 INFO 6070 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-01-30 10:02:01.800 INFO 6070 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.6
2017-01-30 10:02:02.498 INFO 6070 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-01-30 10:02:02.498 INFO 6070 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3747 ms
2017-01-30 10:02:02.803 INFO 6070 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-01-30 10:02:03.557 INFO 6070 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login],methods=[GET]}" onto public java.lang.String com.in28minutes.springboot.web.controller.LoginController.showLoginPage(org.springframework.ui.ModelMap)
2017-01-30 10:02:03.559 INFO 6070 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login],methods=[POST]}" onto public java.lang.String com.in28minutes.springboot.web.controller.LoginController.showWelcomePage(org.springframework.ui.ModelMap,java.lang.String,java.lang.String)
2017-01-30 10:02:03.559 INFO 6070 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/list-todos],methods=[GET]}" onto public java.lang.String com.in28minutes.springboot.web.controller.TodoController.showTodos(org.springframework.ui.ModelMap)
2017-01-30 10:02:03.564 INFO 6070 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-01-30 10:02:03.565 INFO 6070 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-01-30 10:02:03.615 INFO 6070 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-30 10:02:03.616 INFO 6070 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-30 10:02:03.682 INFO 6070 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-30 10:02:04.108 INFO 6070 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-01-30 10:02:04.288 INFO 6070 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-01-30 10:02:04.307 INFO 6070 --- [ restartedMain] c.i.s.web.SpringBootFirstWebApplication : Started SpringBootFirstWebApplication in 7.204 seconds (JVM running for 9.191)
Published at DZone with permission of Ranga Karanam, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments