【问题标题】:Prevent requests for css and js files to go through Dispatcher servlet防止对 css 和 js 文件的请求通过 Dispatcher servlet
【发布时间】:2015-12-27 07:42:48
【问题描述】:

请有人告诉我如何防止对 css 和 js 文件的请求通过调度程序,我使用的是 spring MVC,我得到“No mapping found for HTTP request with URI”:

déc. 26, 2015 5:24:51 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
Avertissement: No mapping found for HTTP request with URI [/spr-mvc-hib/css/style2.css] in DispatcherServlet with name 'dispatcher'
déc. 26, 2015 5:24:51 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
Avertissement: No mapping found for HTTP request with URI [/spr-mvc-hib/css/style.css] in DispatcherServlet with name 'dispatcher'
déc. 26, 2015 5:24:51 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
Avertissement: No mapping found for HTTP request with URI [/spr-mvc-hib/predict_it.png] in DispatcherServlet with name 'dispatcher'
déc. 26, 2015 5:24:51 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
Avertissement: No mapping found for HTTP request with URI [/spr-mvc-hib/js/index.js] in DispatcherServlet with name 'dispatcher'
déc. 26, 2015 5:24:51 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
Avertissement: No mapping found for HTTP request with URI [/spr-mvc-hib/js/index1.js] in DispatcherServlet with name 'dispatcher'
déc. 26, 2015 5:24:51 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
Avertissement: No mapping found for HTTP request with URI [/spr-mvc-hib/predict_it.png] in DispatcherServlet with name 'dispatcher'

这是我的配置:

WebAppConfig 类:

package com.sprhib.init;
import java.util.Properties;
import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@ComponentScan("com.sprhib")
@EnableWebMvc
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
public class WebAppConfig {

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";

    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
        dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
        dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
        dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));

        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource());
        sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
        sessionFactoryBean.setHibernateProperties(hibProperties());
        return sessionFactoryBean;
    }

    private Properties hibProperties() {
        Properties properties = new Properties();
        properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
        properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));


        return properties;  
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

}

初始化器

package com.sprhib.init;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class Initializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext)
            throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        //ctx.register(WebAppConfig.class);
        ctx.register(com.sprhib.init.WebAppConfig.class);
        servletContext.addListener(new ContextLoaderListener(ctx));
        ctx.setServletContext(servletContext);

        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }

}

这里有更多信息:

my project tree, I made resources in two locations,nothing works :(

Here is inspection on browser :

【问题讨论】:

    标签: java spring-mvc


    【解决方案1】:

    基本上,您需要将静态内容的资源处理程序映射添加到调度程序配置中。 这将告诉调度程序在响应中发送静态资源。类似的东西:

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
        registry.addResourceHandler("/css/**")
         .addResourceLocations("/css/");
        registry.addResourceHandler("/img/**")
         .addResourceLocations("/img/");
        registry.addResourceHandler("/js/**")
         .addResourceLocations("/js/");
    
    }
    

    【讨论】:

    • 谢谢,这帮助我避免了这个错误,但是css和js文件还没有加载:(,这是同一个问题没有回答here@Nighthacks
    • @YoussefElRhailani,检查您的资源路径,如果您没有收到任何错误,这意味着映射正确但您的资源不在正确的位置。
    • 感谢您回答@Nighthacks,我尽了最大努力但徒劳无功:\ 我真的尝试了所有可能的位置但没有结果,非常感谢=)
    • @YoussefElRhailani,您在浏览器控制台中看到任何错误吗?如果您可以发布资源的目录结构,我可能会提供帮助。
    • 谢谢 Nighthacks,我解决了这个问题,我只是没有使用“”,所以它引用了根项目... 非常感谢您的帮助 =)
    猜你喜欢
    • 2014-09-18
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2016-10-07
    • 2016-06-30
    • 1970-01-01
    • 2012-05-26
    • 2011-02-16
    相关资源
    最近更新 更多