【问题标题】:EL and <c:out> prints empty string for null attributes but throws exception when the request is dispatched from servletEL 和 <c:out> 为空属性打印空字符串,但在从 servlet 分派请求时抛出异常
【发布时间】:2013-03-16 07:15:13
【问题描述】:

我的调度器 servlet 是:

 SampleModel model = new SampleModel();
 model.setModelName("someName");
 request.setAttribute("model", model);
 request.getRequestDispatcher("nulltester.jsp").forward(request, response);

这里 SampleModel 类只有一个名为 modelName 的属性。转发请求的 JSP 片段:

The undefined bar property is : ${model.bar} , <c:out value="${model.bar}">

如果我们点击 servlet 并且 servlet 然后将请求分派给 jsp,则此代码将引发异常。但是,如果我们直接点击 JSP,那么我们就会毫无例外地得到 o/p !谁能解释一下为什么会这样?

【问题讨论】:

    标签: jsp jstl el


    【解决方案1】:

    如果根本没有模型属性,EL 将评估 ${model} 为 null,并将在那里停止评估 ${model.bar},返回一个空字符串。

    如果有模型,它将评估${model} 到您的对象并尝试通过调用model.getBar() 评估${model.bar},但找不到任何此类getter,这将导致异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2019-11-27
      相关资源
      最近更新 更多