Java 使用 SPRING MVC 在 JSP 中显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20369529/
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
Display image in JSP with SPRING MVC
提问by Phuu792
I am trying to display an image on a jsp
.
My image file is located at
我正在尝试在jsp
. 我的图像文件位于
MyApp/WebContent/images/logo.jpg
And my JSP pages are located at
我的 JSP 页面位于
MyApp/WebContent/WEB-INF/view/home.jsp
I have already tried to use the image by
我已经尝试通过以下方式使用图像
<'img src="<%=request.getContextPath()%>/images/logo.jpg" />
and
和
<'img src="<'c:url value='<%=request.getContextPath()%>/images/logo.jpg'></c:url></img>
Is this issue something because of my location hierarchy where I have placed my image?
这个问题是否是因为我放置图像的位置层次结构?
Really appreciate your help. Thank you.
真的很感谢你的帮助。谢谢你。
UPDATE:
更新:
I've found the solution to my problem in: http://www.tutorialspoint.com/spring/spring_static_pages_example.htm
我在以下位置找到了解决我的问题的方法:http: //www.tutorialspoint.com/spring/spring_static_pages_example.htm
I just have to use resource mapping in my servlet.xml
.
我只需要在我的servlet.xml
.
I really appreciate all of your kind answers. :)
我真的很感谢你所有的友好回答。:)
采纳答案by erencan
Any static resource is also look for a URL Mapping in spring mvc, so static resources should be defined in the springmvc-servlet.xml
.
任何静态资源也是在 spring mvc 中寻找 URL Mapping,所以静态资源应该在springmvc-servlet.xml
.
Add the following entry to your MVC configuration. I assume that your static files in resources
folder.
将以下条目添加到您的 MVC 配置中。我假设您的文件resources
夹中的静态文件。
<mvc:resources mapping="/resources/**" location="/resources/" />
then static files can be accessible from the page.
然后可以从页面访问静态文件。
<img src="/resources/images/logo.jpg" />
回答by Scary Wombat
try
尝试
<img src="/MyApp/WebContent/images/logo.jpg" />
Even though it is a Spring MVC app, it should still deploy as a normal webapp. Check your deployment to make sure, and also use the browser to test loading.
即使它是一个 Spring MVC 应用程序,它仍然应该作为一个普通的 web 应用程序进行部署。检查您的部署以确保,并使用浏览器测试加载。
回答by Jér?me Gloaguen
To avoid to have to indicate explicitly the context path you can use jstl core and do it like that
为了避免必须明确指示上下文路径,您可以使用 jstl core 并这样做
<img src="<c:url value="/images/logo.jpg"/>"/>
You can also check this thread about spring ressource and path
您还可以查看有关 spring 资源和路径的此线程
回答by Kyle
I put images folder under WEB-INF directory, after did fully configuration in the spring-dispatcher-servlet.xml file, I used this img src:< img src="projectname/../images/logo.jpg" /> in my jsp page, images display finally.
我把images文件夹放在WEB-INF目录下,在spring-dispatcher-servlet.xml文件中完全配置后,我使用了这个img src:< img src=" projectname/../images/logo.jpg" />我的jsp页面,图片终于显示出来了。
回答by Laurent Duvergé
To make it work I had to do in spring config:
为了使它工作,我必须在 spring 配置中做:
<mvc:resources mapping="/resources/**" location="/resources/" />
In JSP:
在 JSP 中:
<spring:url value="/resources/images" var="images" />
<img src="${images}/back.png"/>
回答by Nefise ?z?ahin
in springmvc-servlet.xml you should add <mvc:resources location="/WEB-INF/images/" mapping="/images/**" />
and in jsp <img src="images/logo.jpg" />
and you should create a folder under web-inf which is named images and in the web.xml your servlet mapping shoul be like that <url-pattern>/</url-pattern>
.
在 springmvc-servlet.xml 中,您应该<mvc:resources location="/WEB-INF/images/" mapping="/images/**" />
在 jsp 中添加和,<img src="images/logo.jpg" />
并且应该在 web-inf 下创建一个名为 images 的文件夹,在 web.xml 中,您的 servlet 映射应该是这样的<url-pattern>/</url-pattern>
。