一尘不染

访问请求范围的bean

jsp

我在一个jsp页面中声明了一个请求范围的bean,并试图访问另一个页面。我能够使用另一个页面上访问bean的scriptlet,以及EL如果使用,但是它有一个例外jsp;getProperty。以下是两个页面的代码。

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(另一页):

     <%
            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.

是否可以使用jsp:getProperty访问bean。请让我知道我错了。谢谢。

编辑:下面是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)

阅读 210

收藏
2020-06-10

共1个答案

一尘不染

Name was not previously introduced" indicates that you haven't told your JSP about this bean as of yet.

您需要添加

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

在使用之前访问属性 jsp:getProperty

也,

的“名称”属性jsp:getProperty应与声明的Bean的“ id”属性匹配。

然后访问

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

使用Scriptlet时

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

检查您是否已经<%@page import="com.mybean.TaskBean"%>在JSP中添加了statemement。

因此,当JSP页面像一样被编译时RequestScope_jsp.java

首先,它具有bean的import语句。其次,_jspService方法具有requestresponse参数作为参数。

_jspService(HttpServletRequest request, HttpServletResponse response)

因此,您可以使用转发的JSP页面访问它们scriptlets。发生相同的事情EL

Remember thumb rule

:当您使用来访问bean属性时jsp:getProperty,您需要添加<jsp:useBean>用于查找和实例化bean类的属性

2020-06-10