Java 无法在jsp页面中加载图像

Java 无法在jsp页面中加载图像,java,html,jsp,spring-mvc,Java,Html,Jsp,Spring Mvc,我在SpringMVC项目中创建了一个简单的jsp页面。但是我无法从图像文件夹加载图像。我尝试了真实路径和上下文路径,但仍然无法将图像加载到jsp文件中 实际路径: <% String realPath = application.getRealPath("/"); %> <img src="<%=realPath%>images\logo.png" alt="student" title="student"> <% String contex

我在SpringMVC项目中创建了一个简单的jsp页面。但是我无法从图像文件夹加载图像。我尝试了真实路径和上下文路径,但仍然无法将图像加载到jsp文件中

实际路径:

<%
 String realPath = application.getRealPath("/");
%>

 <img src="<%=realPath%>images\logo.png" alt="student" title="student">
<%
 String contextPath = request.getContextPath();
%>
 <img src="<%=realPath%>images\logo.png" alt="student" title="student">

images\logo.png“alt=“student”title=“student”>
上下文路径:

<%
 String realPath = application.getRealPath("/");
%>

 <img src="<%=realPath%>images\logo.png" alt="student" title="student">
<%
 String contextPath = request.getContextPath();
%>
 <img src="<%=realPath%>images\logo.png" alt="student" title="student">

images\logo.png“alt=“student”title=“student”>
我的项目文件夹结构看起来像

我错过什么了吗?我需要在web.xml中进行任何配置吗


有什么建议吗?

您必须像这样配置静态资源,如图像、js和css文件:

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

在上面的示例中,来自此url模式/images/**的任何请求,Spring都将从/images文件夹中查找资源

  • 您不能使用real path,因为浏览器与webapp不在同一台计算机上
  • 您不能使用
    \
    ,因为URL必须使用
    /
  • 不要在JSP中使用Java代码,尤其是当要使用EL变量时
  • 您缺少一个
    /
    。引用以下文件: 路径以“/”字符开头,但不以“/”字符结尾

  • 这是如何做到的:

    <img src="${pageContext.request.contextPath}/images/logo.png" alt="student" title="student">
    
    
    
    仅当
    /*
    映射到Spring时才需要。谢谢。它对我有用。但我有一个小小的疑问。假设我有10个静态资源文件夹(css、js、图像…),那么我是否需要映射所有10个文件夹?或者,如果我复制资源文件夹中的所有文件夹,并仅为资源文件夹进行配置,如()。那有意义吗?请提供任何建议您可以为每种文件类型添加许多配置,例如: