Spring Boot无法呈现(非常)简单的index.jsp

时间:2018-09-11 04:14:34

标签: java spring spring-mvc jsp spring-boot

我正在尝试呈现仅带有h1标记的html .jsp ...感觉像它应该可以工作,但是不能。

如何将index.jsp文件呈现到浏览器中?请帮助!

我正在使用: Maven 3.5.4 Java 1.8.0_181 STS版本:3.9.5发行

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1> hello</h1>
</body>
</html>

我成功登录到路由被命中,但未读取index.jsp。

package com.coding.test1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
@Controller
public class Test1Application {

    public static void main(String[] args) {
        SpringApplication.run(Test1Application.class, args);
    }
    @RequestMapping("/")
    public String index() {
        System.out.println("+++++hit main+++++++");
        return "index.jsp";
    }
}

日志

  

+++++ hit main +++++++

浏览器显示

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Sep 10 20:49:47 PDT 2018
There was an unexpected error (type=Not Found, status=404).
No message available

application.properties

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

Blockquote

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.coding.test1</groupId>
    <artifactId>test1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>test1</name>
    <description>test1</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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-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-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>
    </dependencies>

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


</project>

文件夹结构 https://cdn.discordapp.com/attachments/477766353709826054/488922584260411392/unknown.png (没有足够的代表来张贴我的目录图片)

编辑: 添加ServletInitializer.java

package com.coding.test1;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

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

}

记录资源:https://github.com/GNouchi/SO-52268550/blob/master/README.md

4 个答案:

答案 0 :(得分:2)

在您的application.properties中添加此行

spring.mvc.view.suffix=.jsp

并更改

return "index.jsp"

return "index"

此外,作为一个旁注,我建议您将@controller移至另一堂课。

答案 1 :(得分:1)

这是一个安装错误。不确定哪个步骤可以完全解决它,但是我会概述一下,以防任何人遵循。

  • 从C:\ users {username}删除.m2
  • 已删除apache-maven-3.5.4
  • 已删除maven的路径已删除JAVA_HOME下载该文件的新副本 从http://maven.apache.org/download.cgi
  • 压缩
  • 解压缩为所需 夹。
  • 将路径添加到{文件夹结构} /apache-maven-3.5.4/bin
  • 在SYSTEM VARIABLES下添加指向jdk文件夹(不是bin)的JAVA_HOME (不是用户变量)-以前是用户变量
  • 在全新的工作区remake Spring Starter项目中打开Eclipse以测试html页面的基本呈现方式

非常感谢大家对此话题的关注!帮助我走出黑暗的地方:)。

编辑:样式列表

答案 2 :(得分:0)

我尝试了这个,对我有用。

    <!-- This is a web application -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Tomcat embedded container-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- JSTL for JSP -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <!-- Need this to compile JSP -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Need this to compile JSP, 
        tomcat-embed-jasper version is not working, no idea why -->
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- Optional, test for static content, bootstrap CSS-->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>

</dependencies>

答案 3 :(得分:0)

实际上,您在主要班级中陷入困境。首先像

这样编写您的主类
@SpringBootApplication
@EnableAutoConfiguration
public class Test1Application  extends SpringBootServletInitializer{

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

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

现在创建一个类似https://i.stack.imgur.com/EWMAW.png

的项目结构

现在在com.coding.test1下创建一个程序包现在在该新程序包上编写一个控制器

@Controller
public class Controller {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String gotoHome() {
        return "index";
    }

现在像这样写application.properties

spring.mvc.view.prefix: /WEB-INF/
spring.mvc.view.suffix: .jsp

我觉得不需要这种依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>