用SPRING MVC显示JSP中的图像

时间:2021-07-18 22:15:19

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. :)

我真的很感激你的回答。:)

6 个解决方案

#1


13  

Any static resource is also look for a URL Mapping in spring mvc, so static resources should be defined in the springmvc-servlet.xml.

任何静态资源都要在springmvc中寻找URL映射,因此静态资源应该在springmvc .xml中定义。

Add the following entry to your MVC configuration. I assume that your static files in resources folder.

将以下条目添加到MVC配置中。我假设您的静态文件在资源文件夹中。

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

then static files can be accessible from the page.

然后可以从页面中访问静态文件。

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

#2


3  

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 ressource和path。

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

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

#3


1  

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应用,它仍然应该作为一个普通的webapp来部署。检查您的部署以确保,并使用浏览器来测试加载。

#4


1  

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

#5


0  

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中完全配置之后。在我的jsp页面中,我使用了这个img src:< img src="projectname/../images/logo.jpg" />,最后显示图像。

#6


0  

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。你应该添加 ,在jsp 用SPRING MVC显示JSP中的图像中,你应该在web-inf下创建一个文件夹,该文件夹命名为images和web。xml您的servlet映射应该类似于 / 模式>。

#1


13  

Any static resource is also look for a URL Mapping in spring mvc, so static resources should be defined in the springmvc-servlet.xml.

任何静态资源都要在springmvc中寻找URL映射,因此静态资源应该在springmvc .xml中定义。

Add the following entry to your MVC configuration. I assume that your static files in resources folder.

将以下条目添加到MVC配置中。我假设您的静态文件在资源文件夹中。

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

then static files can be accessible from the page.

然后可以从页面中访问静态文件。

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

#2


3  

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 ressource和path。

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

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

#3


1  

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应用,它仍然应该作为一个普通的webapp来部署。检查您的部署以确保,并使用浏览器来测试加载。

#4


1  

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

#5


0  

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中完全配置之后。在我的jsp页面中,我使用了这个img src:< img src="projectname/../images/logo.jpg" />,最后显示图像。

#6


0  

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。你应该添加 ,在jsp 用SPRING MVC显示JSP中的图像中,你应该在web-inf下创建一个文件夹,该文件夹命名为images和web。xml您的servlet映射应该类似于 / 模式>。