Spring MVC不显示图像 [英] Spring MVC doesn't show images

查看:96
本文介绍了Spring MVC不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天. 我已经创建了mvc-dipatcher-servlet.xml

Good day. I have created mvc-dipatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">

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

    <context:component-scan base-package="com.springapp.mvc"/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles2.TilesView
            </value>
        </property>
    </bean>
    <bean id="tilesConfigurer"
          class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

</beans>

在我的Web应用程序中,我创建了静态"文件夹,并在其中创建了图像,js,css文件夹.

In my webapp I create "static" folder, and images, js, css folders in it.

这是我的web.xml:

Here is my web.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>Spring MVC Application</display-name>

        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>

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

        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>/static/css/*</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>/static/js/*</url-pattern>
        </servlet-mapping>
    </web-app>

还有tile.xml:

And 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="footer" value="/WEB-INF/jsp/footer.jsp" />
    </definition>

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

</tiles-definitions>

如果我使用mvc:resourses删除行-我的网站可以运行,但是<img src="<c:url value="/images/header_icon_02.png"/>" alt="" />标记不显示图像.但是我通过

If I delete row with mvc:resourses - my site works, but <img src="<c:url value="/images/header_icon_02.png"/>" alt="" /> tag doesn't show images. But css/js files witch I include by

<link rel="stylesheet" type="text/css" href="<c:url value="static/css/style.css"/>" />

工作正常.

如果我添加mvc:resourses-网站总是返回404页面.如何在我的项目中显示静态图像?

If I add mvc:resourses - site always return 404 for page. How can I display static images in my project???

P.S.
这是我的控制器:

P.S.
Here is my controller:

@Controller
public class HelloController {
    @RequestMapping(value="/", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "contact";
    }
}

推荐答案

我认为你们在解决问题方面都做得很好,我想在这里补充一点,即

I think you all guys having great work regarding the problem solution , I would like to add some point here , that is

您应该为DispatcherServlet提供特定的url映射,以便servlet可以取消特定模式的请求,否则会产生问题.

You should have to give specific url mapping for DispatcherServlet , So that servlet can hadnle the request of specific pattern , Otherwise it will create a problem .

也许您会遇到问题

No HTTP request handler found for " some url pattern"

所以我要求您添加特定的网址格式("* .do" .... etc),而不是一般的(如您执行"/")

So I requested you add specific url pattern( "*.do".... etc ) instead of generic( as you did "/")

这篇关于Spring MVC不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆