日期:2014-05-17  浏览次数:20703 次

在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.scoreY