Facelets welcome page not found or redirected using Spring Boot [duplicate] - spring-boot

This question already has answers here:
Changing default welcome-page for spring-boot application deployed as a war
(4 answers)
how to specify welcome-file-list in WebApplicationInitializer.onStartup()
(4 answers)
Closed 3 years ago.
I am currently trying to move a java EE 8 project running on wildfly to the latest spring boot.
For this i will need primefaces, primefaces-extensions and omnifaces cause of it's
components i am already using and also designed (buyed) theme.
Starting with the latest spring boot in combination with joinfaces i currently try to
define the welcome page or redirect to the index.xhtml page
but anything i tried failed.
Here is my current sources:
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>
<!-- Project settings -->
<groupId>com.skf.ecocalcweb</groupId>
<artifactId>EcoCalcDD4Web</artifactId>
<version>2.0.0</version>
<packaging>jar</packaging>
<!-- Using spring boot as parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- Project properties -->
<properties>
<!-- General settings -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<!-- Libraries versions -->
<lombok.version>1.18.12</lombok.version>
<joinfaces.version>4.1.4</joinfaces.version>
<primefaces.core.version>7.0.11</primefaces.core.version>
<primefaces.extensions.version>7.0.3</primefaces.extensions.version>
<omnifaces.version>3.4.1</omnifaces.version>
<fontawesome.version>5.12.0</fontawesome.version>
</properties>
<!-- Use joinfaces for jsf starters -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>joinfaces-dependencies</artifactId>
<version>${joinfaces.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- JAR-Dependencies -->
<dependencies>
<!-- Spring boot starters -->
<!-- Exclude mojarra, so use myfaces instead -->
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>primefaces-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.joinfaces</groupId>
<artifactId>mojarra-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>myfaces-spring-boot-starter</artifactId>
</dependency>
<!-- Also use omnifaces3, so latest jsf version must be used -->
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>omnifaces3-spring-boot-starter</artifactId>
</dependency>
<!-- Java Enterprise API for using primefaces in JSF -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- OmniFaces -->
<dependency>
<groupId>org.omnifaces</groupId>
<artifactId>omnifaces</artifactId>
<version>${omnifaces.version}</version>
</dependency>
<!-- Primefaces core -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>${primefaces.core.version}-SNAPSHOT</version>
</dependency>
<!-- Primefaces extensions -->
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>${primefaces.extensions.version}</version>
</dependency>
<!-- Primefaces theme -->
<dependency>
<groupId>com.skf.ecocalcweb</groupId>
<artifactId>PrimefacesTheme</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Font Awesome -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>font-awesome</artifactId>
<version>${fontawesome.version}</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<!-- Final name of war-file -->
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
/src/main/java/com/test/Application.java
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.run(args);
}
#Bean
public ServletRegistrationBean facesServletRegistration() {
FacesServlet servlet = new FacesServlet();
ServletRegistrationBean registration = new ServletRegistrationBean(servlet, new String[]{"*.xhtml"});
registration.setName("Faces Servlet");
registration.setLoadOnStartup(1);
registration.setEnabled(true);
registration.setOrder(1);
return registration;
}
#Bean
public ServletContextInitializer servletContextInitializer() {
return servletContext -> {
// MyFaces
servletContext.setInitParameter("org.apache.myfaces.LOG_WEB_CONTEXT_PARAMS", Boolean.FALSE.toString());
// Primefaces
servletContext.setInitParameter("primefaces.THEME", "harmony-fate");
servletContext.setInitParameter("primefaces.MOVE_SCRIPTS_TO_BOTTOM", Boolean.FALSE.toString());
servletContext.setInitParameter("primefaces.FONT_AWESOME", Boolean.TRUE.toString());
// Omnifaces
servletContext.setInitParameter("org.omnifaces.SOCKET_ENDPOINT_ENABLED", Boolean.TRUE.toString());
// JavaX
servletContext.setInitParameter("javax.faces.PROJECT_STAGE", "Production");
servletContext.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
servletContext.setInitParameter("com.sun.faces.enableRestoreView11Compatibility", Boolean.TRUE.toString());
servletContext.setInitParameter("javax.faces.FACELETS_SKIP_COMMENTS", Boolean.TRUE.toString());
servletContext.setInitParameter("javax.faces.STATE_SAVING_METHOD", "server");
servletContext.setInitParameter("com.sun.faces.compressViewState", Boolean.FALSE.toString());
servletContext.setInitParameter("com.sun.face.serializeServerState", Boolean.FALSE.toString());
servletContext.setInitParameter("javax.faces.FACELETS_REFRESH_PERIOD", "-1");
servletContext.setInitParameter("javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL", Boolean.TRUE.toString());
servletContext.setInitParameter("javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE", Boolean.TRUE.toString());
servletContext.setInitParameter("javax.faces.ENABLE_CDI_RESOLVER_CHAIN", Boolean.TRUE.toString());
};
}
}
/src/main/java/com/test/controllers/Index.java
#Getter
#Setter
#RequestScope
#Named
public class Index {
private String firstName = "John";
private String lastName = "Doe";
public String showGreeting() {
return "Hello " + firstName + " " + lastName + "!";
}
}
/src/main/resources/META-INF/resources/index.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>PrimeFaces Hello World Example</title>
</h:head>
<h:body>
<h:form id="helloworld-form">
<p:panel header="PrimeFaces Hello World Example">
<h:panelGrid columns="2" cellpadding="4">
<h:outputText value="First Name: " />
<p:inputText id="first-name" value="#{index.firstName}" />
<h:outputText value="Last Name: " />
<p:inputText id="last-name" value="#{index.lastName}" />
<p:commandButton id="submit" value="Submit"
update="greeting-panel"
oncomplete="PF('greetingDialog').show()" />
</h:panelGrid>
</p:panel>
<p:dialog header="Greeting" widgetVar="greetingDialog"
modal="true" resizable="false">
<h:panelGrid id="greeting-panel" columns="1" cellpadding="4">
<h:outputText value="#{index.showGreeting()}" />
</h:panelGrid>
</p:dialog>
</h:form>
</h:body>
</html>
/src/main/resources/META-INF/resources/application.properties
# the embedded application server config
server.port=80
server.servlet.context-path=/
# thymeleaf templating engine config
spring.thymeleaf.enabled=false
So what's needed here to set the welcome-page for a jsf application right ?
When i directly called
http://localhost/index.xhtml
the primefaces page will be rendered right,
but when i enter
http://localhost/
no redirect to the index.xhtml will be made
and i currently found nothing which is working as a "redirect" ...

I think i currently found the solution :
So i post it for all others which are start using spring boot and primefaces together:
Adding this dependency will fix my problem with the welcome page as xhtml :
<!-- Add spring boot web needed for navigation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Then i can use the following class file to set my welcome-page:
/src/main/java/com/test/navigation/WelcomePageRedirect.java
#Configuration
public class WelcomePageRedirect implements WebMvcConfigurer {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/")
.setViewName("forward:/index.xhtml");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
Now the following technologies are working together:
Java 8 (And also Java 11 !)
Primefaces 7.0.11 + Primefaces Extensions
Omnifaces 3.4.1
Latest FontAwesome 5.12.0 in combination with Primefaces "old" FontAwesome
Spring Boot 2.2.4
JoinFaces 4.1.4
MyFaces 2.3

Related

error 404 while run springBoot application on chrome browser

I want to use jsp with springboot, i add jasper dependency in my
pom.xml file, but when i run my springboot app it run corectly ,when i
type localhost:8080/home in my browser it show me this type of error.
i use getMapping annotation in palace of RequestMapping,I try it last
four days but it cannot shorout
this is my project file strcture
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.main</groupId>
<artifactId>SpringSecurityTutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringSecurityTutorial</name>
<description>Spring Security Service</description>
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>8.0.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>8.5.35</version>
</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>
HomeConteoller.java
package com.main.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class HomeController {
#GetMapping("/home")
public String home() {
return "Home.jsp";
}
}
home.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome to Security Protection
</body>
</html>
The string returned by the home() handler method in your controller should be the name of the JSP view, without the extension. i.e. return "home";.
Try by adding the following dependency
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
Try this :
return "/Home.jsp";

Why controller class not return jsp page in springBoot application

I created just a simle springBoot application,and I want when i lunch
my application it retun me my jsp page in my browser. but when I type
localhost:8080 in my browser it return me this type of error"There was
an unexpected error (type=Not Found, status=404)."I added jasper
dependency got same error,I use jsp api dependency in place jasper i
got same error.
what is the reason i am not getting jsp page as a result,why it throws
error404
this type of error
project file strcture
HomeController.java
package com.main.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HomeController {
#RequestMapping("/")
public String host() {
return "Katak.jsp";
}
}
Katak.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome to security World
</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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.main</groupId>
<artifactId>BasicSecurity-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>BasicSecurity-2</name>
<description>Demo project for SpringSecurity</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>8.5.37</version>
</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>
Make sure that you are using proper jsp names, in project structure, jsp name is showing as "Katakile.jsp". But in controller you are returning "Katak.jsp".
If names are proper and still not working, please use "tomcat-embed-jasper" dependency instead of "tomcat-jasper" and remove the tomcat-jasper dependency from your classpath, if exists.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.6</version>
<scope>provided</scope>
</dependency>
Just edit this line
#RequestMapping("/katak")
public String host() {
return "/Katakile.jsp";
}
And try acces localhost:8080/katak

How to setup Spring forms for Spring boot

I try to use Spring formtags. I have a running Spring boot application.
I have a hello.jsp like this, which doesn't contain any formtags:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Hello
</body>
</html>
When calling my controller which resolves the view it displays "Hello" as expected.
When adding
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
on top and calling the controller -> all content is shown, the complete source of the page. It seems the taglib is not recognized.
In my dependencies I have (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>com.solvapps</groupId>
<artifactId>springboottests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboottests</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.faces/jsf-api -->
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
My MVC configuration:
#Configuration
#EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
#Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".jsp");
return resolver;
}
#Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
* Update *
In the console of tomcat I get the following error:
2019-01-31 12:47:40.988 ERROR 13952 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
[Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.springframework.http.MediaType.isPresentIn(Ljava/util/Collection;)Z] with root cause
java.lang.NoSuchMethodError: org.springframework.http.MediaType.isPresentIn(Ljava/util/Collection;)Z
What is missing here ? Any help ?

spring boot 2.1.0 war file cannot config 'context-path' or else 'include' in jsp not working

I'm using Spring Boot 2.1.0 to develop a web application. I package the codes into a WAR file with maven.
I configured server.servlet.context-path=/marpt in application.properties file so that application can not access by ip+port directly.
Then I run the WAR using java -jar marpt.war command, and it is successed start.
Then I open the running web application in Chrome, there're some problems throw out:
1. The index login page shows corrcetly;
2. After login, jsp include not working, both of two types of inclue command, <jsp:include page="${ctx}/home/sidebar" /> and <%#include file="../../pages/share/partial/header.jsp" %> and any other references.
I use javascript to alert the ${ctx} which I defined in another jsp file, and include it in top of homepage by <%# include file="../../pages/common/taglibs.jsp" %>, it's very strange, the taglibs.jsp can be included. In it I set <c:set var="ctx" value="${pageContext.request.contextPath}" />. In home page I do this: alert('${pageContext.request.contextPath}'), the result is /marpt.
I'm confused where is wrong.
I try to package the jar file, it's also start correctly, but pages cannot open in browser at all.
Below are main configuration files:
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>com.test</groupId>
<artifactId>ma-rpt</artifactId>
<version>1.5.0</version>
<packaging>war</packaging>
<name>ma-rpt</name>
<description>Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<!-- Shiro ehCache -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</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>
</dependency>
<!-- jstl支持 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>marpt</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
<include>properties/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
</build>
</project>
application.properties
# Spring Public
server.port =8096
server.servlet.context-path =/marpt
# Cache
spring.cache.type =ehcache
# Db
spring.datasource.driver-class-name =oracle.jdbc.driver.OracleDriver
spring.datasource.url =jdbc:oracle:thin:#192.168.0.1:1521:marpt
spring.datasource.username =admin
spring.datasource.password =123456
# MyBatis
mybatis.config-location =classpath:/configs/mybatis/mybatis.cfg.xml
mybatis.mapper-locations =classpath*:/mybatis/**/*Mapper.xml
# Log
spring.output.ansi.enabled =DETECT
logging.path =/data/home/www/marpt/logs
logging.file =ma-rpt
logging.level.* =DEBUG
#debug=false
rpt.project.name =ma-rpt
rpt.project.develop =true
SpringBootConfig.java
#SpringBootConfiguration
public class SpringBootConfig implements WebMvcConfigurer {
#Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
configurer.setUseRegisteredSuffixPatternMatch(true);
}
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true)
.favorParameter(true)
.parameterName("format")
.ignoreAcceptHeader(true)
.defaultContentType(MediaType.TEXT_HTML)
.mediaType("html", MediaType.TEXT_HTML)
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xls", MediaType.valueOf("application/vnd.ms-excel"))
.mediaType("xlsx", MediaType.valueOf("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
}
#Override
public void configureViewResolvers(ViewResolverRegistry registry) {
Set<String> modelKeys=new HashSet<>();
modelKeys.add("list");
modelKeys.add("table");
registry.jsp("/views/", ".jsp");
registry.enableContentNegotiation(new MappingJackson2JsonView());
XlsView xlsView=new XlsView();
xlsView.setModelKeys(modelKeys);
registry.enableContentNegotiation(xlsView);
XlsxView xlsxView=new XlsxView();
xlsxView.setModelKeys(modelKeys);
registry.enableContentNegotiation(xlsxView);
}
}
Main of homepage.jsp
<%# page language="java" pageEncoding="UTF-8" errorPage="../error/error.jsp" %>
<%# include file="../../pages/common/taglibs.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<title>Home</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="${ctx}/assets/global/plugin/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="${ctx}/assets/global/plugin/jquery/jquery.min.js" type="text/javascript"></script>
</head>
<body >
<div class="page-wrapper">
<%#include file="../../pages/share/partial/header.jsp" %>
<div id="pageContainer" class="page-container">
<jsp:include page="${ctx}/home/sidebar" />
<jsp:include page="${ctx}/home/content" />
</div>
</div>
<script src="${ctx}/assets/global/plugin/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script>
$(function() {
alert('${ctx}'); // result is '/marpt'
alert('${pageContext.request.contextPath}'); // result is '/marpt' too
});
</script>
</body>
</html>
The application structure:
application structure
So, if I define the server.servlet.context-path=/marpt configure, how can I make <%#include... or <jsp:include... working in jsp file in Spring Boot? Thanks very much!
======================== supply ===========================
I tried to downgrade too 1.5.18, modified properties to server.context-path=/marpt, the problem is still exist, not relate to the version of spring boot.
The include files are some html tags, js, and some bind model attributes(such as ${param.dt}..), JSP files which are NOT mapping to servlet. If I configure them in a static resources' path, also failure.
======================== figure out ===========================
see JB Nizet's comment
Add the following spring tag in order to get property value "server.servlet.context-path" using #environment.getProperty('some.property') should work.
<spring:eval expression="#environment.getProperty('server.servlet.context-path')" var="ctx" />
<link href="${ctx}/assets/global/plugin/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

Spring Boot jsp files cannot be found

I'm developing simple Spring Boot MVC application. Previously I had everything working with just Spring + Hibernate (that is there was servlet.xml etc.) and I'm trying to rebuild project to use Spring Boot. Althought I have read many answers on SO I still can't fix Spring Boot not finding my .jsp files - it displays whitelabel error page.
Many answers on SO are about including tomcat-embed-jasper and jstl in pom.xml but I already have it. I'm using IntelliJ IDEA 17 Ultimate, Tomcat 8.5.13, latest versions of Spring and Spring Boot.
Project structure:
Pom.xml:
<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-invoice</artifactId>
<groupId>spring-invoice</groupId>
<version>1.0.0</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<properties>
<springframework.version>4.3.8.RELEASE</springframework.version>
<hibernate.version>5.2.9.Final</hibernate.version>
<mysql.connector.version>5.1.41</mysql.connector.version>
<hibernatevalidator.version>5.4.1.Final</hibernatevalidator.version>
<start-class>com.invoices.Application</start-class>
</properties>
<dependencies>
<!-- Spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Hibernate validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernatevalidator.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<!-- C3PO -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- Servlet + JSP + JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>spring-invoice</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- The Compiler Plugin is used to compile the sources of project -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Builds a Web Application Archive (WAR) file from the project output and its dependencies. -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties file
# Database
spring.datasource.url=jdbc:mysql://localhost:3306/fakturowanie
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
# Hibernate
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
entitymanager.packagesToScan=com.invoices
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
Application class
#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);
}
}
One of my jsp files
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="pl-PL">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Zarządzaj kontrahentami</title>
</head>
<body>
<div id="wrapper">
<div id="header">
<h2>Zarządzaj kontrahentami</h2>
</div>
</div>
<div>
<div>
<!-- put new button: Add Customer -->
<input type="button" value="Dodaj nowego kontrahenta"
onclick="window.location.href='addCustomer'; return false;"
class="add-button"/>
<table>
<tr>
<th>Alias</th>
<th>Nazwisko</th>
<th>Imię</th>
<th>Nazwa firmy</th>
<th>NIP/PESEL</th>
<th>Ulica i nr mieszkania</th>
<th>Kod pocztowy</th>
<th>Miejscowość</th>
<th>Sposób zapłaty</th>
<th>Uwzględnij numer faktury</th>
</tr>
<c:forEach var="tempCustomer" items="${allCustomers}">
<c:url var="updateLink" value="/customers/updateCustomer">
<c:param name="customerID" value="${tempCustomer.id}"/>
</c:url>
<c:url var="deleteLink" value="/customers/deleteCustomer">
<c:param name="customerID" value="${tempCustomer.id}"/>
</c:url>
<tr>
<td>${tempCustomer.alias}</td>
<td>${tempCustomer.lastName}</td>
<td>${tempCustomer.firstName}</td>
<td>${tempCustomer.companyName}</td>
<td>${tempCustomer.taxIdentifier}</td>
<td>${tempCustomer.postalCode}</td>
<td>${tempCustomer.city}</td>
<td>${tempCustomer.paymentMethod}</td>
<td>${tempCustomer.includeInCount}</td>
<td>
<!-- display the update and delete link -->
Edytuj
|
<a href="${deleteLink}" onclick="if (!(confirm(
'Czy na pewno usunąć tego kontrahenta?'))) return false">Usuń</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
And controller of it
#Controller
#RequestMapping("/customers")
public class CustomersController implements IUtilities<Customer>
{
#Autowired
private ICustomerDAO customerDAO;
#GetMapping("/manageCustomers")
public String manageCustomers(Model model)
{
List<Customer> allCustomers = convertIterableToCollection(customerDAO.findAll());
model.addAttribute("allCustomers", allCustomers);
return "manage-customers";
}
#GetMapping("/addCustomer")
public String showFormForAdd(Model model)
{
// create model attribute to bind form data
Customer theCustomer = new Customer();
model.addAttribute("customerToEdit", theCustomer);
return "customer-form";
}
#GetMapping("/updateCustomer")
public String showFormForUpdate(#RequestParam("customerID") int customerId, Model model)
{
// get the customer from service
Customer theCustomer = customerDAO.findOne(customerId);
model.addAttribute("customerToEdit", theCustomer);
//redirect to update form
return "customer-form";
}
#GetMapping("/deleteCustomer")
public String deleteCustomer(#RequestParam("customerID") int customerId)
{
//delete the customer
customerDAO.delete(customerId);
return "redirect:/customers/manageCustomers";
}
#PostMapping("/saveCustomer")
public String saveCustomer(#ModelAttribute("customerToEdit") Customer customer) {
//save the customer using our service
customerDAO.save(customer);
return "redirect:/customers/manageCustomers";
}
#Override
public List<Customer> convertIterableToCollection(Iterable<Customer> iterable) {
List<Customer> list = new ArrayList<>();
iterable.forEach(list::add);
return list;
}
}
You are using Spring Boot but your pom is riddled with conflicting dependencies. For starters cleanup your pom.xml. Also in your pom you should take into account what is being done in the JSP Samples pom.xml (Which means at least make the spring-boot-starter-tomcat provided instead of the default compile scope.
Finally you don't need the maven-compile and maven-war plugins as those are already inherited from the parent.
<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-invoice</artifactId>
<groupId>spring-invoice</groupId>
<version>1.0.0</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<properties>
<hibernate.version>5.2.9.Final</hibernate.version>
<start-class>com.invoices.Application</start-class>
</properties>
<dependencies>
<!-- Spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- C3PO -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- Servlet + JSP + JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<finalName>spring-invoice</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Also take into account the limitations there are when using Spring Boot with JSPs.
However the actual problem is your project structure, you are using Maven with should have a src/main/java directory for your java sources and all your web related things should be in src/main/webapp instead of web, further non java related resources should be in src/main/resources. Due to not having this default structure files aren't going to be copied to the correct location (actually they will be ignored).

Resources