【问题标题】:Spring boot unable to resolve html viewSpring Boot 无法解析 html 视图
【发布时间】:2015-02-12 10:23:20
【问题描述】:

我使用 eclipse 创建了一个 Spring Starter 项目,并且只选中了 Web 依赖项复选框。我写了一个简单的 html 文件和一个控制器,但我总是得到 Whitelabel Error Page。我没有使用任何模板引擎,此时我也不想使用任何模板引擎。稍后我们将使用 AngularJS 作为前端,但目前我只想解析一个 http 页面。

控制器:

package demo.web;

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

@Controller
public class MyController {

    @RequestMapping(value="/")
    public String goHome(){
        return "home";
    }
}

主类:

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

@SpringBootApplication
public class SpringBootTest5Application {

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

home.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

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>org.test</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>demo.SpringBootTest5Application</start-class>
        <java.version>1.7</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-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>

文件夹结构

【问题讨论】:

    标签: java angularjs spring maven spring-mvc


    【解决方案1】:
    <body data-ng-cloak>
    

    XML 无效且 Thymeleaf 验证输出,您可以将 Thymeleaf 模板模式更改为“旧版 HTML5”或将视图 (home.html) 更改为

    <body data-ng-cloak="">
    

    【讨论】:

      【解决方案2】:

      我猜你缺少对模板引擎的依赖,例如 Thymeleaf。 将其添加到您的 POM 中就足够了:

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      

      【讨论】:

      • 这确实有效,但是,我不希望使用任何模板引擎。我们最终将使用的 index.html 文件包含以下行(AngularJS 标记): =" 符号。
      • 我认为如果没有模板引擎,return "home"; 将永远无法工作,您将不得不在不使用控制器的情况下处理对主页的访问
      • Tome,你能解释一下为什么你必须有一个模板引擎来返回一个 html 文件(“home”)吗?如果您添加一些信息来支持该断言,此答案将非常有帮助。
      • OP 已将home.html 文件放在templates 目录中,这是必须由模板引擎处理的文件的常用位置(如Thymeleaf thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html)。如果您只需要提供静态内容,Spring Boot 会自动配置一个 ResourceHandler 以便直接提供在 staticdirectory 中的内容。
      【解决方案3】:

      您的 spring-context 是否具有资源映射? 事实上页面“home”不存在,我想你有 home.jsp 在像“jsp”这样的文件夹中。 您需要配置您的 spring-context.xml 以详细说明控制器方法的返回字符串,以获得您要显示的页面的正确路径。例如:

      <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
          <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <beans:property name="prefix" value="/WEB-INF/views/" />
              <beans:property name="suffix" value=".jsp" />
          </beans:bean>
      

      这个 bean 告诉 Spring 获取 Controller 输出,为文件夹添加前缀,并将文件扩展名作为后缀以获取正确的资源。

      【讨论】:

      • Spring Boot 配置了一个可以通过 spring.view.prefix 和 spring.view.suffix 配置的 InternalResourceViewResolver。不幸的是,它对我不起作用。添加一个模板引擎就可以了,但后来我又回到了我原来的问题,即模板引擎并没有真正理解 AngularJS 标签。我接受了 Zeronex 的回答,显然解决方案比尝试完全不使用模板引擎要简单得多。
      猜你喜欢
      • 1970-01-01
      • 2014-08-27
      • 2017-07-08
      • 2017-12-01
      • 1970-01-01
      • 2022-11-16
      • 2018-02-10
      • 2020-12-11
      • 1970-01-01
      相关资源
      最近更新 更多