Loading
 
What's Next: View All Tutorial →

Spring boot Web MVC with JSP

A simple project setup for the Spring boot WEB MVC with JSP file setup   Maven Dependency   <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo...

Published Date: 05/09/2018  Last Modified Date: 05/09/2018    New Post



A simple project setup for the Spring boot WEB MVC with JSP file setup

 

Maven Dependency

 
<?xml version="1.0" encoding="UTF-8"?>
<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>com.demo</groupId>
    <artifactId>spring-boot-webmvc</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>spring-boot-webmvc</name>
    <description>Demo project for Spring Boot Web MVC</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>   
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>            
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Folder Structure

 
src
+---main
¦   +---java
¦   ¦   +---com
¦   ¦       +---demo
¦   ¦           +---spring
¦   ¦               ¦   Application.java
¦   ¦               ¦
¦   ¦               +---controller
¦   ¦                       WelcomeController.java
¦   ¦
¦   +---resources
¦   ¦   ¦   application.properties
¦   ¦   ¦
¦   ¦   +---static
¦   ¦   ¦       style.css
¦   ¦   ¦
¦   ¦   +---templates
¦   +---webapp
¦       +---WEB-INF
¦           +---jsp
¦                   index.jsp
¦

 

Application.java

 
package com.demo.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Bean
	public InternalResourceViewResolver vr() {
		InternalResourceViewResolver vr = new InternalResourceViewResolver();
		vr.setPrefix("/WEB-INF/jsp/");
		vr.setSuffix(".jsp");
		return vr;
	}

}


index.jsp


src\main\webapp\WEB-INF\jsp\index.jsp
 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
<link  rel="stylesheet" href="${pageContext.request.contextPath}/style.css"></link >
    </head>
    <body>
        <h1>Welcome</h1>

        <form:form method="POST" action="${pageContext.request.contextPath}/addEmployee">
            <label>Enter Employee name</label>
            <input type="text" value="John" name="empName"> <input
                type="submit" />
        </form:form>
        <br> Employee Name: ${employeeName}
        <c:out value="${employeeName}"/>
    </body>
</html>


${pageContext.request.contextPath} - gives the context path

style.css

Style.css should be stored inside resources/static/style.css
 

h1{
    color:blue;
}



application.properties

 
server.port=8080
server.servlet.context-path=/app


WeclomeController.java

 
package com.demo.spring.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class WelcomeController {

    @RequestMapping(path = "/", method = RequestMethod.GET)
    public String home(HttpServletRequest request) {
        return "index";
    }

    @RequestMapping(path = "/welcome", method = RequestMethod.GET)
    public String welcome(HttpServletRequest request) {
        return "index";
    }

    @RequestMapping(path = "/addEmployee", method = RequestMethod.POST)
    public String addEmployee(HttpServletRequest request, Model map) {

        map.addAttribute("employeeName", request.getParameter("empName"));
        return "index";
    }
}

@Controller - This anotation identify this as a controller class 

@RequestMapping - create a rest api url with GET or POST method, If method not provided, it support both GET and POST
 

Output:

Access the application using url
 
http://localhost:8080/app/welcome

8080 is the port number, which can be configure in application.properties
/app is the contextpath, which can be configure in application.properties 
 

Source Code available here

https://github.com/Catchmycity/Springboot/tree/master/spring-boot-webmvc

 
Awaiting for Administrator approval




← Back to list


Related Post




×