【问题标题】:Spring Boot: Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'Spring Boot:无法解析名称为“dispatcherServlet”的servlet中名称为“index”的视图
【发布时间】:2018-12-20 13:20:18
【问题描述】:

我正在尝试使用 spring boot 设置我的应用程序的主页。但我收到错误,因为 无法解析名称为“dispatcherServlet”的 servlet 中名称为“index.html”的视图

我的代码如下

RunApplication.java

@SpringBootApplication
public class RunApplication {

    static Logger logger = Logger.getLogger(RunNavneetApplication.class);

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

application.properties

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.html

HomeController.java

@Controller 
public class HomeController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

WebConfig.java

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/productImages/**")
        .addResourceLocations("file:/home/rahul/Desktop/product_images/")
        .setCachePeriod(0);
    }
}

pom.xml

<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.wocs</groupId>
  <artifactId>REST</artifactId>
  <version>0.0.1-SNAPSHOT</version>


<properties>
<java-version>1.8</java-version>
<service-version>0.1.36-SNAPSHOT</service-version>
</properties>  

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

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

    <dependency>
          <groupId>com.wocs</groupId>
          <artifactId>services</artifactId>
          <version>${service-version}</version>
    </dependency>

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

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

</dependencies>

请帮我解决这个问题.. 非常感谢提前...

【问题讨论】:

  • 你的问题解决了吗?

标签: spring-boot spring-mvc


【解决方案1】:

在您的代码中,您已经定义了后缀和前缀

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.html

所以您请求中的所有页面都会自动转换为html文件,所以您需要将return "index.html";更改为return "index";

改成

@Controller 
public class HomeController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

【讨论】:

  • javax.servlet.ServletException:无法解析名称为“dispatcherServlet”的 servlet 中名称为“index”的视图
  • 我试过了,但错误是一样的...我已经编辑了问题...请检查
  • 请求映射应该使用@Controller注解,如
【解决方案2】:

对我来说(spring-boot-starter-parent 版本 2.0.5.RELEASE)它以这种方式改变了路径:

src/main/resources/WEB-INF/view/index.htmlsrc/main/resources/META-INF/resources/view/index.html(由于某种原因,它似乎不喜欢 WEB-INF 名称,它需要嵌套的 resources 目录)。

它似乎也适用于src/main/resources/resources/view/index.html

在这些情况下,您甚至不需要添加@EnableWebMvc 注释并实现WebMvcConfigurerapplication.properties 中配置的前缀和后缀就足够了)。

我在回答中还添加了关于从 Spring Boot 参考指南中获取的 MVC 配置的说明:

27.1.1 Spring MVC 自动配置

如果您想保留 Spring Boot MVC 功能并且想要添加额外的 MVC 配置 [...] 您可以添加您自己的 @Configuration 类型为 WebMvcConfigurer 的类,但 没有 @987654334 @。

如果您想完全控制 Spring MVC,可以添加自己的 @Configuration,并带有 @EnableWebMvc 注释。

76.7 关闭默认 MVC 配置

完全控制 MVC 配置的最简单方法是为您自己的 @Configuration 提供 @EnableWebMvc 注解。这样做会将所有 MVC 配置留在您的手中。

【讨论】:

    【解决方案3】:

    在我的 Spring Boot 应用程序中执行 CURL 请求时出现此错误:

    {"timestamp":1566549840380,"status":500,"error":"内部服务器 错误","消息":"无法解析名称为 'createApiKey' 的视图 名称为 'dispatcherServlet'","path":"/createApiKey"}

    的 servlet

    我通过从@Controller 更改为@RestController(它是@Controller 和@ResponseBody 的组合)来修复它,如下所示:

    @RestController
    public class ApiKeyController {
        ...
        @GetMapping("/createApiKey")
        public DeferredResult<CreateApiKeyResp> createApiKey(@RequestParam long userId) {
            ...
    

    【讨论】:

      【解决方案4】:

      在 Spring Boot 项目中删除 @EnableWebMvc 注释对我有用。

      【讨论】:

        猜你喜欢
        • 2018-12-13
        • 1970-01-01
        • 2020-12-11
        • 2014-05-27
        • 2016-12-27
        • 2015-08-06
        • 1970-01-01
        • 1970-01-01
        • 2018-06-02
        相关资源
        最近更新 更多