Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - where to place the jsp files

I am trying to develop a new Spring boot application using MVC as a first step to move my existing Spring MVC application to Spring boot.

However, I am facing an issue with the mapping of jsp files.

Could not resolve view with name 'hello' in servlet with name 'dispatcherServlet'

I have ready many answers in SO, but none seem to solve my issue - I am not planning to use any template engines as I will have a lot of jsps to consider - might be a plan once spring boot is set up.

I have a project structure as below:

MyFirstApp
  --src/main/java
    --uk.co.company
      --MainApplication.java
      --ServletInitializer.java
    --uk.co.company.web
      --HelloController.java
  --src/main/resources
    --static
    --templates
    --application.properties
 --src
   --main
     --webapp
       --WEB-INF
         --jsp
           --hello.jsp
  --pom.xml

Placing the code below:

MyFirstAppApplication.java

 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, 
 HibernateJpaAutoConfiguration.class })
 @EnableWebMvc
 public class MyFirstAppApplication extends SpringBootServletInitializer {
     public static void main(String[] args) {
    SpringApplication.run(MyFirstAppApplication.class, args);
  }
}

ServletInitializer.java

  public class ServletInitializer extends SpringBootServletInitializer {

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

HelloController.java

@Controller
public class HelloController {  
@RequestMapping("/hello")
public String sayHello() {      
    return "hello";
}   
}

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Hello</title>
</head>
<body>
hellooo
</body>
</html>

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>

<groupId>uk.co.company</groupId>
<artifactId>MyFirstApp</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>

<name>MyFirstApp</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.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>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
    </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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
like image 313
Reema Avatar asked May 23 '17 09:05

Reema


2 Answers

There is a dependency to include as Spring boot doesn't know how to translate JSP to Servlet. So,

  1. Check the version of your tomcat-embed-core-.jar.

  2. Go to that corresponding version release on https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper

  3. Copy the dependency, it will look like-

<dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>9.0.37</version> 
</dependency> 

here version needs to be updated as per your tomcat-embed-core jar version

  1. Update Maven Project so that it can download the jar from repo.

Now you are good to go with your project.

thanKs.

like image 197
Ashish Kumar Avatar answered Oct 13 '22 02:10

Ashish Kumar


Using the directory src/main/webapp/WEB-INF/jsp/.. is discouraged if you want to deploy via a jar package.

“Do not use the src/main/webapp folder if your application will be packaged as a jar. Although this folder is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.”

Reference: https://docs.spring.io/spring-boot/docs/1.1.4.RELEASE/reference/htmlsingle/#using-boot-structuring-your-code

like image 43
coder3 Avatar answered Oct 13 '22 02:10

coder3