SpringBoot+jsp项目启动出现404

2019-02-28 23:08:36

通过maven创建springboot项目启动出现404

  • application.properties配置
  1. spring.mvc.view.prefix=/WEB-INF/jsp/
  2. spring.mvc.view.suffix=.jsp
  • 项目结构

  • 控制器方法
  1. package com.example.demo.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. public class IndexController {
  6. @RequestMapping("/")
  7. public String index() {
  8. return "index";
  9. }
  10. }
  • 启动项目访问localhost:8080,出现404
  1. Whitelabel Error Page
  2. This application has no explicit mapping for /error, so you are seeing this as a fallback.
  3. Thu Feb 28 22:59:29 CST 2019
  4. There was an unexpected error (type=Not Found, status=404).
  5. No message available

解决方法

  • pom.xml添加依赖
  1. <dependency>
  2. <groupId>org.apache.tomcat.embed</groupId>
  3. <artifactId>tomcat-embed-jasper</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>javax.servlet</groupId>
  7. <artifactId>jstl</artifactId>
  8. </dependency>
  • clean并刷新maven

  • 重启并访问localhost:8080

打包为jar运行仍然出现404

  • 打包插件版本设置为1.4.2.RELEASE,并且配置好资源目录
  1. <build>
  2. <resources>
  3. <resource>
  4. <directory>src/main/webapp</directory>
  5. <!--这里必须是META-INF/resources-->
  6. <targetPath>META-INF/resources</targetPath>
  7. <includes>
  8. <include>**/**</include>
  9. </includes>
  10. <filtering>false</filtering>
  11. </resource>
  12. </resources>
  13. <plugins>
  14. <plugin>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-maven-plugin</artifactId>
  17. <version>1.4.2.RELEASE</version>
  18. </plugin>
  19. </plugins>
  20. </build>

0
2
0

添加评论

正在回复:
取消
0
0
2
0