简体   繁体   English

使用SPRING MVC在JSP中显示图像

[英]Display image in JSP with SPRING MVC

I am trying to display an image on a jsp . 我正在尝试在jsp上显示图像。 My image file is located at 我的图片文件位于

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. 非常感谢您的所有答复。 :) :)

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映射,因此应该在springmvc-servlet.xml定义静态资源。

Add the following entry to your MVC configuration. 将以下条目添加到您的MVC配置。 I assume that your static files in resources folder. 我假设您的静态文件位于resources文件夹中。

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

then static files can be accessible from the page. 然后可以从页面访问静态文件。

<img src="/resources/images/logo.jpg" />

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

try 尝试

<img src="/MyApp/WebContent/images/logo.jpg" />

Even though it is a Spring MVC app, it should still deploy as a normal webapp. 即使它是Spring MVC应用程序,它仍应作为普通Web应用程序进行部署。 Check your deployment to make sure, and also use the browser to test loading. 检查您的部署以确保安全,并使用浏览器测试加载。

To make it work I had to do in spring config: 为了使其工作,我必须在spring config中做:

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

In JSP: 在JSP中:

<spring:url value="/resources/images" var="images" />
    <img src="${images}/back.png"/>

我将images文件夹放在WEB-INF目录下,在spring-dispatcher-servlet.xml文件中进行了完整配置之后,我使用了以下img src:<img src =“ projectname /../ images / logo.jpg ” />我的jsp页面上,图像终于显示了。

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

TRY THIS ! 尝试这个 ! ALWAYS WORKS FINE ! 总是很不错!

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

check the screenshots and enjoy ! 检查截图并享受!

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM