Omar B.
Omar B.

Reputation: 491

Spring boot can't show jsp page but run Controller mapping method

I have a problem in my spring boot application : I can not show the jsp page but it's run the Controller method mapped to that page .

main Class :

package com.example.souk;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Souk5Application {

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

the Controller :

package com.example.souk;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Souk5Controller {

    @RequestMapping("/afficher")
    public String test(){
        System.out.println("je suis dans souk 5 controller ");
        return "afficher";
    }
}

application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/kijiji
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-AUTO=update
spring.jpa.properties.hibernate.dialct=org.hibernate.dialect.MySQL5Dialect
server.port=8181
spring.mvc.view.prefix= WEB-INF/jsp/
spring.mvc.view.suffix= .jsp

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.benamar</groupId>
    <artifactId>souk5</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
    </dependencies>

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


</project>

Please any help is welcome , I m stuck in that for a while . when I call the url "http://localhost:8181/afficher" in google chrome : it shows : Whitelabel Error Page and I can see in the Console the message from the method "je suis dans souk 5 controller ".

Upvotes: 0

Views: 1848

Answers (3)

Omar B.
Omar B.

Reputation: 491

thanks every body.finally I found my error , I can not beleive it it was a space in the application.properties: spring.mvc.view.suffix= .jsp I had a space just here after .jsp , I could not imagine that affect .I wish that help someone one .

Upvotes: 0

Mehraj Malik
Mehraj Malik

Reputation: 15844

As mentioned by Rishikesh add dependencies in pom.xml and do the following also.

You need to extend your Main class from SpringBootServletInitializer.

@SpringBootApplication 
public class Souk5Application extends SpringBootServletInitializer{

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

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

Note : Make sure your directory is as correct as below (Sources : sitesbay):

JSP Directory Structure

Upvotes: 0

Chao Jiang
Chao Jiang

Reputation: 493

Add below dependencies to your POM file:

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

Place the JSP templates under src/main/resources/META-INF/resources/WEB-INF/jsp/

In your application.properties: Should update to below:

spring.mvc.view.prefix= /WEB-INF/jsp/

spring.mvc.view.suffix= .jsp

Upvotes: 0

Related Questions