文章目录
前言
这一章我们通过JSP来获取图片,完成SpringMvc对静态资源的获取。
一、准备
1. springmvc.png
2. resources.jsp
<%--
Created by IntelliJ IDEA.
User: 张军国001
Date: 2024/4/28
Time: 20:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>rest</title>
</head>
<body>
<img src="${pageContext.request.contextPath}/resources/images/springmvc.png"/>
</body>
</html>
3. ResourcesController (跳转使用)
package org.example.springmvc.params.controller;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Create by zjg on 2024/4/29
*/
@Controller
public class ResourcesController {
@RequestMapping("hello-resources")
public String params(HttpServletRequest request){//重定向
System.out.println(request.getCharacterEncoding());
return "redirect:params/resources.jsp";
}
}
4. 结果
5. 日志
29-Apr-2024 21:01:15.008 警告 [http-nio-8080-exec-5] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET /resources/images/springmvc.png
29-Apr-2024 21:01:15.038 警告 [http-nio-8080-exec-5] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleNoHandlerFoundException No endpoint GET /resources/images/springmvc.png.
二、配置
在此前内容的基础上,修改配置文件
1. spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.example.springmvc"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<!--静态资源配置-->
<mvc:resources mapping="/**" location="/resources"></mvc:resources>
</beans>
2.访问
http://localhost:8080/params/resources.jsp