Java 使用Spring3提供静态内容
我试图使用Spring3的资源映射功能,但它似乎不起作用。以下是我所拥有的:Java 使用Spring3提供静态内容,java,model-view-controller,spring,spring-mvc,Java,Model View Controller,Spring,Spring Mvc,我试图使用Spring3的资源映射功能,但它似乎不起作用。以下是我所拥有的: <servlet> <servlet-name>aaa</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup&
<servlet>
<servlet-name>aaa</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>aaa</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
尝试更换您的
与
您正在将应用程序映射到根上下文,因此您可能应该包括
<mvc:default-servlet-handler/>
在mvc配置中。请查看中的15.12.5。当我的DispatcherServlet映射到/时,如果没有该设置,我就无法让mvc:resources工作。至少,我记得几个月前在我的项目中配置它时,我必须这么做。修复了它
看起来ResourceHttpRequestHandler中有一个bug,它只与Weblogic一起出现
要解决此问题,请执行以下操作:
除去
<resources mapping="/resources/**" location="/resources/"/>
加
我知道有些人建议使用默认的servlet处理程序,但必须删除资源映射才能使其工作 您是否尝试过添加
?是否尝试过只使用普通链接而不使用c:url@杰克:在这个场景中,mvc:annotation-driven的目的是什么?@Ralph我不完全确定:P。但我知道我遇到了一个非常类似的问题,添加
神奇地解决了这个问题。这里有更详细的描述:我已经使用了I get Error 404——未找到我已经尝试用常规链接替换c:url相同的错误如果我尝试将链接localhost:7001/XXX/resources/css/…css直接放入浏览器中我已经使用了I get Error 404——未找到我已经尝试用常规链接替换c:url相同的错误如果我尝试放置直接链接到浏览器
${title}
的链接,如果我查看源代码,css链接会生成以下内容:
,其中XXX是应用程序上下文。我实际上已经尝试过使用此链接,尽管使用此链接并将服务器映射到/?无论哪种方式,拥有这条线都没什么区别。它一点都不多余。仅当您将dispatcherservlet映射到根上下文时才需要它。文档中的第一行表示“此标记允许将DispatcherServlet映射到“/”(从而覆盖容器默认Servlet的映射),同时仍然允许容器默认Servlet处理静态资源请求。”使用它时不使用任何“默认Servlet名称”将根据容器使用一个合理的默认值,但是如果您对默认servlet有其他内容,您也可以配置它。所以。。。如果必须删除资源映射,如何将目录映射为静态资源?
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/jsps directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/jsps/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basename" value="messages"/>
</beans:bean>
<!-- Imports user-defined @Controller beans that process client requests -->
<beans:import resource="controllers.xml" />
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="com.app.controller" />
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:default-servlet-handler/>
<resources mapping="/resources/**" location="/resources/"/>
<default-servlet-handler/>