【问题标题】:Static resources in spring mvcspring mvc中的静态资源
【发布时间】:2015-04-12 11:35:04
【问题描述】:

我是 spring mvc 的新手,我想将一些图像作为静态资源存储在 spring mvc 中,并在需要时调用它们, 我试试这个:

这是我的调度程序 servlet 文件:

   <?xml version='1.0' encoding='UTF-8' ?>

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

    <context:component-scan base-package="sajjad.htlo"/> 
    <mvc:annotation-driven/>

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

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
        <property name="prefix"> 
            <value>/WEB-INF/jsp/</value> 
        </property> 
        <property name="suffix"> 
            <value>.jsp</value> 
        </property> 
    </bean>
</beans>

这是控制器:

@Controller
public class IndexController {

    @RequestMapping(value = "/index.html")
    public ModelAndView indexPage() {
        return new ModelAndView("index");
    }
}

这是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

查看:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome</title>
    </head>

    <body>
        this is index page
        <img src="/resources/pics/t3.jpg" />
    </body>
</html>

但是当我运行应用程序时,我只能看到单词和显示的图像点。

这是我的项目结构:

【问题讨论】:

  • 打开浏览器的网络控制台。检查请求的发送位置以及收到的响应。
  • @SotiriosDelimanolis 它在哪里?
  • 不知道你用的是什么浏览器。看看就好。

标签: java spring spring-mvc


【解决方案1】:

在您的 Web 应用程序上下文配置 (mvc-dispatcher-servlet.xml) 中添加:

<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/"/>

<mvc:default-servlet-handler/>

把你的文件放在webapp/static/[some folder]

用 Maven 打包

在 JSP 中,使用 &lt;c:url/&gt; 引用资源,例如:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<link rel="stylesheet" href="<c:url value='/static/styles/site.css'/>">

【讨论】:

【解决方案2】:

“资源”文件夹对网络应用程序不可用。

在“WEB-INF”下添加一个“static”文件夹,并在其下添加“img”文件夹。 把你的图片放在那里。

像这样:/WEB-INF/static/img

将servlet中的资源映射改为:

<mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>

更改“index.html”对图片的引用:

<img src="/static/img/t3.jpg" />

【讨论】:

    【解决方案3】:

    有一个对我有用的快速解决方案: 我只是把'。在“pic”文件夹名称之前,它告诉我的 HTML 在调度程序 servlet 文件中配置的资源文件夹的根目录中搜索此图片文件夹:

    <img src="./pics/t3.jpg" />
    

    当然,资源文件夹必须在 /WEB-INF/ 文件夹中。

    【讨论】: