spring 3 , tiles can not display image or use css style
I have deleloped a simple spring 3 application following a tutorial. After that, I did integrated tiles 2. Right now I am trying to use css style in my tiles but i can not do this and i can not even display an image.
Inside my tiles.xml I have:
<?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>
Inside layout.jsp I have
<%@ 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>
What I want is to remove the table structure and use css. For this I use on layout.jsp the following:
<link type="text/css" rel="stylesheet" href="<%=request.getContextPath()%>/style style.css"/>
Now inside my header.jsp I have:
<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>
None of the imaeges are getting displayed here and i do not understand why.
The folder styles which cotnrains css and images is in the rood folder of the war.
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"
My web.xml contains the following if od any help:
<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>
Any help would be appreciated.
Thanks a lot, giannis
It's not related to Tiles.
Since DispatcherServlet
is configured to handle all URL in your application, you need to configure it to handle requests for static content.
Since Spring 3.0.4 it can be done by adding <mvc:resources>
to your Spring config (<mvc:annotation-driven />
is also needed):
<mvc:resources location="/styles/" mapping="/styles/**" />
See also:
- 15.12.4 mvc:resources
It looks that your main problem is that the server is not able to provide your static content (images, 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"
What you need is a configured mapping for your static content in the spring configuration:
<!-- Handles HTTP GET requests for by efficiently serving up static resources -->
<mvc:resources location="/styles/" mapping="/styles/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
For more details: look at chapter "15.12.4 mvc:resources" of the spring reference
Added Of course you need to declare the mvc prefix:
<?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">
Make sure you are using spring 3.0.4 or later otherwise the tag mvc:resources
will not work, as it was introduced with that version according to the book "Spring in Action" book.
精彩评论