Spring Boot MVC Example | Spring Boot Login Online TutorialsPoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, I am going to show how to create a Spring Boot MVC application.
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.

<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 :
It’s 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;

@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";
        }
    }
}
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"
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 a success view :
<%@ 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 :
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(Application.class, args) is a static method in SpringApplication class, which accepts two arguments. First one is a class which is the main class of Spring boot, and another one is arguments which can be passed to the main method. When we run this class the SpringApplication class does many things for us :
It sets up the default configurations for Spring application.
It starts the Spring application context.
Performs the Classpath scan (Loads all spring annotated classes).
And finally, start the 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.
Success page :
Happy Learning 🙂


spring_boot_mvc_example

Spring Boot MVC Login Example
File size: 8 MB
Downloads: 4859


Spring MVC Login Form Example Tutorials
Spring Boot Validation Login Form Example
How to set Spring Boot SetTimeZone
Spring Web MVC Framework Flow
How to change Spring Boot Tomcat Port Number
How To Change Spring Boot Context Path
Simple Spring Boot Example
Spring Boot FileUpload Ajax Example
Spring MVC Form Validation Example
Spring Boot H2 Database + JDBC Template Example
Spring Boot Actuator Database Health Check
Spring Boot RabbitMQ Message Publishing Example
How to use Spring Boot Random Port
Spring Boot How to change the Tomcat to Jetty Server
Spring Boot MockMvc JUnit Test Example

Spring MVC Login Form Example Tutorials
Spring Boot Validation Login Form Example
How to set Spring Boot SetTimeZone
Spring Web MVC Framework Flow
How to change Spring Boot Tomcat Port Number
How To Change Spring Boot Context Path
Simple Spring Boot Example
Spring Boot FileUpload Ajax Example
Spring MVC Form Validation Example
Spring Boot H2 Database + JDBC Template Example
Spring Boot Actuator Database Health Check
Spring Boot RabbitMQ Message Publishing Example
How to use Spring Boot Random Port
Spring Boot How to change the Tomcat to Jetty Server
Spring Boot MockMvc JUnit Test Example
 


Δ
 Spring Boot – Hello World
 Spring Boot – MVC Example
 Spring Boot- Change Context Path
 Spring Boot – Change Tomcat Port Number
 Spring Boot – Change Tomcat to Jetty Server
 Spring Boot – Tomcat session timeout
 Spring Boot – Enable Random Port
 Spring Boot – Properties File
 Spring Boot – Beans Lazy Loading
 Spring Boot – Set Favicon image
 Spring Boot – Set Custom Banner
 Spring Boot – Set Application TimeZone
 Spring Boot – Send Mail
 Spring Boot – FileUpload Ajax
 Spring Boot – Actuator
 Spring Boot – Actuator Database Health Check
 Spring Boot – Swagger
 Spring Boot – Enable CORS
 Spring Boot – External Apache ActiveMQ Setup
 Spring Boot – Inmemory Apache ActiveMq
 Spring Boot – Scheduler Job
 Spring Boot – Exception Handling
 Spring Boot – Hibernate CRUD
 Spring Boot – JPA Integration CRUD
 Spring Boot – JPA DataRest CRUD
 Spring Boot – JdbcTemplate CRUD
 Spring Boot – Multiple Data Sources Config
 Spring Boot – JNDI Configuration
 Spring Boot – H2 Database CRUD
 Spring Boot – MongoDB CRUD
 Spring Boot – Redis Data CRUD
 Spring Boot – MVC Login Form Validation
 Spring Boot – Custom Error Pages
 Spring Boot – iText PDF
 Spring Boot – Enable SSL (HTTPs)
 Spring Boot – Basic Authentication
 Spring Boot – In Memory Basic Authentication
 Spring Boot – Security MySQL Database Integration
 Spring Boot – Redis Cache – Redis Server
 Spring Boot – Hazelcast Cache
 Spring Boot – EhCache
 Spring Boot – Kafka Producer
 Spring Boot – Kafka Consumer
 Spring Boot – Kafka JSON Message to Kafka Topic
 Spring Boot – RabbitMQ Publisher
 Spring Boot – RabbitMQ Consumer
 Spring Boot – SOAP Consumer
 Spring Boot – Soap WebServices
 Spring Boot – Batch Csv to Database
 Spring Boot – Eureka Server
 Spring Boot – MockMvc JUnit
 Spring Boot – Docker Deployment