2011-11-20 47 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(或任何其他文檔)應該是有意義的,類似於「返回名稱」的東西不會告訴我們任何我們還不知道的東西)

相關問題