jsp:useBean作用域属性如何工作? [英] how does jsp:useBean scope attribute work?

查看:121
本文介绍了jsp:useBean作用域属性如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解jsp:useBean JSP操作标记中的scope属性是如何工作的.在我的理解中,scope用于指示Bean的位置(请求,会话,应用程序等),但是在进行一些测试之后,我遇到了一个有趣的情况,并非如此,请考虑以下JSP代码(我是为了简单起见,在这里使用scriplets):

I am trying to understand how exactly scope attribute in jsp:useBean JSP action tag works. In my understanding scope is used to indicate where the bean is located (request,session,application etc.), but after some testing I came across an interesting situation where it's not the case, please consider the following JSP code (I am using scriplets here just for the sake of simplicity):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"  import="package2JSP.User" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Title</title>
</head>

<body>
    <%
        User user1 = new User("id1","name1");
        User user2 = new User("id2","name2");
        request.setAttribute("user", user1);
        session.setAttribute("user", user2);
    %>
    <%-- Here I expect to create user bean that represents user2 from session scope--%>
    <jsp:useBean id="user" class="package2JSP.User" scope="session"/>

    <%-- Here I expect user name to be name2 but it is name1 instead--%>
    <jsp:getProperty property="name" name="user"/>
</body>
</html>

因此,基本上我在这里创建了2个用户对象,并将它们设置为请求和会话范围中的用户"属性,当我尝试使用jsp:useBean从会话"范围中检索用户"时,似乎检索到请求"范围.

So basically here I created 2 users objects and set them as "user" attributes in request and session scopes, when I tried to retrieve "user" from "session" scope using jsp:useBean it seems as if "user" from "request" scope was retrieved.

您能解释一下为什么会发生吗?那么,使jsp:useBean以这种方式工作而不是通常从指定范围内选择属性的开发原因是什么?

Can you please explain me why it happened? And what was the development reason to make jsp:useBean work this way instead of normally selecting the attribute from the specified scope, are there any advantages of it?

现在我知道我可以使用JSTL/EL来检索所需的值,即<c:out value="${sessionScope.user.name}" />,但是我只想知道jsp:useBean的工作原理.

Now I know I could use JSTL/EL to retrieve the needed value i.e. <c:out value="${sessionScope.user.name}" /> but I just want to know how jsp:useBean works.

推荐答案

在这种情况下,涉及2个标签:

In this situation are 2 tags involved:

  • jsp:useBean
  • jsp:getProperty

jsp:useBean

根据规范-JSP.5-JSP.5.1 <jsp:useBean> :

一个jsp:useBean动作关联Java编程的实例 在给定范围内定义的语言对象,并且可与 给定的ID和一个新声明的具有相同ID的脚本变量. 在无脚本页面或 无脚本上下文(如在上述动作的主体中所示), 没有创建Java脚本变量,而是EL变量是 已创建.

A jsp:useBean action associates an instance of a Java programming language object defined within a given scope and available with a given id with a newly declared scripting variable of the same id . When a action is used in an scriptless page, or in an scriptless context (as in the body of an action so indicated), there are no Java scripting variables created but instead an EL variable is created.

已编译的Java代码为:

The compiled java code is:

      package2JSP.User user = null;
      synchronized (session) {
        user = (package2JSP.User) _jspx_page_context.getAttribute("user", PageContext.SESSION_SCOPE);
        if (user == null){
          try {
            user = (package2JSP.User) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "package2JSP.User");
          } catch (ClassNotFoundException exc) {
            throw new InstantiationException(exc.getMessage());
          } catch (Exception exc) {
            throw new ServletException("Cannot create bean of class " + "package2JSP.User", exc);
          }
          _jspx_page_context.setAttribute("user", user, PageContext.SESSION_SCOPE);
        }
      }

如果要访问此用户对象,则可以使用scriptlet(表达式)<%=user.getName()%>

If you want to access this user object you can use a scriptlet(expression) <%=user.getName()%>

jsp:getProperty

根据规范-JSP.5章-JSP.5.3 <jsp:getProperty> 说:

中的name属性的值 jsp:setProperty和jsp:getProperty将引用一个对象, 是通过pageContext对象的findAttribute获得的 方法.

The value of the name attribute in jsp:setProperty and jsp:getProperty will refer to an object that is obtained from the pageContext object through its findAttribute method.

JSP编译器从jsp:getProperty标记对

The JSP Compiler make from the jsp:getProperty tag a call to the findAttribute():

out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString(
  (((package2JSP.User)_jspx_page_context.findAttribute("user")).getName())));

findAttribute()
在页面,请求,会话(如果有效)中搜索命名属性, 并按顺序返回应用范围,并返回关联的值或 空.

findAttribute()
Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

基本上:返回第一个匹配项.

Basically: the first match is returned.

使用useBeangetProperty被认为是不好的做法.
使用JSTL/EL是使用属性的更好方法.

Using useBean and getProperty is considered bad practice.
Using JSTL/EL is the better way to work with attributes.

这篇关于jsp:useBean作用域属性如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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