春天的静态资源mvc

时间:2015-02-11 22:10:00

标签: java spring 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>

但是当我运行app时,我只能看到这个词,并显示图像点。

这是我的项目结构:

enter image description here

3 个答案:

答案 0 :(得分: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中,使用<c:url/>引用资源,如:

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

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

答案 1 :(得分:0)

&#39;资源&#39;文件夹不可用于Web应用程序。

添加&#34;静态&#34; &#34; WEB-INF&#34;下的文件夹和&#34; img&#34;那个文件夹。 将图像放在那里。

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

将servlet中的资源映射更改为:

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

更改&#34; index.html&#34;参考图像:

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

答案 2 :(得分:0)

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

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

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

相关问题