简体   繁体   English

访问请求范围的bean

[英]Accessing request scoped bean

I declared a request scoped bean in a jsp page and tried to access it another page. 我在jsp页面中声明了一个请求范围的bean,并尝试访问另一个页面。 I was able to access the bean on another page using scriptlet as well as EL , however it got an exception if used jsp;getProperty . 我能够使用scriptletEL访问另一个页面上的bean,但如果使用jsp;getProperty则会出现异常。 Below is the code of both the pages. 下面是两个页面的代码。

PageScope.jsp(First Page) : PageScope.jsp(第一页):

   <jsp:useBean id="taskBean" class="com.mybean.TaskBean" scope="request" />
    <jsp:setProperty name="taskBean" property="multiplier" value="55" />
    <jsp:setProperty name="taskBean" property="multiplicand" value="<%= dd %>" />

<%
        System.out.println("Forwarding request to other page.");
        RequestDispatcher dispatcher = request.getRequestDispatcher("RequestScope.jsp");
        dispatcher.forward(request, response);
%>

RequestScope.jsp(Another Page): RequestScope.jsp(另一页):

     <%
            out.println("Accessing request scoped bean");
            TaskBean taskBean = (TaskBean) request.getAttribute("taskBean");
            out.println(taskBean.getMultiplier());
        %>

        ${taskBean.multiplier}

        <jsp:getProperty name="taskBean" property="multiplier" />
         //THE ABOVE LINE THROWS EXCEPTION.

Is it possible to access bean using jsp:getProperty. 是否可以使用jsp:getProperty访问bean。 Please let me know i am wrong. 请让我知道我错了。 Thanks. 谢谢。

Edit : Below is the stacktrace. 编辑:下面是堆栈跟踪。

org.apache.jasper.JasperException: file:/RequestScope.jsp(28,4) jsp:getProperty for bean with name 'taskBean'. Name was not previously introduced as per JSP.5.3
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1104)
    org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1126)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3489)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jsp.PageScope_jsp._jspService(PageScope_jsp.java:109)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Name was not previously introduced" indicates that you haven't told your JSP about this bean as of yet.

You need to add 你需要添加

<jsp:useBean id="taskBean" class="com.mybean.TaskBean" scope="request" />

before you access the properties using jsp:getProperty 在使用jsp:getProperty访问属性之前

Also, 也,

The "name" attribute of jsp:getProperty should match the "id" attribute of the declared bean. jsp:getProperty的“name”属性应该与声明的bean的“id”属性匹配。

Then access 然后访问

<jsp:getProperty name="taskBean" property="multiplier" />

With Scriptlet When you use 使用Scriptlet时使用

TaskBean taskBean = (TaskBean) request.getAttribute("taskBean");

Check You have already added <%@page import="com.mybean.TaskBean"%> statemement in your JSP. 检查您已在JSP中添加了<%@page import="com.mybean.TaskBean"%> statemement。

So, When JSP page gets compiled like RequestScope_jsp.java . 因此,当JSP页面像RequestScope_jsp.java一样编译时。

First, It has import statement for your bean Second, _jspService method has request and response parameters as argument. 首先,它有你的bean的import语句。其次, _jspService方法有requestresponse参数作为参数。

_jspService(HttpServletRequest request, HttpServletResponse response)

Thus, you can forwarded JSP pages access them using scriptlets . 因此,您可以转发JSP页面使用scriptlets访问它们。 The same things happens with EL . EL

Remember thumb rule

: When you access bean property using jsp:getProperty , you require to add <jsp:useBean> which is used to locate and instantiate a bean class :使用jsp:getProperty访问bean属性时,需要添加<jsp:useBean> ,用于定位和实例化bean类

I believe the tag 我相信标签

<jsp:useBean id="taskBean" type="com.mybean.TaskBean" scope="request" />

Will be needed in the RequestScope.jsp file, since it's already be created and exists in the scope. 将在RequestScope.jsp文件中需要,因为它已经创建并存在于范围中。 Likely the takeBean is better created in the servlet, and the servlet creates the taskBean and set the values, then performs the dispatch.forward instead of from another jsp page. 可能最好在servlet中创建takeBean,servlet创建taskBean并设置值,然后执行dispatch.forward而不是从另一个jsp页面执行。

1 . 1。 You need to declare the bean before using its name in jsp:getProperty. 您需要在jsp:getProperty中使用其名称之前声明bean。

JSP.5.3 JSP.5.3

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:setProperty和jsp:getProperty中name属性的值将引用通过其findAttribute方法从pageContext对象获取的对象。

The object named by the name must have been “introduced” to the JSP processor using either the jsp:useBean action or a custom action with an associated VariableInfo entry for this name. 必须使用jsp:useBean操作或具有此名称的关联VariableInfo条目的自定义操作将名称命名的对象“引入”JSP处理器。 If the object was not introduced in this manner, the container implementation is recommended (but not required) to raise a translation error, since the page implementation is in violation of the specification. 如果没有以这种方式引入对象,则建议(但不要求)容器实现来引发转换错误,因为页面实现违反了规范。

You can read about PageContext here: http://docs.oracle.com/javaee/5/api/javax/servlet/jsp/PageContext.html Another reference to pageContext implicit object is in JSP.2.4 Implicit Objects section. 您可以在此处阅读有关PageContext的信息: httpJSP.2.4 Implicit Objects对pageContext隐式对象的另一个引用位于JSP.2.4 Implicit Objects部分。

Since you forward to another JSP there will be new PageContext. 由于您转发到另一个JSP,因此将有新的PageContext。

It will be safe to use the same instruction, the specification says that the bean with the same name (in the target scope) will not be re-created if it exists: 使用相同的指令是安全的,规范说如果存在,则不会重新创建具有相同名称的bean(在目标范围内):

JSP.5.1 JSP.5.1

The jsp:useBean action is quite flexible; jsp:useBean动作非常灵活; its exact semantics depends on the attributes given. 它的确切语义取决于给定的属性。 The basic semantic tries to find an existing object using id and scope. 基本语义尝试使用id和scope查找现有对象。 If the object is not found it will attempt to create the object using the other attributes. 如果未找到该对象,它将尝试使用其他属性创建对象。

So add the same tag as in PageScope.jsp to the second JSP (RequestScope.jsp): 因此,将与PageScope.jsp中相同的标记添加到第二个JSP(RequestScope.jsp):

 <jsp:useBean id="taskBean" class="com.mybean.TaskBean" scope="request" />

2 . 2。 Regarding why it is accessible with EL, the JSP spec describes this in JSP.2.9 Resolution of Variables and their Properties . 关于为何可以使用EL访问它,JSP规范在JSP.2.9 Resolution of Variables and their Properties描述了这一点。 You better take a look yourself, here is a sample from that section 你最好自己看看,这是该部分的样本

${product} $ {}产品

This expression will look for the attribute named product, searching the page, request, session, and application scopes, and will return its value. 此表达式将查找名为product的属性,搜索页面,请求,会话和应用程序范围,并将返回其值。 If the attribute is not found, null is returned. 如果未找到该属性,则返回null。

3 . 3。 Regarding why it's accessible with scriptlet is obvious 关于为什么用scriptlet访问它是显而易见的

jsp 1: jsp 1:

<jsp:useBean id="taskBean" class="com.mybean.TaskBean" scope="request" />

jsp 2: jsp 2:

TaskBean taskBean = (TaskBean) request.getAttribute("taskBean");

You just take it from the place (request attribute) where to the bean was placed by the first jsp:useBean invocation. 您只需从第一个jsp:useBean调用放置bean的位置(请求属性)获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM