2012-03-12 82 views
-1

當我運行下列servlet:爲什麼我得到org.apache.jasper.JasperException?

import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.IOException; 

public class Controller extends HttpServlet { 
@Override 
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException { 
    Bean bean = new Bean(); 
    bean.setName("Suhail Gupta"); 
    request.setAttribute("Name", bean); 
    RequestDispatcher rd = request.getRequestDispatcher("index.jsp"); 
    rd.forward(request, response); 
    } 
} 

異常:

HTTP Status 500 - 

type Exception report 

message 

descriptionThe server encountered an internal error() that prevented it from fulfilling this request. 

exception 

org.apache.jasper.JasperException: PWC6054: Cannot find any information on property 'Name' in a bean of type 'Bean' 

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.1 logs. 

被生成。我不明白這個原因。

以下是類:

public class Bean { 

private String Name = null; 

public void setName(String n) { 
    Name = n; 
} 

public String getName() { 
    return Name;   
    } 
} 

,這是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> 
    <jsp:useBean id="name" class="Bean" scope="request" /> 
    Person created by the Servlet : <jsp:getProperty name="name" property="Name" /> 
</body> 
</html> 

我無法找到異常的原因。

+1

嘗試在'Bean'類中使用'name'而不是'Name'來代替類'Bean'中的成員,並在'Controller'中使用屬性名稱。 – Jesper 2012-03-12 12:10:06

+0

@ Jesper沒有幫助 – 2012-03-12 12:14:18

回答

3
  • 的屬性應爲小寫private String name - 這是由Java約定和JavaBean標準。
  • 標籤應小寫property="name"使用規定 - 的JavaBeans,再次
  • bean的名字不應該是Name,這是令人困惑的。使它nameBean(小寫,最好是)
  • 你的班級應該有一個包。默認包會導致問題。
  • 而不是jsp:標籤您可以簡單地使用EL:${nameBean.name}將解析爲適當的值。
+0

是的!問題解決了。 _(問題集還包括默認包!)_但是我得到'null',我期望通過bean.setName(「Suhail Gupta」)設置名稱''。這可能是什麼原因? – 2012-03-12 12:31:19