Spring MVC doesn't show images

柔情痞子 提交于 2021-02-08 10:35:53

问题


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>

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

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>

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>

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"/>" />

work ok.

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

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";
    }
}

回答1:


I have solved a problem! I needed to add <mvc:annotation-driven /> into my mvc-dipatcher-servlet.xml




回答2:


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

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 .

May be you will face the problem

No HTTP request handler found for " some url pattern"

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



来源:https://stackoverflow.com/questions/18045819/spring-mvc-doesnt-show-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!