有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

SpringMVC中的java静态资源

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

这是我的dispatcher 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");
    }
}

这是{}:

<?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>

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

这是我的项目结构:

enter image description here


共 (3) 个答案

  1. # 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'/>">
    
  2. # 2 楼答案

    有一个对我有效的快速解决方案: 我只是放了个“.”在“pic”文件夹名称之前,这对我的HTML表示,要在dispatcher servlet文件中配置的resources文件夹的根目录中搜索此图片文件夹:

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

    当然,资源文件夹必须位于/WEB-INF/文件夹中

  3. # 3 楼答案

    “资源”文件夹对web应用不可用

    在“WEB-INF”下添加一个“静态”文件夹,并在该文件夹下添加“img”文件夹。 把你的照片放在那里

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

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

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

    将“index.html”引用更改为图像:

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