SpringBoot无法解析静态资源的路径(404)

时间:2017-07-02 12:22:58

标签: intellij-idea spring-boot

我检查的同样问题的解决方案都没有对我有用。我正在使用Intellij Idea Spring Initializer,它具有明确的项目结构:

的src:    
---主要:          
---------- java:                  
---------------- myproject:                      
------------------------------ config                      
------------------------------控制器                      
------------------------------ dao                      
------------------------------模特                      
------------------------------服务                      
------------------------------  HibernateApplication.java
---------资源:
---------------静态:
---------------模板

这里 HibernateApplication 是一个主要的跑步者类,一个由SpringBoot提供并标记为@SpringBootApplication的标准类; 资源位于目录下,并且从其他示例中可以看出,它充当了web-inf文件夹。
config 中,有一个@Configuration类:

    @Configuration
    public class AppConfig {
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new 
        InternalResourceViewResolver();
        resolver.setPrefix("/resources/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

这是我的 @Controller 类:

@Controller
@RequestMapping("/")
public class ActorController {
    @Autowired
    private ActorService actorService;

    @GetMapping
    public String addActor(){
        //actorService.addActor();
        return "hello";
    }
}

我没有在这里使用我的服务,所有我想做的只是让他得到并回复一个完全位于资源/静态的“ hello.jsp ”文件夹。它的内容很简单:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
Hello from Spring Boot!
</body>
</html>

以下是我的 application.properties

spring.datasource.url = JDBC:MySQL的://本地主机:3306 / sakila的
spring.datasource.username =根
spring.datasource.password =根
spring.jpa.hibernate.ddl-AUTO =创造降
security.basic.enabled =假

我的依赖包括 tomcat-embded-jasper jstl javax.servlet 以及所有必要的spring boot内容。但是,当我运行localhost时:8080 /我收到一个带有意外404错误的whitelabel错误页面。如果我的路径配置为如.setPrefix(“/ resources /”)。setSuffix(“。jsp”),则说它无法解析“/resources/hello.jsp”。如果我将前缀设置为(“/ resources / static /”),则找不到“/resources/static/hello.jsp”。

什么事?当我在该路径上单击Ctrl时,甚至Intellij也可以轻松地解析路径,并将其重定向到“resources”文件夹。

P.S。好的,我发现了一个问题:默认情况下,Intellij创建了这个“myproject”文件夹,并且其中有一个运行者类,我错误地开始在这个文件夹中添加新包。现在我已经解决了这个问题,将所有软件包从这个文件夹中移到'main'文件夹中,只留下“myproject”文件夹里面的runner类。层次结构现在必须正常,但仍然没有在浏览器中返回视图。

1 个答案:

答案 0 :(得分:0)

发现了一个问题  1.缺少一个Thymeleaf依赖  2.控制器,DAO和东西都应该作为SpringBootApplication主类的主文件夹中的子文件夹。我的问题是我的控制器甚至没有在组件扫描路径上  3.视图应放在'templates'文件夹中,而不是像我的情况(错误地)在'static'中。视图(在我的例子中)也必须是html,而不是jsp。
 4.最后我在代码中删除了ViewResolver定义,因为我的html文件现在默认由SpringBoot检测到。

谢谢,现在可以关闭这个问题了。

相关问题