如何从jsp访问图像? [英] How to access the images from jsp?
问题描述
我正在使用Spring MVC 3.0。
I am using Spring MVC 3.0.
我必须加载与 WEB-INF
目录平行的文件夹images中存在的图像。
I have to load the images which exist in the folder "images" parallel to the WEB-INF
directory.
我在 WEB-INF / jsp
文件夹中有jsp文件。
I have the jsp files in WEB-INF/jsp
folder.
文件夹结构为:
-app
--images
--WEB-INF
---jsp
---classes
...
在web.xml中, DispatcherServlet
的url映射有点像
In web.xml the url mapping for DispatcherServlet
is some thing like
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
现在在JSP中,如果我按以下方式加载jsp:
Now in JSP if I load the jsp in the following way:
<img src="<%=request.getContextPath()%>/images/calogo.jpg" />
它现在作为 DispatcherServlet
拦截它我想。
Its now working as DispatcherServlet
is intercepting it I guess.
推荐答案
是的,你是对的。您的调度员正在拦截您显示图像的请求。
Yes you are right. Your dispatcher is intercepting your request to display the images.
根据我的说法,当您尝试访问jsp文件中的图像时,它会给您404错误。
According to me when you try to access the image in your jsp file it will give you 404 error.
您需要在servlet.xml文件中包含以下代码行。
You need to include the following line of code in your servlet.xml file.
<mvc:resources location="/images/" mapping="/images/**" />
然后一切正常。
希望这会对你有所帮助。
Hope this helps you.
干杯。
这篇关于如何从jsp访问图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!