我是jsp的新手。我正在学习useBean方法,并且在
中有一个例子http://www.studytonight.com/jsp/getproperty-tag.php
向下滚动,你会发现它。
这是我的jsp文件:
<html> <head> <title>Welcome Page</title> </head> <jsp:useBean id="person" type="PersonBean" scope="request"/> <body> Hello, <jsp:getProperty name="person" property="name"/> </body> </html>
这是我的PersonBean.java文件,编译成类文件并放入 classes文件:
import java.io.Serializable; public class PersonBean implements Serializable { private String name="Hello World"; public PersonBean() { this.name="Hello"; } public void setName(String name) { this.name = name; } public String getName() { return name; } }
我收到错误: jsp文件中的第5行:/hello1.jsp发生错误 PersonBean无法解析为类型
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 5 in the jsp file: /hello1.jsp PersonBean cannot be resolved to a type 2: <head> 3: <title>Welcome Page</title> 4: </head> 5: <jsp:useBean id="person" type="PersonBean" scope="request"/> 6: <body> 7: Hello, <jsp:getProperty name="person" property="name"/> 8: </body> An error occurred at line: 5 in the jsp file: /hello1.jsp PersonBean cannot be resolved to a type 2: <head> 3: <title>Welcome Page</title> 4: </head> 5: <jsp:useBean id="person" type="PersonBean" scope="request"/> 6: <body> 7: Hello, <jsp:getProperty name="person" property="name"/> 8: </body> An error occurred at line: 7 in the jsp file: /hello1.jsp PersonBean cannot be resolved to a type 4: </head> 5: <jsp:useBean id="person" type="PersonBean" scope="request"/> 6: <body> 7: Hello, <jsp:getProperty name="person" property="name"/> 8: </body> 9: </html> An error occurred at line: 7 in the jsp file: /hello1.jsp PersonBean cannot be resolved to a type 4: </head> 5: <jsp:useBean id="person" type="PersonBean" scope="request"/> 6: <body> 7: Hello, <jsp:getProperty name="person" property="name"/> 8: </body> 9: </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:490) org.apache.jasper.compiler.Compiler.compile(Compiler.java:379) org.apache.jasper.compiler.Compiler.compile(Compiler.java:354) org.apache.jasper.compiler.Compiler.compile(Compiler.java:341) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.70 logs.
答案 0 :(得分:0)
jsp:useBean action tag:
<jsp:useBean id= "instanceName" scope= "page | request | session | application"
class= "packageName.className" type= "packageName.className"
beanName="packageName.className | <%= expression >" >
</jsp:useBean>
1.在包中放置PersonBean
类,以便能够在其他类中使用它。默认包中的类对于包本身而言是不可见的。
2.将.class
文件放在web-inf/classes
。
3.在PersonBean
声明中指定<jsp:useBean/>
的完全限定类名。
此外 - JVM查找默认包类的约定是查看&#34; current&#34;目录。在服务器环境中,您无法控制&#34;当前&#34;目录,所以你必须使用一个包。
修改您的代码如下:
PersonBean.java
package com.person;
import java.io.Serializable;
public class PersonBean implements Serializable {
private String name="Hello World";
public PersonBean()
{
this.name="Hello";
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
Person.jsp
<html>
<head>
<title>Welcome Page</title>
</head>
<jsp:useBean id="person" class="com.person.PersonBean" scope="request"/>
Hello, <jsp:getProperty name="person" property="name"/>
</html>