Spring引导不能显示jsp页面但运行Controller映射方法

时间:2018-02-20 03:02:07

标签: java spring spring-boot

我在Spring启动应用程序中遇到问题:我无法显示jsp页面,但它运行映射到该页面的Controller方法。

主要班级:

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);
    }
}
控制器:

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>

欢迎任何帮助,我坚持了一段时间。当我打电话给网址&#34; http://localhost:8181/afficher&#34;在谷歌浏览器中:它显示:白标错误页面,我可以在控制台中看到来自方法的消息&#34; je suis dans souk 5 controller&#34;。

3 个答案:

答案 0 :(得分:0)

将以下依赖项添加到您的POM文件中:

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

将JSP模板放在src / main / resources / META-INF / resources / WEB-INF / jsp /

application.properties:中 应更新到以下:

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

spring.mvc.view.suffix = .jsp

答案 1 :(得分:0)

正如瑞诗凯诗所述,在pom.xml中添加了依赖关系,并执行以下操作。

您需要从SpringBootServletInitializer扩展您的Main类。

@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);
    }
}

注意:确保您的目录正确如下(来源:sitesbay):

JSP Directory Structure

答案 2 :(得分:0)

感谢每一个人。最后我发现了我的错误,我无法相信它是application.properties中的一个空格: spring.mvc.view.suffix = .jsp我在.jsp后面有一个空格,我无法想象这种影响。我希望能帮助别人。