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

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

Display image in JSP with SPRING MVC

javaimagejspurlspring-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 resourcesfolder.

将以下条目添加到您的 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 资源和路径的此线程

Spring 3 MVC resources and tag <mvc:resources />

Spring 3 MVC 资源和标签 <mvc:resources />

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

回答by Daniel Movemann

TRY THIS ! ALWAYS WORKS FINE !

尝试这个 !总是工作正常!

  1. Create your img folder at src/main/resources
  2. Copy the picture inside this folder called "img"
  3. Write inside
  4. Use this picture inside
  1. 在 src/main/resources 创建你的 img 文件夹
  2. 将图片复制到名为“img”的文件夹中
  3. 写在里面
  4. 在里面使用这张图片

check the screenshots and enjoy !

检查屏幕截图并享受!

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明