spring 3 ,tiles无法显示图像或使用css样式

发布于 2024-10-17 15:45:53 字数 4038 浏览 6 评论 0原文

我按照教程开发了一个简单的 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和图像的文件夹styles位于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>

任何帮助将不胜感激。

多谢, 扬尼斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2024-10-24 15:45:54

它与瓷砖无关。

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

从 Spring 3.0.4 开始,可以通过将 添加到 Spring 配置中来完成(还需要 ) :

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

另请参阅:

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:

成熟稳重的好男人 2024-10-24 15:45:54

看来您的主要问题是服务器无法提供您的静态内容(图像、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/**"/>

有关更多详细信息:请参阅章节 spring 参考的“15.12.4 mvc:resources”

添加
当然你需要声明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">

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">
痞味浪人 2024-10-24 15:45:54

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文