EL和< c:out& gt;从null属性打印空字符串,但从servlet分派请求时抛出异常 [英] EL and <c:out> prints empty string for null attributes but throws exception when the request is dispatched from servlet

查看:66
本文介绍了EL和< c:out& gt;从null属性打印空字符串,但从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} 评估为空,并停止对的评估. $ {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评估 $ {model.bar} .getBar(),但找不到任何此类getter,这将导致异常.

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和&lt; c:out&amp; gt;从null属性打印空字符串,但从servlet分派请求时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆