Why does <jsp:getProperty> need <jsp:useBean>, but EL doesn't?

若如初见. 提交于 2021-01-29 07:10:03

问题


I have servlet with following code:

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);
}

I have two choices for printing person's name inside result.jsp. Using <jsp:getProperty> or Expression language.

Simple EL code:

<!DOCTYPE html>

<html><body>

Welcome ${person.name}

</body></html>

Or using jsp:getProperty like this:

<!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>

From my understanding, both these codes for getting name either by ${person.name} or <jsp:getProperty name="person" property="name"/> call findAttribute(). But there is one major difference. Code written in EL doesn't need <jsp:useBean>, while <jsp:getProperty> works only combined with <jsp:useBean>.

We might ask how does ${person.name} know what type of object is "person". Well, it uses something like this. My question is how can't <jsp:getProperty> work the same way as EL as stated in given link? Why can't it call getClass(),getMethod() and "spare" us struggle of typing class,type,id inside <jsp:useBean>? Is it simply because EL is newer, thus providing us with less code needed to type, or there is something else hiding behind this that I am not seeing?


回答1:


As already mentioned in the answers from this other question of yours, <jsp:getProperty> uses <jsp:useBean> because the JSP specification says so. Why it was decided to work like that is a question best asked to those that drafted the spec. Finding out exactly the reason might satisfy your curiosity :) but it won't make any change in the way the tags work. So basically, that's what we have, that's how it works, that's what we use.

Also, as I mentioned in my answer on that question, you can "decouple" the two tags, at least in Tomcat (I don't know if possible in other servlet containers), by using the following system property:

-Dorg.apache.jasper.compiler.Generator.STRICT_GET_PROPERTY=false

And now <jsp:getProperty> behaves like the EL expression.


With that being said, there is this quote I like (attributed to a bunch of people) that says:

Things are the way they are because they got that way

JSP has been around for a while now. In the beginning, there was no Expression Language. If you wanted to use server tags, you had to use the ones from the <jsp: prefix which are defined in the specification and servers need to implement them. It's the bare minimum of "batteries included" to write JSPs nice and clean without throwing a bunch of scriptlets in there and making a mess. What was not batteries included you could build on your own with classes and interfaces from the tag handler package.

With time people started writing their own tags and tag libraries made their appearance. One of the most popular was JSTL, which you will usually find under the <c: prefix. These are more powerful and flexible tags than what <jsp: offered so people started using these in basically every web application out there. I'm not sure if JSTL introduced the ${} notation or not, but that language became popular because of JSTL.

So time passed and people figured out that all those ${} evaluations in JSTL and all that language that almost everybody knew - or bumped into sooner or later - could be evaluated directly by the servlet container and thus obtain a much cleaner content. The JSP specifications were enhanced and now you have EL directly implemented, without needing to use <jsp:getProperty>.



来源:https://stackoverflow.com/questions/65621836/why-does-jspgetproperty-need-jspusebean-but-el-doesnt

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