无法在Spring MVC中加载CSS

时间:2017-11-05 11:23:54

标签: java css spring spring-mvc

我尝试创建简单的spring mvc项目,但我的index.jsp不想加载CSS。我搜索了这个问题并尝试了很多东西,但没有任何改变。

我的WebAppConfig:

@Configuration
@ComponentScan(basePackages = {"org.training.ytaranau"})
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public Order order() {
        return new Order();
    }

    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

index.jsp中的链接:

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

Webapp structure

2 个答案:

答案 0 :(得分:0)

问题出在Collection

<强>的web.xml

WebAppConfig.java

我将 WebAppConfig.java 替换为以下文件

<强> /src/main/webapp/WEB-INF/spring-web-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
    <display-name>homework15</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/jsp/errorPages/error404.jsp</location>
    </error-page>
    <error-page>
        <error-code>401</error-code>
        <location>/jsp/errorPages/error401.jsp</location>
    </error-page>
    <servlet>
        <servlet-name>hello-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-web-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>hello-dispatcher</servlet-name>
        <url-pattern>/resources</url-pattern>
    </servlet-mapping>
</web-app>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.training.ytaranau" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/> <mvc:annotation-driven /> </beans> style.csshibernate.cfg.xml出现在errorPageStyles.css目录中。

我在/webapp/resources/进行了一些更改,以便返回error.jsp而不是index.jsp

../index.jsp中出现了一些问题,我不知道问题是什么,但WebAppConfig.java解决了这个问题。

另外,我已经对课程spring-web-config.xml进行了评论,因为不需要它。

这是更新后的项目:

https://drive.google.com/open?id=1Keg3tyu1abVb90GLe9tlnFW-1N3xCszn

答案 1 :(得分:0)

我有一个基于Angular 2的应用程序,它打包在这个文件结构backend/target/appname/WEB-INF/classes/static中,这就是我参考映射的方式。

registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
  

我建议尝试将classpath:添加到位置参考中。

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {  
  registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/");
}
  

建议

您应该考虑使用Git之类的内容来控制版本,使用GitHubBitBucket来备份项目。不像Google云端硬盘。