视图文件映射如何在Spring

时间:2018-03-11 11:54:48

标签: spring spring-tool-suite

我不明白Spring如何将视图映射到文件(例如“主页”视图到“Home.jsp”)。在Websphere上,您可以在XML文件中找到它。在Spring上,我看到您将端点映射到Java类中的控制器上的视图,但我实际上无法找到如何选择将特定视图映射到特定文件。

谢谢

编辑1: 似乎我的第一个问题是没有启用JSP支持。这是我的 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.springframework</groupId>
    <artifactId>gs-serving-web-content</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


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

</project>

如果我添加:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

然后我的 pom.xml

出现错误
The managed version is 8.5.27 The artifact is managed in org.springframework.boot:spring-boot-dependencies:1.5.10.RELEASE

报告的文件是 org.springframework.boot:spring-boot-dependencies:1.5.10.RELEASE.pom ,行报告为:

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <version>${tomcat.version}</version>
</dependency>

编辑2:

请考虑一下,即使我设置了spring.mvc.view.suffix:.html,当我尝试访问http://localhost:8080/greeting时收到错误500。这是我的控制台错误:

2018-03-11 21:24:40.923 ERROR 4892 --- [nio-8080-exec-5] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-5] Exception processing template "greeting": Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers
2018-03-11 21:24:40.924 ERROR 4892 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers

1 个答案:

答案 0 :(得分:0)

如果您希望Spring查找您的视图文件,您需要向Spring ViewResolver提供两件事:
前缀 - 所有搜索开始的文件夹的路径 后缀 - 扩展您的观点。

您可以通过xml:

执行此操作
<bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
              <value>/WEB-INF/pages/</value>
           </property>
          <property name="suffix">
             <value>.jsp</value>
          </property>
    </bean>

通过 application.properties

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

通过Java Config:

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/pages/");
    resolver.setSuffix(".jsp");
    return resolver;
}

因此,如果你有文件 /WEB-INF/jsp/home.jsp ,Spring可以找到只有hello字符串的文件。

如果您在不同的文件夹中有两个 hello.jsp ,则它不会成为问题,因为您需要明确告知您的视图所在的文件夹(例如,您将指定{ {1}}或return "/foo/hello"