Java 无法在Spring MVC中返回HTML视图

Java 无法在Spring MVC中返回HTML视图,java,spring,spring-boot,intellij-idea,Java,Spring,Spring Boot,Intellij Idea,这是我第一次尝试使用Spring,但遇到了一个问题。我似乎无法将简单的HTML文件作为控制器内的视图返回。我试图返回index.html文件(它只有一些文本和一个按钮),因为它在转到localhost:8080/时显示正确 该项目是使用IntelliJ生成的,我想我会提到这一点,因为它处理了大部分配置 以下是文件结构: app: | src | main | java | com.my.testapp | Main.java

这是我第一次尝试使用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);
    }
}
java(第一个控制器实际工作)

@RequestMapping(“/test”)
@控制器
公共类测试控制器
{
@RequestMapping(method=RequestMethod.GET,value=“/testget”)
public@ResponseBody ArrayList foo()
{
ArrayList tests=新的ArrayList();
添加(新测试(“id1”、“pass1”));
添加(新测试(“id2”、“pass2”));
回归测试;
}
@RequestMapping(method=RequestMethod.GET,value=“/someview”)
公共字符串showView()
{
返回“索引”;
}
}
最后是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>

4.0.0
com.my
特斯塔普
0.0.1-快照
罐子
弹簧试验
SpringBoot的演示项目
org.springframework.boot
spring启动程序父级
1.5.8.1发布
UTF-8
UTF-8
1.8
org.springframework.boot
SpringBootStarterWeb
mysql
mysql连接器java
运行时
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件
真的
我以前使用@RestController注释,控制器只返回字符串索引,但现在我得到一个404页面(白标签错误页面)。我知道这是一个常见的问题,但我一直无法使用其他人的解决方案来解决它

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


编辑#2:我终于找到了如何使视图正常工作的方法,请检查下面的答案。

您需要定义
viewsolver
bean,让spring知道您的视图在哪里

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

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

经过大量无用的谷歌搜索,两个善良的印度家伙在YouTube上解释了一些东西,还有几根白发,我终于让应用程序正常运行了。以下是我发现的一切:

文件结构

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

使用控制器返回HTML视图

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

@Controller
public class TestController
{
    @RequestMapping(value = "/someview")
    public String showView()
    {
        return "index";
    }
}
通过将注释更改为,可以将更多URL添加/映射到此控制器

@RequestMapping(值={/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()
行。

我们不在return语句中提供扩展。我认为没有
.html
扩展,它将无法工作。如果你想省略这个扩展,你必须做额外的配置。在评论之前,请仔细阅读Spring MVC手册。extension is.html why your configure.jsphmm我认为这只是一个例子,OP会自己修复它,所以我将它保留为默认值。我将编辑,然后我尝试了这个,但将“/WEB-INF/view/”改为“resources/static/”(参见上面的结构),但我得到了以下异常:java.lang.ClassNotFoundException:javax.servlet.jsp.jstl.core.ConfigOk,我为jstl添加了maven依赖项,它不再抛出异常,但是我现在又得到了404。试着把你的html放在
src/main/webapp/WEB-INF/view
文件夹下
@Controller
public class TestController
{
    @RequestMapping(value = "/someview")
    public String showView()
    {
        return "index";
    }
}