【问题标题】:Cannot return an HTML view in Spring MVC无法在 Spring MVC 中返回 HTML 视图
【发布时间】:2017-11-12 22:42:46
【问题描述】:

这是我第一次尝试使用 Spring,但遇到了问题。我似乎无法将简单的 HTML 文件作为控制器内的视图返回。我正在尝试返回 index.html 文件(它只有一些文本和一个按钮),因为它在转到 localhost:8080/ 时显示正确。

该项目是使用 IntelliJ 生成的,我想我会提到这一点,因为它处理了大部分配置。

这是文件结构:

app:
 | src
    | main
       | java
          | com.my.testapp
              | Main.java
              | controllers
                  | TestController.java
       | resources
          | static
              | index.html

这是我的文件:

Main.java

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

TestController.java(第一个控制器实际工作)

@RequestMapping("/test")
@Controller
public class TestController
{
    @RequestMapping(method = RequestMethod.GET, value = "/testget")
    public @ResponseBody ArrayList<Test> foo()
    {
        ArrayList<Test> tests = new ArrayList<>();

        tests.add(new Test("id1", "pass1"));
        tests.add(new Test("id2", "pass2"));

        return tests;
    }

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

最后是 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.my</groupId>
    <artifactId>testapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.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>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>
                <configuration>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

我以前使用@RestController 注释,控制器只会返回字符串索引,但现在我得到一个 404 页面(Whitelabel 错误页面)。我知道这是一个常见问题,但我无法使用其他人的解决方案来解决它。

编辑:好的,我设法通过删除@RequestMapping("/test") 行来修复它的一部分,但我现在需要将视图作为“index.html”而不是简单的“index”传递。有没有办法跳过.html?目前没有它是行不通的。

编辑#2:我终于弄清楚了如何使视图正常工作,请在下面查看我的答案。

【问题讨论】:

    标签: java spring spring-boot intellij-idea


    【解决方案1】:

    经过大量无用的 Google 搜索、两个善良的印度帅哥在 YouTube 上解释内容和一些白发后,我终于让应用程序正常运行。这是我发现的所有内容:

    文件结构

    我在问题中发布的结构是正确的,无需更改任何内容。 HTML 视图可以位于src/main/resources/static 下。

    使用控制器返回 HTML 视图

    我从 Controller 类中删除了注释,现在我的简化控制器如下所示:

    @Controller
    public class TestController
    {
        @RequestMapping(value = "/someview")
        public String showView()
        {
            return "index";
        }
    }
    

    您可以通过将注释更改为

    来添加/映射更多 URL 到此控制器

    @RequestMapping(value = {"/someview", "/someotherview", "/onelastview"}).

    现在我可以导航到localhost:8080/someview 并获得index.html 视图(实际上无需在控制器中指定文件扩展名)。

    额外配置

    默认情况下,我的控制器函数需要返回"index.html" 而不是"index"。解决方案是在application.properties 中添加一行。该文件位于(在我的情况下)resources 文件夹下。

    线路是spring.mvc.view.suffix=.html。这指定了视图的类型。您也可以设置spring.mvc.view.prefix=/path/to/views,但我不知道如何设置它并且它仍然有效(我猜它默认为resources/static)。

    这基本上告诉框架使用以下路径获取视图:

    prefix + viewName + suffix,在我的例子中 viewName 是 index(无论我在我的方法中返回什么字符串)。

    Main.java 不需要额外配置,只需 SpringApplication.run() 行。

    【讨论】:

      【解决方案2】:

      只需在showView() 方法的返回语句中尝试"index.html" 而不是"index"。让我知道它是否有效。

      【讨论】:

      • 我们不提供return语句中的扩展。
      • 我认为如果没有.html 扩展,它将无法工作。如果你想省略扩展,你必须做额外的配置。
      • 请在评论前正确阅读 Spring MVC 手册。
      【解决方案3】:

      你需要定义ViewResolver bean 让spring知道你的视图在哪里

      @Bean
      public ViewResolver internalResourceViewResolver() {
          InternalResourceViewResolver bean = new InternalResourceViewResolver();
          bean.setViewClass(JstlView.class);
          bean.setPrefix("/WEB-INF/view/");
          bean.setSuffix(".html");
          return bean;
      }
      

      【讨论】:

      • 扩展名是 .html 为什么你要配置 .jsp
      • hmm 我认为这只是一个示例,OP 会自己修复它,所以我将其保留为默认值。然后我会编辑
      • 我试过这个,但用“resources/static/”更改了“/WEB-INF/view/”(参见上面的结构),但我得到以下异常:java.lang.ClassNotFoundException:javax。 servlet.jsp.jstl.core.Config
      • 好的,我为 Jstl 添加了 maven 依赖,它不再抛出异常,但我现在又得到 404。
      • 试着把你的html放到src/main/webapp/WEB-INF/view文件夹下
      猜你喜欢
      • 1970-01-01
      • 2010-11-15
      • 2016-01-08
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多