为什么< 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
.
I have two choices for printing person's name inside result.jsp. Using <jsp:getProperty>
or 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>
结合使用.
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>
.
我们可能会问${person.name}
如何知道什么类型的对象是人".嗯,它使用的是这个.我的问题是<jsp:getProperty>
如何不能像给定链接中所述的那样以与EL相同的方式工作?为什么不能调用 getClass(), getMethod()和"spare"?我们很难输入 class , type , id
在<jsp:useBean>
里面?仅仅是因为EL是较新的,从而为我们提供了更少的键入代码,还是其他隐藏在我后面的东西?
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?
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
现在<jsp:getProperty>
的行为类似于EL表达式.
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已经存在了一段时间.最初,没有表达语言.如果要使用服务器标记,则必须使用规范中定义的<jsp:
前缀标记,服务器需要实现它们.这是包含的电池"的最低要求.编写干净整洁的JSP而不在其中扔一堆scriptlet,并且.您可以使用
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.
随着时间的流逝,人们开始编写自己的标签,并且标签库崭露头角.最受欢迎的之一是 JSTL ,通常会在<c:
前缀下找到它.这些标签比<jsp:
提供的标签更强大,更灵活,因此人们开始在几乎每个Web应用程序中使用它们.我不确定JSTL是否引入了${}
表示法,但是由于JSTL,该语言开始流行.
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.
所以时间流逝,人们发现JSTL中的所有${}
评估以及几乎每个人都知道的语言-或早晚碰到的所有语言-都可以由servlet容器直接评估,从而获得更加简洁的内容. JSP规范得到了增强,现在您可以直接实现EL,而无需使用<jsp:getProperty>
.
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>
.