gpt4 book ai didi

spring-boot - Spring Boot : Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'

转载 作者:行者123 更新时间:2023-12-03 16:40:36 63 4
gpt4 key购买 nike

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

我的代码如下

运行应用程序

@SpringBootApplication
public class RunApplication {

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

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

应用程序属性
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.html

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

配置文件
@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-starter-parent version 2.0.5.RELEASE)它以这种方式改变了路径:src/main/resources/网络信息 /view/index.htmlsrc/main/resources/ META-INF/资源 /view/index.html (出于某种原因,它似乎不喜欢 WEB-INF 名称,它需要嵌套的 resources 目录)。
它似乎也适用于 src/main/ 资源/资源 /view/index.html在这些情况下,您甚至不需要添加 @EnableWebMvc注释和实现 WebMvcConfigurer (在application.properties中配置的前缀和后缀就足够了)。
我还在我的回答中添加了关于从 Spring Boot 引用指南中获取的 MVC 配置的说明:

27.1.1 Spring MVC Auto-configuration

If you want to keep Spring Boot MVC features and you want to add additional MVC configuration [...] you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

76.7 Switch off the Default MVC Configuration

The easiest way to take complete control over MVC configuration is to provide your own @Configuration with the @EnableWebMvcannotation. Doing so leaves all MVC configuration in your hands.

关于spring-boot - Spring Boot : Could not resolve view with name 'index' in servlet with name 'dispatcherServlet' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51303453/

63 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com