Spring 5.3.7使用JSTL标签触发NullPointerException空指针异常求助
解决方案
- 修复JSTL依赖冲突
你使用的IBM WebSphere容器本身自带JSTL实现,应用内打包的JSTL包会和容器内置包产生类加载冲突,是该问题的高频诱因。修改pom.xml中JSTL依赖的scope为provided:
<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency>
- 校验web.xml版本
如果web.xml声明的版本低于2.4,默认会禁用EL表达式,导致${i}解析失败抛出空指针。将web.xml头更新为3.1及以上版本(适配Spring 5.3的最低Servlet版本要求):
<?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_3_1.xsd" version="3.1"> <!-- 原有web配置保持不变 --> </web-app>
同时可在报错的JSP页面顶部添加如下配置,强制开启EL表达式解析:
<%@ page isELIgnored="false" %>
- 开启Spring JSTL视图支持
Spring MVC的视图解析器需要显式配置JSTL支持,才能正确渲染JSTL标签。如果使用注解配置,修改视图解析器Bean:
@Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); // 核心配置:指定使用JstlView处理JSP页面 resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class); return resolver; }
如果使用XML配置视图解析器,添加对应的viewClass属性即可。
- 调整WebSphere类加载顺序(仅容器部署场景适用)
如果上述配置修改后仍报错,进入WebSphere管理控制台,找到当前应用的类加载配置,将类加载顺序调整为应用类优先加载,避免容器自带的JSTL实现覆盖应用配置。
内容的提问来源于stack exchange,提问作者Oscar
相关产品推荐
相关产品推荐

