2011-11-20 18 views
0

我是JSP的初学者,并试图做简单的jsp页面。我想设置我的班级字段姓名和打印在页面上。我的等级:JSP Hello Page Error

package org.mypackage.person; 

/** 
* 
* @author cemalinanc 
*/ 
public class Person { 

    private String name; 
    private String surname; 

    Person() 
    { 
     name = null; 
     surname = null; 
    } 

    /** 
    * @return the name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * @return the surname 
    */ 
    public String getSurname() { 
     return surname; 
    } 

    /** 
    * @param surname the surname to set 
    */ 
    public void setSurname(String surname) { 
     this.surname = surname; 
    } 

} 

和我的index.jsp是这样的:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1>Hello World!</h1> 
     <form name="input form" action="response.jsp"> 
      Name: 
      <input type="text" name="name" value="" /> 
      Surname: 
      <input type="text" name="surname" value="" /> 
      <input type="submit" value="Ok" /> 
     </form> 
    </body> 
</html> 

和我的response.jsp页是这样的:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <jsp:useBean id="mybean" scope="session" class="org.mypackage.person.Person" /> 
     <jsp:setProperty name="mybean" property="name" /> 
     <h1>Hello,  <jsp:getProperty name="mybean" property="name" />!</h1> 
    </body> 
</html> 

我只是想设置在这两个领域班级和打印屏幕两个,但我不能。后来我试图打印只是名称字段,但再次不能。我出现如下错误:

服务器遇到内部错误(),导致它无法完成此请求。 org.apache.jasper.JasperException:/response.jsp(line:15,column:8)useBean类属性org.mypackage.person.Person的值无效。

这是什么问题?

如果你能给我一个想法,我将不胜感激。非常感谢您的帮助。

回答

1

删除您的Person()构造函数。

由于未使用“public”声明,因此它的默认范围是“package scope”而不是“public”。每http://java.sun.com/products/jsp/tags/syntaxref.fm14.html,“该类不能是抽象的,必须有一个公共的,无参数的构造函数”。

我建议只删除构造函数,因为它没有做任何事情。默认情况下,你的namesurname实例变量将被初始化为null - 并且只要没有声明其他构造函数,就会自动为你创建一个默认的public,no-arg构造函数。 (我还建议从你的bean类中移除Javadoc注释,Javadocs(或任何其他文档)应该是有意义的,类似于“返回名称”的东西不会告诉我们任何我们还不知道的东西)

相关问题