rah*_*gar 6 spring-mvc spring-boot
我正在尝试使用 spring boot 设置我的应用程序的主页..但我收到错误,因为无法解析名称为“dispatcherServlet”的 servlet 中名称为“index.html”的视图
我的代码如下
运行应用程序
@SpringBootApplication
public class RunApplication {
static Logger logger = Logger.getLogger(RunNavneetApplication.class);
public static void main(String[] args) {
SpringApplication.run(RunNavneetApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序属性
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.html
Run Code Online (Sandbox Code Playgroud)
主控制器.java
@Controller
public class HomeController {
@RequestMapping("/")
public String index() {
return "index";
}
}
Run Code Online (Sandbox Code Playgroud)
配置文件
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题.. 提前非常感谢...
对于我来说(spring-boot-starter-parent 版本 2.0.5.RELEASE),它可以通过以下方式更改路径:
\nsrc/main/resources/WEB-INF/view/index.html\xe2\x86\x92\n src/main/resources/META-INF/resources/view/index.html(由于某种原因,它似乎不喜欢这个WEB-INF名称,并且需要嵌套resources目录)。
它似乎也适用于src/main/资源/资源/view/index.html
在这些情况下,您甚至不需要添加@EnableWebMvc注释并实现WebMvcConfigurer(配置的前缀和后缀application.properties就足够了)。
我在我的答案中还添加了有关 Spring Boot 参考指南中的 MVC 配置的说明:
\n\n\n27.1.1 Spring MVC 自动配置
\n如果您想保留 Spring Boot MVC 功能并且想添加额外的 MVC 配置 [...] 您可以添加您自己的
\n@Configuration类型类WebMvcConfigurer,但不包含@EnableWebMvc.如果你想完全控制Spring MVC,你可以添加你自己的
\n@Configuration注释@EnableWebMvc。76.7 关闭默认 MVC 配置
\n完全控制 MVC 配置的最简单方法是使用
\n@Configuration\@EnableWebMvcnan 符号提供您自己的配置。这样做会将所有 MVC 配置保留在您手中。
在您的代码中您已经定义了后缀和前缀
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.html
Run Code Online (Sandbox Code Playgroud)
所以你请求中的所有页面都会自动转换为html文件,所以你需要更改return "index.html";为return "index";
改成
@Controller
public class HomeController {
@RequestMapping("/")
public String index() {
return "index";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17394 次 |
| 最近记录: |