[英]Including css and javascript files in spring 4 project
I'm making a web app using Spring 4, the Spring security module and tomcat 8. I'm trying to include some css files and js files in a .jsp file, but it's not working. 我正在使用Spring 4,Spring安全模块和tomcat 8制作一个Web应用程序。我试图在.jsp文件中包含一些css文件和js文件,但是它不起作用。 When I check in the sources tag in Chrome the content of the css seems to be a log in form.
当我在Chrome中签入source标签时,css的内容似乎是登录表单。 I suspect that it may have something to do with spring security.
我怀疑这可能与春季安全性有关。
My css file is included like this in the .jsp 我的css文件像这样包含在.jsp中
<link href="<c:url value='resources/css/materialize.min.css' />" rel="stylesheet"
type="text/css"></link>
This is the WebConfig file 这是WebConfig文件
@Configuration
@ComponentScan(basePackages = "mypackage")
@EnableWebMvc
@EnableTransactionManagement
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Autowired
@Bean
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
This is the SecurityConfig file 这是SecurityConfig文件
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/js/**", "/resources/css/**", "/resources/img/**", "/resources/font/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().loginPage("/signin")
.failureUrl("/signin?param.error=bad_credentials")
.and().logout().logoutUrl("/signout")
.and().authorizeRequests()
.antMatchers("/favicon.ico", "/resources/css/**", "/resources/font/**",
"/resources/js/**", "/auth/**", "/signin/**", "/signup/**", "/disconnect/facebook").permitAll()
.antMatchers("/**").authenticated()
.and()
.rememberMe().
and().csrf();
}
}
According to other answers here in stackoverflow it should work with the code that I have but the css only returns this: 根据此处stackoverflow中的其他答案,它应该与我拥有的代码一起工作,但是css仅返回以下代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:social="http://spring.io/springsocial"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id="content" >
<form id="signin" action="signup" method="post">
<input type="hidden" name="" value=""/>
<div class="formInfo">
</div>
<fieldset>
<label for="login">Email</label>
<input id="login" name="email" type="text" size="25"></input>
<label for="Nombre">Email</label>
<input id="nombre" name="nombre" type="text" size="25"></input>
<label for="password">Password</label>
<input id="password" name="contrasena" type="password" size="25"></input>
</fieldset>
<button type="submit">Sign In</button>
<p>Or you can <a href="signin">signin</a> with a new account.</p>
</form>
</div>
All my css and js files are inside WebContent/resources 我所有的CSS和JS文件都位于WebContent /资源中
I solved the problem, apparently there was an ambiguous routing in one of my controllers, so when I tried to access a url that started with "/resources" it routed it to the controller, and thus returned a .jsp instead of the css/js/image. 我解决了这个问题,显然在我的一个控制器中路由不明确,因此当我尝试访问以“ / resources”开头的网址时,将其路由到控制器,从而返回了.jsp而不是css / js /图片。 My original controller binded the url in the @Controller, and left the @RequestMapping without indicating the route.
我原来的控制器将URL绑定到@Controller中,并保留@RequestMapping而不指示路由。
@Controller("/signup")
public class SignUpController {
@RequestMapping(method=RequestMethod.GET)
public String signUpForm(){
...
}
@RequestMapping(method=RequestMethod.POST)
public String crearUsuario(HttpServletRequest request){
...
}
So I changed the @Controller annotation, and put the url in each @RequestMapping like this: 因此,我更改了@Controller批注,并将URL放在每个@RequestMapping中,如下所示:
@Controller
public class SignUpController {
@RequestMapping(value="/signup", method=RequestMethod.GET)
public String signUpForm(){}
@RequestMapping(value="/signup",method=RequestMethod.POST)
public String crearUsuario(HttpServletRequest request){}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.