EL and <c:out> prints empty string for null attributes but throws exception when the request is dispatched from servlet

怎甘沉沦 提交于 2019-12-14 03:22:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!