Tôi biết câu hỏi này đã được hỏi nhiều lần, nhưng tôi không thể tìm ra vấn đề là gì. Tôi có thư mục hình ảnh trong thư mục src/main/webapp (đây là một dự án web maven). Tôi có index.jsp trong thư mục src/main/webapp/WEBINF/views.Không hiển thị hình ảnh trong Spring MVC
Tôi đang cố gắng để truy cập hình ảnh và các nguồn lực khác như css và js như thế này:
<img src="/images/left_arrow.png" alt="" />
Nhưng những hình ảnh không nhận được hiển thị.
Dưới đây là file 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>
Đây là thư mục WEB-INF/MVC-phối-servlet.xml tập tin
<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>
Đây là Controller gói com.ravi. Ứng dụng web;
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";
}
}
Tôi nhận được lỗi này sau khi thêm việc kê khai nguồn lực trong Cấu hình mùa xuân "Không có ánh xạ tìm thấy cho yêu cầu HTTP URI [/ WebApp /] trong DispatcherServlet với tên 'MVC-dispatcher' " – Ravi
Nó hoạt động. Sự thay đổi tôi thực hiện là tôi đã giới thiệu các hình ảnh trong jsp bằng cách sử dụng thẻ c: url "alt =" "/>. Cảm ơn sự giúp đỡ. – Ravi