JSP and Spring Boot

spring boot 2 jsp example
spring boot jsp viewresolver example
spring boot jsp view resolver
how to call jsp page from controller in spring boot
spring boot jsp form example
spring boot jsp crud example
spring boot jsp not rendering
spring boot jsp not found

I trying to build a simple welcome page (in jsp) using Spring Boot.

Below is the project structure

Application

@SpringBootApplication
public class Application {

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

    public SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}

APIController

@Controller
public class APIController {

    @RequestMapping("/")
    public String home() {
        System.out.println("testing");
        return "welcome";
    }
}

When I access http://localhost:8080/, I get below error

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed May 23 15:31:51 MYT 2018 There was an unexpected error (type=Not Found, status=404). No message available

By curl

{"timestamp":1527061233703,"status":404,"error":"Not Found","message":"No message available","path":"/"}

pom.xml

<?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>

    <artifactId>spring-boot-web-jsp</artifactId>
    <packaging>war</packaging>
    <name>Spring Boot Web JSP Example</name>
    <description>Spring Boot Web JSP Example</description>

    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!-- This is a web application -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- Optional, test for static content, bootstrap CSS-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

You have to define the prefix and suffix for you jsp file in application.properties file like following:

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

Spring Boot 2 With JSP View, In this tutorial, we demonstrate how easy it is to create web application with Spring Boot 2, along with embedded Tomcat JSP template and JSP  This tutorial walks you through the steps of creating a Hello World web app example with Spring Boot and JSP Spring Boot is a popular Java-based framework used to build production-grade web applications and services. You can find more Spring Boot tutorials here JSP is a Java-based server-side template engine

In directory \src\main\resources\static, Create a simple index.html file

<html>
    <head>Hello, Bich Van</head>
    <body>
        <h3>Today is a rainny day</h3>
    </body>
</html>

Then try again at http://localhost:8080

If you don't like static HTML files, you can use JSP, but need a little complicate. You need declaring JSP View resolver in Spring MVC configuration.

Reference document: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-welcome-page

Spring Boot JSP View Resolver Example, Spring Boot JSP View Resolver Example. Learn to configure Spring boot application use JSPTemplate to map JSP files using InternalResourceViewResolver. In this tutorial, we will learn how to develop Spring boot Todo Management web application using Spring Boot, Spring MVC, Spring Security, JSP, JPA and MySQL as a database. This mini project is mainly for beginners to learn how to develop a Web application step by step using Spring Boot.

Ensure that you have jasper and jstl in the list of dependencies:

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

Here is a working starter project - https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp

Spring Boot JSP Tutorial with Example, This tutorial walks you through the steps of creating a Hello World web app example with Spring Boot and JSP. Spring Boot is a popular  By default spring-boot doesn't configure jsp. There are four different steps to take to configure jsp within spring boot. Create JSP controller. First we will need to create a controller, JSPController that has a @RequestMapping /jsptest which will return a string which will resolve to a jsp file name jsp-spring-boot.jsp. @Controller public class JSPController {@RequestMapping ("/jsptest") public String test (ModelAndView modelAndView) {return "jsp-spring-boot";}}

It's better to use thymeleaf in your spring boot project and add the following dependency <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

In directory \src\main\resources\template, Create a simple index.html file

Creating a Web Application with Spring Boot with JSP – Spring Boot , Setting up a basic web application with Spring Boot is a cake walk. We will create a simple web application using Spring Initializr and add JSP  Learn to create and configure spring boot jsp view resolver which uses JSP template files to render view layer. This example uses embedded Tomcat server to run the application. Sourcecode Structure. The files in this application are placed as given structure in image. Spring Boot Application Structure. Maven dependencies – pom.xml.

How to create a Spring Boot Web Application (Spring MVC with JSP , This guide shows how to use Spring Boot to create MVC web application with JSP. Prerequisites: Eclipse IDE (neon release) Maven 4 Java 1.8  Spring Boot - Serving JSP pages Placing dynamic contents like JSP pages into static locations does not work. In a war project, JSP pages are served from src/main/webapp/WEB-INF/. In a Jar project, JSP pages cannot simply be served from webapp location or from src/main/resources/.

Spring Boot & JSP, Maven Dependencies. tomcat-embed-jasper : This is the artifact which is actually required to render .jsp pages in spring boot app. All  Is there any simple way to generate Spring Boot Mvc project for .jsp views? I've tried to create project in similar way like Spring Boot Rest project, but it's not soo easy for me. I've tried to use official Spring Boot Mvc jsp sample, but when I try to run this I get exception like this:

Spring Boot Mvc with Jsp Example, I pick the module “ spring-boot-starter-web ” that adds an embedded Tomcat servlet container and Spring MVC. Note that the embedded Tomcat  Overview This article contains spring boot form submit example using JSP. The form has been submitted using post method and the controller will handle post using @PostMapping  annotation. Submitted data has been transferred to another JSP using ModelMap.

Comments
  • What's the location of your welcome jsp file ?
  • @RichardK WEB-INF --> jsp ---> welcome.jsp
  • I guess you should return jsp/welcome
  • @RichardK still same..It only display testing in console
  • Can you also include all the dependencies ? (also the answer below may help)
  • where should I create application.properties file ?
  • Spring boot created it by default in src/main/resources directory
  • can you show me the structure ? I tried but still failed
  • @Cyndi how you generating your project structure ?
  • It works now after I change to intellij..Before that I using netbeans, so the stucture a bit different
  • I create HTML,but still same exception
  • Added but still same