Unable to render the JSP page using Spring Boot
In my SpringBoot application I have 2 controllers. I am trying to render JSP page but unable to do so. I have <app-root>
in JSP page which points to Angular application.
On hitting http://localhost:8081//api/project1/mission/home
I get below error.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jan 14 00:15:58 CST 2022
There was an unexpected error (type=Not Found, status=404).
I have tried many solutions but none of them are working. I need help. Thank you
application.yaml
server:
port: 8081
servlet:
context-path: /api/project1/mission
tomcat:
max-http-header-size: 72636B
management:
endpoints:
web:
base-path:
exposure:
include: health
path-mapping:
health: healthcheck
health:
solr:
enabled: false
redis:
enabled: false
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
welcome.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<base href="/api/project1/mission/"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/dist/polyfills.js" defer></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/dist/runtime.js" defer></script>
</head>
<body>
<div id="mission-app-ctnr">
<app-root></app-root>
</div>
</body>
</html>
FirstController
package com.google.mission.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
@RequestMapping(path = "/")
public class FirstController {
private String message = "Hi";
@GetMapping
public String welcome(Map<String, Object> model) {
model.put("message", this.message);
System.out.println(this.message);
return "welcome";
}
}
AnotherController.js
package com.google.mission.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
@RequestMapping(path = "/home")
public class AnotherController {
private String message = "Hi";
@GetMapping
public String serveTemplate(Map<String, Object> model) {
model.put("message", this.message);
return "welcome";
}
}
1.3.5. JSP Limitations
When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.
With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
Undertow does not support JSPs.
Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.
Putting item 2. in short: "It works only with .war
archives"!
To change your jar to a war, follow these (few, simple) steps:
- Change pom-packaging|gradle-plugin to "war". (https://start.spring.io packaging option)
- Add a
ServletInitializer
to your application. (also best sample from: https://start.spring.io/#!packaging=war )
Adding the below dependency should solve the error.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>