【第7章】spring-mvc静态资源(resources)


前言

这一章我们通过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
在这里插入图片描述


总结

回到顶部

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值