gpt4 book ai didi

java - 使用 Spring 3 提供静态内容

转载 作者:IT老高 更新时间:2023-10-28 13:47:21 25 4
gpt4 key购买 nike

我正在尝试使用 Spring 3 的资源映射功能,但它似乎不起作用。这是我所拥有的:

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

在我的 web.xml 中

然后在我的 aaa-servlet.xml 我有以下内容:

<resources mapping="/resources/**" location="/resources/" />

我正在像这样访问 jsp 中的内容:

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

我一直在阅读的所有内容都表明我已正确设置了所有内容,但它无法正常工作。我正在使用 weblogic 服务器,并且在启动时它会映射/resources/文件夹。

任何帮助将不胜感激!

aaa-servlet.xml 全文:

<?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" />

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:default-servlet-handler/>

在你的 mvc 配置中。查看 spring docs 中的 15.12.5 .当我的调度程序 servlet 映射到/时,如果没有该设置,我无法让 mvc:resources 工作。至少,当我几个月前在我的项目中配置它时,我似乎记得这就是我必须做的事情。

关于java - 使用 Spring 3 提供静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6846692/

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