【问题标题】:Spring Boot unable to render (very) simple index.jspSpring Boot 无法呈现(非常)简单的 index.jsp
【发布时间】:2019-02-15 12:33:33
【问题描述】:

我正在尝试渲染一个只有一个 h1 标记的 html .jsp...感觉它应该可以工作,但它没有。

如何让 index.jsp 文件呈现到我的浏览器中?请帮忙!

我正在使用: Maven 3.5.4 Java 1.8.0_181 STS 版本:3.9.5.RELEASE

<%@ 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";
    }
}

日志

+++++点击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/

块引用

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

【问题讨论】:

  • 您是否尝试删除包 com.coding.test1 中的 @Controller?
  • 将此return "index.jsp" 更改为return "index"
  • @Hana 刚刚尝试过,结果是它没有击中路线。
  • @NicholasK 刚刚尝试过,它仍然成功了,但我没有使用 .jsp 的后缀,所以最终这可能行不通?
  • 向我们展示 ServletInitializer 类在您的项目中的作用。 @NicholasK 提出的解决方案应该可行。

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


【解决方案1】:

在你的 application.properties 中添加这一行

spring.mvc.view.suffix=.jsp

然后改变

return "index.jsp"

return "index"

另外,作为旁注,我建议您将您的 @controller 移至另一个班级。

【讨论】:

  • 抱歉回复您的评论有点早。我确实用后缀尝试过,也没有雪茄,尽管路线仍在成功输入。
  • 奇怪,尝试为您的控制器创建一个单独的类并干净构建项目。
  • 做了一个新的工作空间->新的项目-> 整个shabang都没有用...同样的结果。
  • 您可以尝试另一件事,而不是返回String,而是尝试返回ModelAndView 的对象。然后,您应该将视图设置为您的 index.jsp
【解决方案2】:

这是一个安装错误。不确定是哪些步骤完全修复了它,但我会概述一下,以防有人想遵循。

  • 从 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 (不是用户变量)- 以前是用户变量
  • 在一个全新的工作空间中打开 Eclipse,重新制作 Spring Starter 项目以测试 html 页面的基本渲染

非常感谢此主题的所有人!帮助我走出了黑暗的地方:)。

编辑:样式列表

【讨论】:

    【解决方案3】:

    我试过了,它对我有用。

        <!-- 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>
    

    【讨论】:

    • 刚刚用干净的 proj 构建中的所有依赖项替换了我的所有依赖项,不幸的是它不起作用...我想知道这是否可能是安装错误...虽然它奇怪的是它正确地击中了路线
    • 从本地存储中删除所有 mvn 依赖项,然后更新 maven.. 可能 maven 依赖项安装不正确并且 jar 已损坏。
    【解决方案4】:

    其实你在主课上搞砸了。首先编写您的主类,例如

    @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>
    

    【讨论】:

    • 你能重新链接文件夹结构吗?现在@Controller 有一个错误,它不是注解类型。
    • (1) 扩展SpringBootServletInitializer 只有当他计划在外部Tomcat 上运行它时才需要。他没有说他有。 (2) @EnableAutoConfiguration 不是必需的,因为 @SpringBootApplication 已经暗示了这一点。 (3) 虽然单独的控制器类是个好主意,但这不是必需的。 (4) 文件名为application.properties,而不是application.property
    猜你喜欢
    • 2018-01-13
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多