Sunday , June 25 2017
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

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework.samples.service.service</groupId>
    <artifactId>Spring_Boot_MVC_Example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.5.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Spring Controller :

Its a typical spring login form controller.

LoginController.java


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;

@Controller
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 :

LoginBean.java

 
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:

login.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring Login Form</title>
</head>
<body>
    <form:form name="submitForm" method="POST">

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

        </div>
    </form:form>
</body>
</html>

Preparing success view :

success.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success Form</title>
</head>
<body>
    <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>
</body>
</html>

Spring Boot Application class :

Application.java


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;

@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, 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.

SpringApplication.run(class,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.

http://localhost:8080/login

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

Hi Folks, 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+ ,

Check Also

How to change Spring Boot Tomcat Port Number

In this tutorials, I am going to show you how to change the Spring Boot …

Leave a Reply

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