簡體   English   中英

Spring MVC中的靜態資源

[英]Static resources in spring mvc

我是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>

但是當我運行應用程序時,我只能看到單詞,並顯示圖像點。

這是我的項目結構:

在此處輸入圖片說明

在您的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中,使用<c:url/>引用資源,例如:

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

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

“資源”文件夾不適用於網絡應用。

在“ WEB-INF”下添加一個“靜態”文件夾,並在其下添加“ img”文件夾。 將您的圖像放在那里。

像這樣:/ WEB-INF / static / img

將servlet中的資源映射更改為:

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

更改對圖像的“ index.html”引用:

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

有一種對我有用的快速解決方案:我只輸入“。”。 在“ pic”文件夾名稱之前,它對我的​​HTML表示要在調度程序servlet文件中配置的資源文件夾的根目錄中搜索此圖片文件夾:

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

當然,資源文件夾必須位於/ WEB-INF /文件夾中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM