loading

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 @ 2022-11-21 15:56  Himmelbleu  阅读(32)  评论(0编辑  收藏  举报