mvc:resources Spring映射不起作用

时间:2016-06-26 04:38:20

标签: java spring

我正在尝试映射Spring项目中/views/node_modules/*/resources/angular_resources/*文件夹的所有请求。我怎么能做到这一点?

目前我的applicationContext.xml文件的方式如下:

<context:component-scan
    base-package="fomoapp.domain, fomoapp.dao, fomoapp.service" />

<!-- Root Context: defines shared resources visible to all other web components -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/views/node_modules/**" location="/resources/angular_resources/" />
<mvc:default-servlet-handler/>

完整web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/META-INF/spring/applicationContext.xml
    classpath*:/META-INF/spring/applicationContext-security.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
  <servlet-name>fomoapp</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/fomoapp-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>fomoapp</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/views/node_modules/</url-pattern>
</servlet-mapping>

我在这里尝试了解决方案:Absolute path in <mvc:resources/> instead of path relative to mapping of spring servlet

但它不会以某种方式起作用。

4 个答案:

答案 0 :(得分:2)

最终解决了它。以下主题确实帮助了我:Use of multiple "mvc:resources" tag in spring mvc

所以,我做了以下事情:

  1. <mvc:default-servlet-handler/>移除applicationContext.xml并在底部添加<mvc:annotation-driven />
  2. default
  3. 删除web.xml个servlet内容

    执行此操作后,我可以向applicationContext.xml添加其他映射。这是:

    <context:component-scan
        base-package="fomoapp.domain, fomoapp.dao, fomoapp.service" />
    
    <!-- Root Context: defines shared resources visible to all other web components -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:resources mapping="/views/node_modules/@angular/**" location="/resources/angular_resources/@angular/" />
    <mvc:resources mapping="/views/node_modules/rxjs/**" location="/resources/angular_resources/rxjs/" />
    
    <mvc:annotation-driven />
    

答案 1 :(得分:0)

您应该将静态URL映射到Spring servlet,而不是默认的servlet。 所以你的代码应该是,

<servlet-mapping>
    <servlet-name>fomoapp</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

答案 2 :(得分:0)

您可以在目录

中保留资源目录
  • NetBeans:网页
  • Eclipse:webapps

文件:dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:component-scan base-package="controller" />

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <mvc:resources location="/resources/theme_name/" mapping="/resources/**"  cache-period="10000"/>
    <mvc:annotation-driven/>

</beans>

文件:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
        <url-pattern>*.css</url-pattern>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

在JSP文件中

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

答案 3 :(得分:0)

如果你想使用tomcat和jsp。您可以创建Web MVC配置并将处理程序注册到/ static / ** route

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };
    /**
     *
     * @return
     */
    @Bean
    public ViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();
        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/views/");

        bean.setSuffix(".jsp");
        return bean;
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
    }
}

然后可以从/static/{URL}访问静态文件。请注意,在我的情况下,我使用jasper,tomcat和jstl,静态资源放在 src / java / main / webapp