3

我试图看看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)

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

4

2 回答 2

3

我猜你的问题是你的类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" />
于 2013-06-08T23:37:15.510 回答
1

是的,您必须创建一个单独的 java 包,然后在该包中创建一个新的 java 类。之后,只需进行更改<jsp:useBean class="yournewpackage.yourclassname">

于 2015-03-13T17:17:02.007 回答