为什么 <jsp:getProperty> 需要 <jsp:useBean> 而 EL 不需要?

问题描述

我有带有以下代码的 servlet

protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException {
    Person p = new Person("Mike");
    req.setAttribute("person",p);

    RequestDispatcher view = req.getRequestDispatcher("/result.jsp");
    view.forward(req,resp);
}

我有两种选择在 result.jsp 中打印人名。使用 <jsp:getProperty>Expression language

简单的EL代码:

<!DOCTYPE html>

<html><body>

Welcome ${person.name}

</body></html>

或者像这样使用 jsp:getProperty

<!DOCTYPE html>

<html><body>

<jsp:useBean id="person" type="com.example.Person" class="com.example.Person" scope="request"/>
Welcome <jsp:getProperty name="person" property="name"/>

</body></html>

据我所知,这两个通过 ${person.name}<jsp:getProperty name="person" property="name"/> 获取名称的代码都调用 findAttribute()。但是有一个主要区别。用 EL 编写的代码不需要 <jsp:useBean>,而 <jsp:getProperty> 仅与 <jsp:useBean> 结合使用。

我们可能会问${person.name}如何知道“人”是什么类型的对象。嗯,它使用类似 this 的东西。我的问题是 <jsp:getProperty> 如何不能像给定链接中所述的 EL 那样工作?为什么它不能调用 getClass(),getMethod() 并“省去”我们输入 class,type的麻烦强>,id<jsp:useBean>内?仅仅是因为 EL 较新,从而为我们提供了更少的输入代码,还是隐藏在这背后的其他东西我没有看到?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)