java mvc:resources Spring 映射不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38035245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:02:47  来源:igfitidea点击:

mvc:resources Spring mapping not working

javaspring

提问by Nikita Vlasenko

I am trying to map any requests that come to /views/node_modules/*to /resources/angular_resources/*folder in my Spring project. How could I achieve this?

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

Currently I have applicationContext.xmlfile that looks the following way:

目前我有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/>

Full web.xml:

完整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>

I tried the solution here: Absolute path in <mvc:resources/> instead of path relative to mapping of spring servlet

我在这里尝试了解决方案:<mvc:resources/> 中的绝对路径而不是相对于 spring servlet 映射的路径

But it does not work somehow.

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

回答by Nikita Vlasenko

Ultimately solved it. The following thread really helped me: Use of multiple "mvc:resources" tag in spring mvc.

最终解决了它。以下线程确实帮助了我:在 spring mvc 中使用多个“mvc:resources”标签

So, I did the following:

所以,我做了以下事情:

  1. Remove <mvc:default-servlet-handler/>from applicationContext.xmland add <mvc:annotation-driven />at the bottom instead
  2. Remove defaultservlet stuff from web.xml
  1. <mvc:default-servlet-handler/>从底部移除applicationContext.xml<mvc:annotation-driven />在底部添加
  2. default从中删除servlet 内容web.xml

After doing this I was able to add additional mappings to applicationContext.xml. Here it is:

完成此操作后,我能够将其他映射添加到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 />

回答by Shailesh Sonare

You can keep resources directory in Directory

您可以将资源目录保留在目录中

  • NetBeans: Web Pages
  • Eclipse: webapps
  • NetBeans:网页
  • Eclipse:网络应用程序

File: dispatcher-servlet.xml

文件:调度程序-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>

File: web.xml

文件: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>

In JSP File

在 JSP 文件中

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

回答by MUNGAI NJOROGE

If you would like to use tomcat and jsp. You can create a web MVC configurations and register handler to /static/** route

如果你想使用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 file can then be accessed from /static/{URL}. Note that in my case I used jasper, tomcat and jstl and static resources were placed under src/java/main/webapp

然后可以从/static/{URL}. 请注意,在我的情况下,我使用了 jasper、tomcat 和 jstl,并且静态资源放置在src/java/main/webapp 下

回答by Shankar P S

You should map the static URLs to the Spring servlet, not the default servlet. So your code should be,

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

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