2011-02-15 42 views
2

我已經在教程後刪除了一個簡單的spring 3應用程序。之後,我做了整合瓷磚2.現在我正嘗試在我的瓷磚中使用CSS樣式,但我無法做到這一點,我甚至不能顯示圖像。spring 3,瓷磚無法顯示圖像或使用css風格

裏面我tiles.xml我:

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE tiles-definitions PUBLIC 
    "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" 
    "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> 
<tiles-definitions> 
<definition name="base.definition" 
    template="/WEB-INF/jsp/layout.jsp"> 
    <put-attribute name="title" value="" /> 
    <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" /> 
    <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" /> 
    <put-attribute name="body" value="" /> 
    <put-attribute name="styles" value="base.css"/> 
    <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" /> 
</definition> 

<definition name="dogBreed" extends="base.definition"> 
    <put-attribute name="title" value="Contact Manager" /> 
    <put-attribute name="body" value="/WEB-INF/jsp/dogBreed.jsp" /> 
</definition> 

</tiles-definitions> 

裏面layout.jsp我

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<link type="text/css" rel="stylesheet" href="<%=request.getContextPath()%>/styles/style.css"/> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title><tiles:insertAttribute name="title" ignore="true" /></title> 
</head> 
<body> 

<table border="1" cellspacing="0" cellpadding="0" style="width:100%;height:100%"> 
    <tr> 
     <td height=10% width=100%><tiles:insertAttribute name="header" /> 
     </td> 
    </tr> 
    <tr> 
     <td height=80% width=20%><tiles:insertAttribute name="menu" /></td> 
     <td width=80% height=80%><tiles:insertAttribute name="body" /></td> 
    </tr> 
    <tr> 
     <td height=10% width=100%><tiles:insertAttribute name="footer" /> 
     </td> 
    </tr> 
</table> 
</body> 
</html> 

我要的是刪除表的結構和使用的CSS。爲此,我使用上layout.jsp如下:

<link type="text/css" rel="stylesheet" href="<%=request.getContextPath()%>/style style.css"/> 

現在我的header.jsp裏面我有:越來越此處顯示

<div id="header"> 

    <div class="top_right"> 

     <div class="languages"> 
      <div class="lang_text">Languages:</div> 
      <a href="#" class="lang"><img src="<%=request.getContextPath()%>/styles/images/en.gif" alt="" title="" border="0" /></a> 
      <a href="#" class="lang"><img src="styles/images/de.gif" alt="" title="" border="0" /></a>  
     </div> 

     <div class="big_banner"> 
     <a href="#"><img src="images/banner728.jpg" alt="" title="" border="0" /></a> 
     </div> 

    </div> 


    <div id="logo"> 
     <a href="index.html"><img src="images/logo.png" alt="" title="" border="0" width="182" height="85" /></a> 
    </div> 
    </div> 

的imaeges都沒有,我不明白爲什麼。

cotnrains css和images的文件夾樣式位於戰爭的rood文件夾中。

而且我在控制檯得到一個錯誤說「不映射發現與調度的servlet URI /pedsample/styles/images/en.gif與名泉HTTP請求」

我的web.xml中包含以下如果有任何幫助:

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

任何幫助,將不勝感激。

非常感謝, giannis

+0

你可以下載圖像和CSS(手動)? – Ralph 2011-02-15 16:52:20

回答

1

它不涉及到瓷磚。

由於DispatcherServlet被配置爲處理應用程序中的所有URL,因此需要對其進行配置以處理對靜態內容的請求。

自Spring 3.0以來。4可通過增加<mvc:resources>到您的Spring配置來完成(還需要<mvc:annotation-driven />):

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

參見:

+0

有人可以指點我一個這樣的例子嗎? – giannisapi 2011-02-16 09:54:33

+0

@ user529065:http://blog.springsource.com/2010/07/22/spring-mvc-3-showcase/。另請參閱http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-annotation-driven例如您需要在您的頭部進行的更改XML文件。 – axtavt 2011-02-16 10:02:29

0

它看起來你的主要問題是服務器無法提供您的靜態內容(圖像,CSS):

Also I get an error in my console saying that "no mapping found for HTTP request with uri /pedsample/styles/images/en.gif in dispatcher servlet with name spring"

你需要的是在春季配置您的靜態內容配置的映射:

<!-- Handles HTTP GET requests for by efficiently serving up static resources --> 
<mvc:resources location="/styles/" mapping="/styles/**"/> 
<mvc:resources location="/images/" mapping="/images/**"/> 

有關詳細信息:看章"15.12.4 mvc:resources" of the spring reference

新增 當然,你需要聲明mvc前綴:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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"> 
0

確保您使用的是spring 3.0.4或更高版本,否則標籤mvc:resources將不起作用,因爲它是根據「Spring in Action」一書中的該版本引入的。