【问题标题】:spring 3 , tiles can not display image or use css stylespring 3,tiles不能显示图像或使用css样式
【发布时间】:2011-02-15 16:49:31
【问题描述】:

我按照教程开发了一个简单的 spring 3 应用程序。之后,我做了集成图块 2。现在我正在尝试在我的图块中使用 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>

这里没有显示任何图像,我不明白为什么。

包含css和图片的文件夹样式在war的rood文件夹中。

我在控制台中也收到一条错误消息,提示“在名为 spring 的调度程序 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>

任何帮助将不胜感激。

非常感谢, 詹尼斯

【问题讨论】:

  • 你能下载图片和css(手动)吗?

标签: jsp spring-mvc tiles


【解决方案1】:

它与 Tiles 无关。

由于DispatcherServlet 已配置为处理应用程序中的所有 URL,因此您需要将其配置为处理静态内容请求。

从 Spring 3.0.4 开始,可以通过在 Spring 配置中添加 &lt;mvc:resources&gt; 来完成(还需要 &lt;mvc:annotation-driven /&gt;):

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

另请参阅:

【讨论】:

【解决方案2】:

看来您的主要问题是服务器无法提供您的静态内容(图像、css):

我在控制台中也收到一条错误消息,提示“在名为 spring 的调度程序 servlet 中找不到带有 uri /pedsample/styles/images/en.gif 的 HTTP 请求的映射”

您需要的是在 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">

【讨论】:

  • 非常感谢你们俩。我将更详细地查看它并在这里更新。
  • 好的,伙计们。我现在遇到以下问题:当我尝试部署应用程序时,出现以下错误:313 [http-8080-1] 错误 org.springframework.web.servlet.DispatcherServlet - 上下文初始化失败 org.springframework.beans.factory。 xml.XmlBeanDefinitionStoreException:来自 ServletContext 资源 [/WEB-INF/spring-servlet.xml] 的 XML 文档中的第 21 行已编辑异常是 org.xml.sax.SAXParseException:元素“mvc:resources”的前缀“mvc”不是边界。在我的 spring-servlet.mxl 我有: 再次感谢伙计们
  • @user529065:查看我的扩展答案
【解决方案3】:

确保您使用的是 spring 3.0.4 或更高版本,否则标签 mvc:resources 将不起作用,因为它是根据《Spring in Action》一书在该版本中引入的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多