JSP 页面引入静态资源 404 未找到

错误截图

image

jsp 页面引入了 css 文件,部署项目时发现 css 不生效,打开 f12 查看网络,发现请求状态码是 404。导致这个问题的情况大概有以下两种情况:el 表达式无效;静态资源未配置。

el 表达式无效

如果你通过浏览器 f12 查看 link 或 script 标签的引入情况,src、href 中直接显示${pageContext.request.contextPath},没有转换成项目路径。

可能你没有在 jsp 页面中写 isELIgnored="false"

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>

web.xml 配置

如果上述步骤都没有问题,有可能你没有映射静态资源路径,需要在 web.xml 中添加如下代码:

file:[web.xml]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

  add:[<servlet-mapping>]
    add:[<servlet-name>default</servlet-name>]
    add:[<url-pattern>/css/*</url-pattern>]
  add:[</servlet-mapping>]
</web-app>

spring-config.xml 配置

如果你的项目是 Spring MVC,在 spring 配置文件中这样写:

<?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: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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  add:[<mvc:resources mapping="/css/*" location="/css/"/>]

</beans>
posted @   Himmelbleu  阅读(50)  评论(0编辑  收藏  举报
首页
随笔
博客园
我的
标签
管理
JSP 页面引入静态资源 404 未找到
发表于 2022-11-21 15:56
|
已有 50 人阅读
|
留下 0 条评论
|
全文字数 ≈ 154字

错误截图

image

jsp 页面引入了 css 文件,部署项目时发现 css 不生效,打开 f12 查看网络,发现请求状态码是 404。导致这个问题的情况大概有以下两种情况:el 表达式无效;静态资源未配置。

el 表达式无效

如果你通过浏览器 f12 查看 link 或 script 标签的引入情况,src、href 中直接显示${pageContext.request.contextPath},没有转换成项目路径。

可能你没有在 jsp 页面中写 isELIgnored="false"

JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>

web.xml 配置

如果上述步骤都没有问题,有可能你没有映射静态资源路径,需要在 web.xml 中添加如下代码:

web.xml
XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

  <servlet-mapping>]
    <servlet-name>default</servlet-name>]
    <url-pattern>/css/*</url-pattern>]
  </servlet-mapping>]
</web-app>

spring-config.xml 配置

如果你的项目是 Spring MVC,在 spring 配置文件中这样写:

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: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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  <mvc:resources mapping="/css/*" location="/css/"/>]

</beans>
作者:Himmelbleu
出处: https://www.cnblogs.com/Himmelbleu/#/p/16911659
版权:本作品采用「 署名-非商业性使用-相同方式共享 4.0 国际 」许可协议进行许可。
文章目录
错误截图
el 表达式无效
web.xml 配置
spring-config.xml 配置
点击右上角即可分享
微信分享提示