JSP tag attribute inherits variable from parent scope when null is passed in?

Viewed 1342

I encountered an issue recently where passing null into a custom JSP tag resulted in the tag going up the scope and resolving the variable to a variable of the same name in the parent JSP.

Is this expected in Java/JSP/JSTL and is there any way to override this functionality without having to rename my variables to not have naming collisions?

For example, a JSP file:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="component" uri="http://www.example.com/test/component"%>

<component:testTag model="${model.testModel}"></component:testTag>

that calls this tag file (testTag.tag):

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ attribute name="model" required="true" rtexprvalue="true" type="com.example.test.model.component.TestModel" %>

Is model empty? <c:out value="${empty model}"/>

What I'm seeing is that despite model.testModel being null in the JSP, in the tag model is not null and it actually resolves to the object that model represents in the JSP!? In case it helps, I'm using Spring MVC 3.

1 Answers

Are you sure ${model.testModel} is null in the JSP? Have you tried this syntax for passing in variables:

<jsp:directive.attribute name="model" type="com.example.test.model.component.TestModel" required="true" rtexprvalue="true" />
Related