EL和< c:out>从null属性打印空字符串,但从servlet分派请求时抛出异常 [英] EL and <c:out> prints empty string for null attributes but throws exception when the request is dispatched from servlet
问题描述
我的调度程序servlet是:
My dispatcher servlet is :
SampleModel model = new SampleModel();
model.setModelName("someName");
request.setAttribute("model", model);
request.getRequestDispatcher("nulltester.jsp").forward(request, response);
此处SampleModel
类只有一个名为modelName
的属性.转发请求的JSP片段:
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}">
如果我们点击servlet,然后servlet将请求分派到jsp,则此代码将引发exception.但是,如果我们直接命中JSP,那么我们将获得o/p毫无例外!谁能解释我为什么会这样?
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 ?
推荐答案
如果根本没有模型属性,则EL将把${model}
评估为null,并在那里停止对${model.bar}
的评估,并返回一个空值.字符串.
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.
如果有模型,它将对您的对象评估${model}
并尝试通过调用model.getBar()
评估${model.bar}
,但找不到任何此类吸气剂,这将导致异常.
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.
这篇关于EL和< c:out>从null属性打印空字符串,但从servlet分派请求时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!