我由YouTube指南STUDING春季)Resenty我tryied开始我自己的项目,并得到了一些问题。 404错误 - 从目录/静态/ **不会提供给外部世界的资源。 /静态/位于/ src目录/主/资源/
我想:添加
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文件添加到/静态/或/静态/ 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
请只要将您的目录从
src/main/resources/templates/static/
至
src/main/resources/static/
默认情况下,春季启动将在类路径或从ServletContext的根提供静态内容从一个名为/静态的文件夹(或/公共或/资源或/ META-INF /资源)
谢谢