在JSP页面调用JavaBean,出错!初学!求指导!
一共有三个文件:
①JavaBeanForJSP.jsp
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head>
<title>实验一 潘深练 测试 JavaBean For StuInfo</title>
</head>
<body>
<jsp:useBean id="javabean1" class="JavaBeanForStuInfo" scope="application"></jsp:useBean>
<jsp:setProperty name="javabean1" property="*"/>
<%
if (javabean1.getStuNumber() == null)
{
response.sendRedirect("JavaBeanStudent.jsp");
}
%>
<table>
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>语文成绩</td>
<td>数学成绩</td>
<td>英语成绩</td>
<td>最高成绩</td>
<td>最低成绩</td>
<td>平均成绩</td>
</tr>
<tr>
<td><jsp:getProperty name="javabean1" property="stuNumber"/></td>
<td><jsp:getProperty name="javabean1" property="stuName"/></td>
<td><jsp:getProperty name="javabean1" property="stuAge"/></td>
<td><jsp:getProperty name="javabean1" property="scoreYW"/></td>
<td><jsp:getProperty name="javabean1" property="scoreSX"/></td>
<td><jsp:getProperty name="javabean1" property="scoreYY"/></td>
<td><%= javabean1.getMaxScore() %></td>
<td><%= javabean1.getMinScore() %></td>
<td><%= javabean1.getAvgScore() %></td>
</tr>
</table>
</body>
</html>
②JavaBeanForStuInfo.java
public class JavaBeanForStuInfo {
/*
*包含了学生的学号、姓名、年龄、语文、数学、英语等信息,并编写计算三门课的最大值、最小值和平均分
**/
private int stuNumber = 0;
private String stuName = "no name";
private int stuAge = 0;
private int scoreYW = 0;
private int scoreSX = 0;
private int scoreYY = 0;
public JavaBeanForStuInfo (){}
// 学号
public void setStuNumber(int stuNumber){
if (stuNumber != 0) {
this.stuNumber = stuNumber;
}
}
public int getStuNumber(){
return this.stuNumber;
}
//姓名
public void setStuName(String stuName){
if (stuName.isEmpty()) {
this.stuName = stuName;
}
}
public String getStuName(){
return this.stuName;
}
//年龄
public void setStuAge(int stuAge){
if (stuAge != 0) {
this.stuAge = stuAge;
}
}
public int getStuAge(){
return this.stuAge;
}
//语文
public void setScoreYW(int scoreYW){
if (scoreYW != 0) {
this.scoreYW = scoreYW;
}
}
public int getScoreYW(){
return this.scoreYW;
}
// 数学
public void setScoreSX(int scoreSX){
if (scoreSX != 0) {
this.scoreSX = scoreSX;
}
}
public int getScoreSX(){
return this.scoreSX;
}
// 英语
public void setScoreYY(int scoreYY){
if (scoreYY != 0) {
this.scoreYY = scoreYY;
}
}
public int getScoreYY(){
return this.scoreYY;
}
//最大值
public String getMaxScore(){
return this.scoreYW > this.scoreSX ?
(this.scoreYW >this.scoreYY ? "语文的分数最高,分数为"+this.scoreYW : "英语的分数最高,分数为"+this.scoreYY) :
(this.scoreSX > this.scoreYY? "数学的分数最高,分数为"+this.scoreSX : "英语的分数最高,分数为"+this.scoreYY) ;
}
//最小值
public String getMinScore(){
return this.scoreYW < this.scoreSX ?
(this.scoreYW <this.scoreYY ? "语文的分数最低,分数为"+this.scoreYW : "英语的分数最低,分数为"+this.scoreYY) :
(this.scoreSX < this.scoreYY? "数学的分数最低,分数为"+this.scoreSX : "英语的分数最低,分数为"+this.scoreYY) ;
}
//平均分
public String getAvgScore(){
return "语文、数学、英语三门分数的平均分为 :"+ (this.scoreSX + this.scoreYW + this.scoreYY)/3;
}
}
③JavaBeanStudent.jsp
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head>
<title>实验一 潘深练 测试 JavaBean For StuInfo</title>
</head>
<body>
<form name="javabean" action="javaBeanForJsp.jsp" method="post">
<p> 学号 :<input type="text" name="stuNumber"></p>
<p> 姓名 :<input type="text" name="stuName"></p>
<p> 年龄 :<input type="text" name="stuAge"></p>
<p> 语文成绩 :<input type="text" name="scoreYW"></p>
<p> 数学成绩 :<input type="text" name="scoreSX"></p>
<p> 英语成绩 :<input type="text" name="scoreYY"></p>
<p>
<input type="submit" name="sumbit" value="提交">
<input type="reset" name="reset" value="取消">
</p>
</form>
</body>
</html>
然后打开tomcat,访问:http://127.0.0.1:8080/6/JavaBeanForJSP.jsp (6为项目名称)
结果出错
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 11 in the jsp file: /JavaBeanForJSP.jsp
JavaBeanForStuInfo cannot be resolved to a type
8:
9: <body>
10:
11: <jsp:useBean id="javabean1" class="JavaBeanForStuInfo" scope="application"></jsp:useBean>
12: <jsp:setProperty name="javabean1" property="*"/>
13: <%
14: if (javabean1.getStuNumber() == null)
[解决办法]
<jsp:useBean id="javabean1" class="具体路径" scope="application"> </jsp:useBean>
class="具体路径"。
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html