我检查过的同样问题的解决方案都不适用于我。我使用的IntelliJ IDEA春天初始化程序,其中有一个明确的项目结构:SpringBoot无法解析静态资源的路径(404)
源:
---主营:
----------的java:
----- ----------- myproject:
------------------------------ config
- ----------------------------控制器
------------------- ----------- dao
------------------------------ model
--- ---------------------------服务
------------------------------ HibernateApplication.java
--------- resources:
---------------静:
---------------模板
这里HibernateApplication是主浇道类,一个由SpringBoot提供的标准@SpringBootApplication; 资源位于主要目录下,并且,据我从其他的例子看,它作为一个WEB-INF文件夹中。
在配置,有一个@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-自动=创建降
security.basic.enabled =假
我的依赖性包括Tomcat的embded-碧玉,JSTL,的javax.servlet和所有必要的春天启动的东西。然而,当我运行本地主机:8080 /我发现了一个白色标签错误页面意外404错误。如果我的路径配置一样
.setPrefix( “/资源/”)。setSuffix( “JSP”),它说,它无法解析 “/resources/hello.jsp”。如果我将前缀设置为(“/ resources/static /”),则无法找到“/resources/static/hello.jsp”。
怎么回事?即使我在该路径上单击Ctrl,Intellij也很容易解决此问题,并将其重定向到“资源”文件夹。
P.S.好的,我发现了一个问题:默认情况下,Intellij已经创建了这个“myproject”文件夹,并且里面有runner类,我错误地开始在这个文件夹中添加新的包。现在我已经解决了这个问题,将所有包从这个文件夹移到'main'文件夹,只留下里面的runner类的“myproject”文件夹。现在层次结构必须正常,但浏览器中仍然没有返回视图。
在路径中尝试删除/资源并检查为弹簧启动默认资源文件夹 – Automator1992