问题
My dispatcher servlet is :
SampleModel model = new SampleModel();
model.setModelName("someName");
request.setAttribute("model", model);
request.getRequestDispatcher("nulltester.jsp").forward(request, response);
Here SampleModel
class has only a single property named modelName
. Snippet of JSP where the request is forwarded :
The undefined bar property is : ${model.bar} , <c:out value="${model.bar}">
If we hit the servlet and servlet then dispatches the request to the jsp , this code throws exception . But if we directly hit the JSP then we get the o/p without any exception ! Can anyone explain me why this happens ?
回答1:
If there is no model attribute at all, the EL will evaluate ${model}
to null, and will stop the evaluation of ${model.bar}
there, returning an empty string.
If there is a model, it will evaluate ${model}
to your object and try to evaluate ${model.bar}
by calling model.getBar()
, but won't find any such getter, which will lead to an exception.
来源:https://stackoverflow.com/questions/15446632/el-and-cout-prints-empty-string-for-null-attributes-but-throws-exception-when