簡體   English   中英

在Spring 4項目中包括CSS和JavaScript文件

[英]Including css and javascript files in spring 4 project

我正在使用Spring 4,Spring安全模塊和tomcat 8制作一個Web應用程序。我試圖在.jsp文件中包含一些css文件和js文件,但是它不起作用。 當我在Chrome中簽入source標簽時,css的內容似乎是登錄表單。 我懷疑這可能與春季安全性有關。

我的css文件像這樣包含在.jsp中

<link href="<c:url value='resources/css/materialize.min.css' />" rel="stylesheet"
        type="text/css"></link>

這是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();
    }
}

這是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();
    }
}

根據此處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>

我所有的CSS和JS文件都位於WebContent /資源中

我解決了這個問題,顯然在我的一個控制器中路由不明確,因此當我嘗試訪問以“ / resources”開頭的網址時,將其路由到控制器,從而返回了.jsp而不是css / js /圖片。 我原來的控制器將URL綁定到@Controller中,並保留@RequestMapping而不指示路由。

@Controller("/signup")
public class SignUpController {
    @RequestMapping(method=RequestMethod.GET)
    public String signUpForm(){
        ...
    }
    @RequestMapping(method=RequestMethod.POST)
    public String crearUsuario(HttpServletRequest request){
        ...
    }

因此,我更改了@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM