我正在YouTube指南上学习春季)我很想尝试启动自己的项目,但遇到了一些问题。目录/ static / **中的资源未提供给外部世界-404错误。 / static /位于/ src / main / resources /
我尝试过: 添加
spring.resources.static-locations=/js/,/css/
spring.mvc.view.prefix=/static/css/
spring.mvc.view.suffix=.css
在application.properties中-不变。
当我添加
registry.addResourceHandler("/img/**").addResourceLocations("file:///"+uploadPath+"/");
到
public void addResourceHandlers(ResourceHandlerRegistry registry)
如果我将.js文件添加到/ static /或/ static / js,IDEA通常会将调用从我的代码链接到脚本。但是,当应用程序启动时,脚本不起作用。
@Configuration
public class MvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}}
WebSecurityConfig
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/users","/static/**").permitAll()
//.anyRequest().authenticated()
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.rememberMe()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws
Exception {
auth.userDetailsService(userService)
.passwordEncoder(passwordEncoder);
}
}
application.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url=jdbc:postgresql://localhost:5432/banking
spring.session.store-type=jdbc
spring.session.jdbc.initialize-schema=always
spring.datasource.username=postgres
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=false
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.freemarker.expose-request-attributes=true
项目结构
├───main
│ ├───java
│ │ └───web
│ │ ├───configs
│ │ ├───controllers
│ │ ├───domain
│ │ ├───Repositories
│ │ ├───service
│ │ └───validator
│ └───resources
│ └───templates
│ ├───parts
│ └───static
│ ├───css
│ └───js
└───test
└───java
答案 0 :(得分:0)
请从
中移动目录 src/main/resources/templates/static/
到
src/main/resources/static/
默认情况下,Spring Boot将从类路径中名为/ static(或/ public或/ resources或/ META-INF / resources)的文件夹中或ServletContext的根目录中提供静态内容
谢谢