java - <jsp :useBean> error: Bean cannot be resolved to a type

标签 java jsp javabeans

我试图了解 Javabeans 是如何工作的,所以我创建了一个简单的 JSP 项目,其中包含 InfoRet.java (Javabean) 和一个JSP 文件以从 bean 中检索值。

这是代码:

public class InfoRet implements java.io.Serializable {

  private static final long serialVersionUID = 1L;
  private String name;
  private int age;

  public InfoRet() {
  }
  public String getname() {
    return this.name;
  }
  public int getage() {
    return this.age;
  }
  public void setname(String name) {
    this.name = name;
  }
  public void setage(int age) {
    this.age = age;
  }
}

这是 JSP 文件:

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

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    <jsp:useBean id="obj" class="InfoRet" scope="session"></jsp:useBean>
  </head>

  <body>
    <jsp:setProperty name="obj" property="name" value="harshal" />
    <jsp:setProperty name="obj" property="age" value="323" />
    <jsp:getProperty name="obj" property="name" />
    <br/>
    <jsp:getProperty name="obj" property="age" />
  </body>

</html>

错误是:

    org.apache.jasper.JasperException: Unable to compile class for JSP: 

    An error occurred at line: 8 in the jsp file: /indexs.jsp
    InfoRet cannot be resolved to a type
    5: <head>
    6: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    7: <title>Insert title here</title>
    8: <jsp:useBean id="obj" class="InfoRet" scope="session" ></jsp:useBean>
    9: </head>
    10: <body>
    11: <jsp:setProperty name="obj" property="name"  value="harshal"/>


    An error occurred at line: 8 in the jsp file: /indexs.jsp
    InfoRet cannot be resolved to a type
    5: <head>
    6: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    7: <title>Insert title here</title>
    8: <jsp:useBean id="obj" class="InfoRet" scope="session" ></jsp:useBean>
    9: </head>
    10: <body>
    11: <jsp:setProperty name="obj" property="name"  value="harshal"/>


    An error occurred at line: 8 in the jsp file: /indexs.jsp
    InfoRet cannot be resolved to a type
    5: <head>
    6: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    7: <title>Insert title here</title>
    8: <jsp:useBean id="obj" class="InfoRet" scope="session" ></jsp:useBean>
    9: </head>
    10: <body>
    11: <jsp:setProperty name="obj" property="name"  value="harshal"/>


    An error occurred at line: 14 in the jsp file: /indexs.jsp
    InfoRet cannot be resolved to a type
    11: <jsp:setProperty name="obj" property="name"  value="harshal"/>
    12: <jsp:setProperty name="obj" property="age"  value="323" />
    13: 
    14: <jsp:getProperty  name="obj"  property="name"/>
    15: <br/>
    16: <jsp:getProperty name="obj" property="age"/> 
    17: </body>


    An error occurred at line: 14 in the jsp file: /indexs.jsp
    InfoRet cannot be resolved to a type
    11: <jsp:setProperty name="obj" property="name"  value="harshal"/>
    12: <jsp:setProperty name="obj" property="age"  value="323" />
    13: 
    14: <jsp:getProperty  name="obj"  property="name"/>
    15: <br/>
    16: <jsp:getProperty name="obj" property="age"/> 
    17: </body>


    An error occurred at line: 16 in the jsp file: /indexs.jsp
    InfoRet cannot be resolved to a type
    13: 
    14: <jsp:getProperty  name="obj"  property="name"/>
    15: <br/>
    16: <jsp:getProperty name="obj" property="age"/> 
    17: </body>
    18: </html>


    An error occurred at line: 16 in the jsp file: /indexs.jsp
    InfoRet cannot be resolved to a type
    13: 
    14: <jsp:getProperty  name="obj"  property="name"/>
    15: <br/>
    16: <jsp:getProperty name="obj" property="age"/> 
    17: </body>
    18: </html>


    Stacktrace:
        org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
        org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
        org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
        org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
        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:728)

所以我想知道上面的代码有什么问题。

最佳答案

我猜你的问题是你的类InfoRet默认包中(也就是说,它不在你创建的命名包中)。默认包中的类对于命名包中的其他类是不可见的(就像您的 JSP 一样)。

所以你必须将你的类放在一个包中:

package com.your.package;

public class InfoRet implements java.io.Serializable {
  //...
}

然后在您的 JSP 中使用它:

<jsp:useBean id="obj" class="com.your.package.InfoRet" scope="session" />

关于java - <jsp :useBean> error: Bean cannot be resolved to a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17004913/

相关文章:

java - ceylon 类型检查器 : How to obtain the typed syntax tree?

java - 如何测试struts 2.0.x应用程序

java - 我怎样才能让 URLClassLoader 表现得像这样?

java - jsp 表单中文本字段的值为 null

jsf-2 - session 范围的托管 bean vs 有状态 bean vs httpsession

java - 不能模拟/ spy 类 java.util.Optional

java - 通过 jSTL 检查复选框

javascript - 在提交该页面之前检查表单字段值

java - 无法从 Vector<Object> 转换为 Integer

java - 如何在 Play 框架的索引页面上加载示例 html 页面?