我在 jsp 页面中声明了一个请求范围的 bean,并尝试在另一个页面中访问它。我能够使用scriptlet
as well访问另一个页面上的 bean 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。请让我知道我错了。谢谢。
编辑:下面是堆栈跟踪。
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)