无法呈现JSP页面

时间:2019-01-11 07:16:51

标签: java spring-boot jsp

我正在src / main内手动创建webapp / WEB-INF / views文件夹,并且还添加了2个依赖项,例如org.apache.tomcat.embed和javax.servlet,但是当我运行时仍然会抛出404正在调用控制器的URL

控制器。

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HolidayController {

    @GetMapping("/test11")
    public String getData()
    {

        System.out.println("in the test contoller");
        return "demoJsp";
    }
}

Spring Boot配置文件。

package com.chml.Chml_Admin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EntityScan("com.chml")
@EnableJpaRepositories("com.chml")
@ComponentScan("com.chml")
public class ChmlAdminApplication {

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

}

application.properties。

#==== connect to mysql ======# 

spring.jpa.hibernate.ddl-auto=update 
spring.datasource.url=jdbc:mysql://localhost:3306/chml_admin 
spring.datasource.username=root spring.datasource.password= 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 

#==== Internal View Resolver ======# 

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

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>Demo</title>
 </head>
   <body>
     <h3>This is jsp content</h3>
   </body>
  </html>

pom.xml。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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-web</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>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
  

我在这里添加了我的项目结构

enter image description here

我尝试了各种方法,但是没有得到任何解决方案, 请告诉我在任何地方是否出错。

1 个答案:

答案 0 :(得分:1)

根据屏幕截图,您需要在 application.properties中将视图前缀从views更改为jsps

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