gpt4 book ai didi

java - MVC :resources Spring mapping not working

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:14:34 24 4
gpt4 key购买 nike

我正在尝试将到达 /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>

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

但不知何故它不起作用。

最佳答案

终于解决了。以下线程确实帮助了我:Use of multiple "mvc:resources" tag in spring mvc .

所以,我做了以下事情:

  1. 删除 <mvc:default-servlet-handler/>来自 applicationContext.xml并添加 <mvc:annotation-driven />而不是在底部
  2. 删除 default来自 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 />

关于java - MVC :resources Spring mapping not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38035245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com