在我使用Maven开发的Spring应用程序中,我的资源文件(来自boostrap和jquery的css和js文件)都存储在src / main / resources文件夹中。在我的视图中(jsp文件,存储在src / main / webapp / WEB-INF / jsp中)我以这种方式引用这个文件:
<!-- Bootstrap core CSS -->
<link href="<c:out value="${pageContext.request.contextPath}/resources/bootstrap/css/bootstrap.min.css"/>" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="<c:out value="${pageContext.request.contextPath}/resources/extras/css/signin.css"/>" rel="stylesheet">
哪些不起作用,因为当我打开页面时,浏览器无法从服务器下载此文件(触发错误404)。
在我的jsp文件中链接到这些文件的正确表单是什么?
ps。:在我的配置类中,我对资源文件进行了此设置:
WebConfig.java
@EnableWebMvc
@ComponentScan(value="spring.example")
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/jquery-ui/**").addResourceLocations("/resources/jquery-ui/").setCachePeriod(31556926);
registry.addResourceHandler("/resources/bootstrap/**").addResourceLocations("/resources/bootstrap/").setCachePeriod(31556926);
registry.addResourceHandler("/resources/extras/**").addResourceLocations("/resources/extras/").setCachePeriod(31556926);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
SecurityConfig.java
@Configuration
@ComponentScan(value="spring.example")
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService usuario;
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(usuario)
.passwordEncoder(encoder());
}
private Md5PasswordEncoder encoder() {
return new Md5PasswordEncoder();
}
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/acesso/erro").permitAll()
.antMatchers("/resources/bootstrap/**", "/resources/jquery-ui/**", "/resources/extras/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/acesso/login").permitAll()
.loginProcessingUrl("/login").permitAll()
.usernameParameter("login")
.passwordParameter("senha")
.defaultSuccessUrl("/acesso/start")
.failureUrl("/acesso/login")
.and()
.rememberMe()
.key("lembrete")
.useSecureCookie(true)
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/acesso/login").permitAll();
}
}
答案 0 :(得分:1)
您需要将JS / CSS文件存储在应用的webapp
文件夹中。 Maven的src/main/resources
文件夹仅用于无法从客户端访问的资源(此资源通常由Java应用程序使用,而不是由客户端浏览器使用)。所以只需将文件移动到webapp
文件夹并从JSP