Wednesday , January 17 2018
Home / Spring Boot / Spring Boot MVC Example Tutorials

Spring Boot MVC Example Tutorials

In this tutorials I am going to show you how to create a Spring Boot MVC application.

Spring Boot MVC :

Here we are going to use the below technologies:

  • Spring.4.3.7.RELEASE
  • SpringBoot 1.4.5.RELEASE
  • Java 8

Project Structure :

A typical Maven project directory structure.

Spring boot MVC


<project xmlns="" xmlns:xsi=""

Spring Controller :

Its a typical spring login form controller.

package com.onlinetutorialspoint.spring.boot;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.onlinetutorialspoint.bean.LoginBean;

public class LoginController {
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String init(Model model) {
        model.addAttribute("msg", "Please Enter Your Login Details");
        return "login.jsp";

    @RequestMapping(method = RequestMethod.POST)
    public String submit(Model model,
            @ModelAttribute("loginBean") LoginBean loginBean) {
        if (loginBean != null && loginBean.getUserName() != null
                & loginBean.getPassword() != null) {
            if (loginBean.getUserName().equals("chandra")
                    && loginBean.getPassword().equals("chandra123")) {
                model.addAttribute("msg", loginBean.getUserName());
                return "success.jsp";
            } else {
                model.addAttribute("error", "Invalid Details");
                return "login.jsp";
        } else {
            model.addAttribute("error", "Please enter Details");
            return "login.jsp";

To make the example as simple as possible, I have validated the username and paswords by hard coding.

Model Bean :

package com.onlinetutorialspoint.bean;

public class LoginBean {

    private String userName;
    private String password;

    public String getUserName() {
        return userName;

    public void setUserName(String userName) {
        this.userName = userName;

    public String getPassword() {
        return password;

    public void setPassword(String password) {
        this.password = password;


Preparing View:


<%@ page language="java" contentType="text/html; charset=UTF-8"
<%@taglib uri="" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring Login Form</title>
    <form:form name="submitForm" method="POST">

        <div align="center">
                    <td>User Name</td>
                    <td><input type="text" name="userName" /></td>
                    <td><input type="password" name="password" /></td>
                    <td><input type="submit" value="Submit" /></td>
            <div style="color: red">${error}</div>


Preparing success view :


<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success Form</title>
    <font color="green"><h2>Hello</h2></font>
    <h3>${msg}</h3> You have successfully logged in.
    <font color="green"><h3>Welcome to Spring Boot World !</h3></font>

Spring Boot Application class :

package com.onlinetutorialspoint.spring.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class Application {
    public static void main(String[] args) throws Exception {, args);

@SpringBootApplication annotation tells to Spring Boot, this is a starting point of our Spring Boot Application.

SpringApplication is a class which is coming from org.springframework.boot. This class can be used to bootstrap and launch the spring application using the Java main method.,args);

run(Application.class,args) is a static method in SpringApplication class, which accepts 2 arguments, first one is a class which we have a main method and another one is arguments which are passed to main method. When we run this class the SpringApplication class does many things for us :

  • Its sets up the default configurations for Spring application.
  • It starts the Spring application context.
  • Performs the Class path scan (Loads all spring annotated classes).
  • And finally starts Tomcat Server.

Yes we are done with the Spring Boot MVC login example. Now time to run the application. We can run the Spring Boot application like as simple as Java standalone application (by running the main method).

You can access your application by bit the below url on your favourite browser.


Now you can see the login page like below.

Spring Boot Mvc Login

Success page :

Spring Boot Mvc success

Happy Learning 🙂


Download Example

About chandrashekhar

you have reach this so far, that shows you like what you are learning. Then why don’t you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,


Spring Boot How to change the Tomcat to Jetty Server

In this tutorial, I am going to show you how to change the Tomcat to …

Leave a Reply

Your email address will not be published. Required fields are marked *