MVC:资源Spring映射无效

3
我正在尝试将任何请求映射到/views/node_modules/*的路径转换为Spring项目中的/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>

我尝试了这里提供的解决方案:在<mvc:resources/>中使用绝对路径而不是相对于Spring Servlet映射的路径

但是它似乎并没有起作用。


您能提供完整的web.xml文件吗?根据我的理解,您的服务器从默认映射/ views / node_modules /开始,这在本例中并不清楚。您是如何尝试访问这些文件的? - Moshiour
这里是:添加了完整的 web.xml - Nikita Vlasenko
文件从“resources”文件夹中正常提供,但我需要将其他请求(views/node_modules)映射到resources文件夹内的一个文件夹。 - Nikita Vlasenko
4个回答

2
最终解决了。以下线程对我非常有帮助:Use of multiple "mvc:resources" tag in spring mvc
所以,我做了以下几步:
  1. applicationContext.xml中删除<mvc:default-servlet-handler/>并在底部添加<mvc:annotation-driven />
  2. 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 />

我之前也遇到了同样的问题,但最终通过相同的方法解决了。 - Nguyen Tuan Anh

0

你应该将静态URL映射到Spring servlet,而不是默认servlet。因此,你的代码应该是:

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

不,实际上这些都很好用,我已经得到了所有正确的映射。我需要一个不同的映射,一个新的映射。无论如何,我尝试了你所说的方法,但它并没有起作用。此外,它会破坏当前正在使用的从资源到资源的映射。 - Nikita Vlasenko

0

你可以将资源目录保存在目录中

  • NetBeans:Web Pages
  • 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"/>

0
如果您想使用Tomcat和JSP,可以创建一个Web MVC配置并将处理程序注册到/static/**路由。
@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 下。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接