2015-09-29 151 views
1

我对Spring很陌生。我使用spring 3.29版本,tomcat 7.我需要在.jsp文件中显示图像。我搜索了很多。有很多关于这个问题的文章。但是我仍然无法解决这个问题。请帮忙。如何在Spring MVC中的jsp文件中显示图像

,以下是我的应用程序结构

The following is my application structure

以下是我的春天-servlet.xml文件代码

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 

    <context:component-scan base-package="com.wipro.controller" /> 
    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass"> 
      <value> 
       org.springframework.web.servlet.view.tiles2.TilesView 
      </value> 
     </property> 
    </bean> 
    <bean id="tilesConfigurer" 
     class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> 
     <property name="definitions"> 
      <list> 
       <value>/WEB-INF/tiles.xml</value> 
      </list> 
     </property> 
    </bean> 
</beans> 

我有一个页面footer.jsp中。我需要在这个文件中添加图像 以下是代码

<!-- Here I need to add an image --> 
<hr/> 
<p>Copyright 2010-2014 javatpoint.com.</p> 
+0

?静态或基于用户登录后? –

+0

静态图像。我已经放置在图像文件夹中。谢谢你的时间。请帮忙。 – Abdul

+0

将你的images文件夹移动到WebContent不是WEB-INF,然后你可以用HTML方式将它添加到 vincent

回答

3

我在页面上得到了图像。 @Pankaj Saboo的评论帮了很大忙。谢谢你们所有人。

我已经添加了下面的代码为spring-servlet.xml

<mvc:annotation-driven /> 
<mvc:resources mapping="/images/**" location="WEB-INF/resources/images/" /> 

而在footer.jsp中我已经添加了下面的代码

你想要哪种类型的图像显示
<img src="<c:url value="/images/wipLogo.png" />"/> 
2

添加按照您为spring-servlet.xml

<resources mapping="/images/**" location="/images/" /> 

和JSP线

<link rel="icon" 
href="http://<hostname>/projectname/images/imageName"/> 
+0

一旦我添加这行 。我收到一些错误。无效的内容... – Abdul

+0

您放置此行的位置 –

+0

在上述代码的下一行之后 – Abdul

1

尝试增加以下资源向您的Spring配置声明:

<!-- Handles HTTP GET requests for /images/** by efficiently serving up static resources in the ${webappRoot}/images directory --> 
<resources mapping="/images/**" location="/images/" /> 

或者,更常见的是,有一个资源文件夹,其中包含所有资源(图像,CSS,JS等),由子目录分解。然后

您的配置看起来像:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

和你的资源将如下参考:

<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/screen.css" />" /> 
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.4.min.js" />"></script> 
<img src="<c:url value="/resources/images/left_arrow.png" />"