我使用Netbeans创建了一个Spring MVC 3.0应用程序.我有一个简单的控制器和JSP视图.除了不呈现的图像之外,JSP视图正确显示.我的目录结构如下所示:

在我的Home.jsp页面中,未呈现的图像被引用如下:
<img src="Images/face.png" />
Run Code Online (Sandbox Code Playgroud)
我已经验证face.png位于Images目录中.那么为什么它不会出现在浏览器中呢?在Spring MVC中,我应该在哪里放置JSP视图引用的文件,如图像,CSS,JS等?
这是关于在spring mvc app的jsp页面中访问资源的问题的后续问题 感谢@ kmb385我能够解决这个问题,但现在我在我的JSP文件中得到以下eclipse错误javax.servlet.jsp.JspException不能解决了一个类型和
javax.servlet.jsp.PageContext无法解析为类型
按照kmb385的建议,这是我的控制器:
@Controller
public class AdminController {
@RequestMapping("/")
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("index");
model.addObject("msg", "hello world");
return model;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的index.jsp页面以防万一:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- <link type="text/css" rel="stylesheet" href="css/style.css"/> -->
<style type="text/css">
<%@include file="css/style.css" %>
</style>
<title>My Project - MainPage</title>
</head>
<body>
<h2 class="main_heading2">My Project - MainPage</h2>
<div …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已被多次询问,但我无法弄清问题是什么.我有src/main/webapp文件夹下的images文件夹(这是一个maven web项目).我在src/main/webapp/WEBINF/views文件夹中有index.jsp.
我正在尝试访问图像和其他资源,如css和js,如下所示:
<img src="/images/left_arrow.png" alt="" />
但图像没有显示出来.
这是web.xml文件
<web-app 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"
version="3.0">
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
这是WEB-INF/mvc-dispatcher-servlet.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.ravi.WebApp" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
这是Controller包com.ravi.WebApp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/")
public String printWelcome(Model model) {
return "index";
}
}
Run Code Online (Sandbox Code Playgroud)