2013-03-08 90 views
5

顯示在JSP我是新用JSP到春天我基本上想在我的JSP來顯示圖像圖像不帶彈簧

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
     <title>JSP-Page</title> 
    </head> 
    <body> 
     <img src="images/top.jpg"> 
    </body> 
</html> 

Spring的servlet的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<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.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <!-- Enable annotation driven controllers, validation etc... --> 
    <mvc:annotation-driven /> 
<mvc:resources location="/images/" mapping="/images/**"/> 

    <!-- Application controllers package --> 
    <context:component-scan base-package="net.ignou.onlinetest.controller" /> 

    <bean id="viewoseesolver" 
       class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/views/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
       destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" 
          value="jdbc:mysql://localhost:3306/online_test" /> 
     <property name="username" value="root" /> 
     <property name="password" value="root" /> 
    </bean> 

    <bean id="sessionFactory" 
       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>net.ignou.onlinetest.domain.Question</value> 
       <value>net.ignou.onlinetest.domain.Student</value> 
       <value>net.ignou.onlinetest.domain.Answer</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">none</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="multipartResolver" 
       class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    </bean> 

    <bean id="questionDao" class="net.ignou.onlinetest.dao.daoImpl.QuestionDaoImpl"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
    <bean id="loginDao" class="net.ignou.onlinetest.dao.daoImpl.LoginDaoImpl"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

    <bean id="answerDao" class="net.ignou.onlinetest.dao.daoImpl.AnswerDaoImpl"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

    <bean id="service" class="net.ignou.onlinetest.service.serviceImpl.ServiceImpl"> 
     <property name="questionDao" ref="questionDao"/> 
    </bean> 

</beans> 

我的網頁。 XML

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Person Detail</display-name> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
<welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    </web-app> 

我使用maven.My圖像文件位於

在線測試的\ src \主\ web應用程序\圖片 和我的jsp頁面中

在線測試的\ src \主\ web應用\ WEB-INF \觀點

我也試過更換SRC作爲<img src="../../images/top.jpg">但它沒有工作,我也嘗試將我的jsp和圖像直接移動到webapp文件夾也沒用。有沒有什麼做錯了彈簧如何處理img請求?

+0

它不是處理圖片請求的彈簧,而是您的瀏覽器。你有春天的資源處理程序映射嗎? – 2013-03-08 17:11:12

+0

不,我只查看解析器.. – Lakshmi 2013-03-08 17:13:11

+0

第1步:./images/等 – 2013-03-08 17:15:54

回答

12

你想要做的就是在你的spring servlet-context xml配置中添加這行。

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

mvc XML命名空間爲xmlns:mvc="http://www.springframework.org/schema/mvc"

resources標籤基本上告訴Spring從申報位置服務了一個名爲文件,而不是通過你的控制器棧去處理請求的聲明映射。該映射也可用於提供任何資源:css,js,pdf等。

您不需要多個<mvc:resources>標記,只有一個具有通用映射,例如。/resources/**和逗號分隔的位置列表,例如。/resources/css /,/ resources/js /。

<mvc:resources mapping="/resources/**" location="/resources/images/, /resources/css/" /> 

<resources>標籤是在春天3.0.4推出,所以你至少需要那個版本春和XSD的。您可以使用

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd` 

而且,JB Nizet說,你應該引用您的形象

<img src="<c:url value='/images/top.jpg'/>"/> 

的相對路徑。

+0

@Lakshmi什麼是您的Web應用程序的上下文根?並把你的完整的servlet上下文配置放在你的問題中,這樣我們就可以看到一切是否有序。 – 2013-03-08 17:33:08

+0

在我的問題中添加了代碼還有一件事我得到了關於添加mvc資源的SaxParserException:雖然我已經添加了模式定義,但這是原因匹配通配符是嚴格的,但是沒有可以爲元素'mvc:resources'找到聲明 – Lakshmi 2013-03-08 17:42:40

+0

@ Lakshmi使用'http://www.springframework.org/schema/mvc http:// www.springframework.org/schema/mvc/spring-mvc.xsd'。 'resources'標籤在3.0.4春季推出。因爲'mvc'的模式位置沒有後綴'-3.0'。 – 2013-03-08 17:52:25

4

當涉及到URL路徑時,JSP文件的位置是無關緊要的。重要的是瀏覽器顯示的頁面位置,即顯示在瀏覽器地址欄中的頁面地址。

因此,如果頁面的地址是/webapp/foo/bar/someAction.html和圖像在/webapp/images/top.jpg,路徑應該是/webapp/images/top.jpg(絕對路徑,preferrable和更清晰),或../../images/top.jpg(相對路徑,難以重構,如果你將文件移動或更改網址)。

我的建議是:總是使用絕對路徑,並使用JSTL的C:url標記,以避免硬編碼的web應用的上下文路徑:

<img src="<c:url value='/images/top.jpg'/>"/> 

上面一行將始終工作。

+0

沒有工作單獨嘗試,並且還與 Sotirios Delimanolis解。 – Lakshmi 2013-03-08 17:50:22

+0

+1爲您的良好實踐建議:) – Lakshmi 2013-03-08 18:43:51