Spring mvc Spring3,基于Java的配置和资源访问问题
我使用的是Spring3,基于java的配置,带有引导 我已经下载了引导程序,并将css和js放在参考资料目录下 问题是我不能在freemarker页面中使用这些.css。 不管我是怎么进口的。 在使用基于java的配置时,我添加了“addResourceHandler”,如下所示: WebAppConfig:Spring mvc Spring3,基于Java的配置和资源访问问题,spring-mvc,spring-3,Spring Mvc,Spring 3,我使用的是Spring3,基于java的配置,带有引导 我已经下载了引导程序,并将css和js放在参考资料目录下 问题是我不能在freemarker页面中使用这些.css。 不管我是怎么进口的。 在使用基于java的配置时,我添加了“addResourceHandler”,如下所示: WebAppConfig: @Configuration @EnableWebMvc @ComponentScan("com.springway") public class WebConfig implement
@Configuration
@EnableWebMvc
@ComponentScan("com.springway")
public class WebConfig implements WebApplicationInitializer {
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.setServletContext(servletContext);
root.scan("com.springway");
root.refresh();
final ServletRegistration.Dynamic servlet = servletContext.addServlet("spring", new DispatcherServlet(root));
servlet.setLoadOnStartup(1);
servlet.addMapping("/*");
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
Tomcat日志说:
警告:找不到URI为的HTTP请求的映射
[/springway/resources/css/bootstrap responsive.css]in
名为“spring”的DispatcherServlet
目录:
-SpringWay
> -src
> - main
> -webapp
> -resources
-WEB-INF
-welcome.ftl
-springway.ftl
欢迎光临
[#ftl /]
[#include "springway.ftl" /]
<ul class="breadcrumb">
<li>
<a href="[@spring.url '/test'/]">Test</a> <span class="divider">/</span>
</li>
<li>
<a href="#">Library</a> <span class="divider">/</span>
</li>
<li class="active">Data</li>
</ul>
[#ftl/]
[#包括“springway.ftl”/]
-
/
-
/
- 数据
springway.ftl:
[#ftl/]
[#import "spring.ftl" as spring /]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
<link href="[@spring.url '/resources/css/bootstrap-responsive.css'/]" rel="stylesheet" type="text/css" media="screen"/>
<link href="[@spring.url '/resources/css/bootstrap-responsive.min.css'/]" rel="stylesheet" type="text/css" media="screen"/>
<link href="[@spring.url '/resources/css/bootstrap.css'/]" rel="stylesheet" type="text/css" media="screen"/>
<link href="[@spring.url '/resources/css/bootstrap.min.css'/]" rel="stylesheet" type="text/css" media="screen"/>
<script src="[@spring.url '/resources/js/bootstrap.js'/]" type="text/javascript"></script>
<script src="[@spring.url '/resources/js/bootstrap.min.js'/]" type="text/javascript"></script>
</head>
<body ></body>
</html>
[#ftl/]
[#将“spring.ftl”导入为spring/]
您定义了错误的资源位置
而不是
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
你应该这么做
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/**");
因为您的css文件夹位于资源文件夹内,所以您需要在/之后添加额外的**,它将标识css文件夹,否则它将仅从资源文件夹加载,不会考虑子文件夹
希望对你有帮助
欢呼。< /P> < P>如果您使用Spring Security,您可以考虑将Webjar添加到授权请求列表中,通过将此代码行添加到Security配置类:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**", "/signup", "/about").permitAll()
**.antMatchers("/webjars/**").permitAll()**
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/signin").permitAll()
.and()
.logout().permitAll();
}
@Configuration
@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**", "/signup", "/about").permitAll()
**.antMatchers("/webjars/**").permitAll()**
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/signin").permitAll()
.and()
//...
.logout().permitAll();
}
如果您使用Spring Security,您可以考虑将Webjar添加到授权请求列表中,通过将该代码行添加到Security配置类:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**", "/signup", "/about").permitAll()
**.antMatchers("/webjars/**").permitAll()**
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/signin").permitAll()
.and()
.logout().permitAll();
}
@Configuration
@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**", "/signup", "/about").permitAll()
**.antMatchers("/webjars/**").permitAll()**
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/signin").permitAll()
.and()
//...
.logout().permitAll();
}
感谢日本的帮助。