为什么我得到org.apache.jasper.JasperException?
问题描述:
当我运行下列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>
我无法找到异常的原因。
答
- 的属性应为小写
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
尝试在'Bean'类中使用'name'而不是'Name'来代替类'Bean'中的成员,并在'Controller'中使用属性名称。 – Jesper 2012-03-12 12:10:06
@ Jesper没有帮助 – 2012-03-12 12:14:18