struts2框架——从后台取得数据集,并在前台页面循环显示

时间:2022-02-19 19:31:57

1.CourseBean.java

 1 public class CourseBean {
 2     
 3     private String f_courseId          = "";
 4     private String f_term              = "";
 5     private String f_week              = "";
 6     private String f_credit          = "";
 7     private String f_coursename     = "";
 8     private String f_courseStart    = "";
 9     private String f_courseEnd      = "";
10     private String f_teacherId      = "";
11     private String f_roomId          = "";
12     private String f_subjectId      = "";
13     private String f_gradeId          = "";
14     private String f_periodId          = "";
15 
16         //变量的get和set方法
17         //。。。       
18 } 

2.CourseListBean.java

 1 public class CourseListBean {
 2     
 3     private List<CourseBean> courseList = new ArrayList<CourseBean>();
 4     
 5     public List<CourseBean> getCourseList() {
 6         return courseList;
 7     }
 8 
 9     public void setCourseList(List<CourseBean> courseList) {
10         this.courseList = courseList;
11     }
12 
13 }

3.ClassShowAction.java

 1     public String classShow() throws SQLException {
 2         // TODO Auto-generated method stub
 3         HttpServletRequest request = ServletActionContext.getRequest();
 4         HttpSession session = request.getSession();
 5 
 6         // userBean
 7         String username = (String) session.getAttribute("username");
 8         UserBean userBean = new UserBean();
 9         userBean.setUsername(username);
10         // pageInfoBean
11         PageInfoBean pageInfoBean = new PageInfoBean();
12         pageInfoBean.setIntPage(this.getIntPage());
13         
14         // 处理和设置 courseListBean,pageInfoBean
15         CourseListBean courseListBean = new CourseListBean();
16         courseListBean = courseService.classShow(userBean, pageInfoBean);
17         request.setAttribute("courseListBean", courseListBean);
18         request.setAttribute("pageInfoBean", pageInfoBean);
19 
20         return "Success";
21 
22     }

 

 4.数据注入:courseListBean 和 courseBean获取

 1     public CourseListBean classShow(UserBean userBean, PageInfoBean pageInfoBean)
 2             throws SQLException {
 3 
 4         // 数据库连接
 5         sql = "select * from t_course where f_teacherId = (select f_userId from t_user where f_username = '"
 6                 + userBean.getUsername() + "')";
 7         rs = conn.executeQuery(sql);49 
50         // 获取courseListBean
51         CourseListBean courseListBean = new CourseListBean();
52         for (int i = 0; i < num; i++) {
         //courseBean的获取,即courseBean的数据注入
53 CourseBean courseBean = new CourseBean(); 54 courseBean.setF_courseId(rs.getString("f_courseId")); 55 courseBean.setF_term(rs.getString("f_term")); 56 courseBean.setF_week(rs.getString("f_week")); 57 courseBean.setF_credit(rs.getString("f_credit")); 58 courseBean.setF_coursename(rs.getString("f_coursename")); 59 courseBean.setF_courseStart(rs.getString("f_courseStart")); 60 courseBean.setF_courseEnd(rs.getString("f_courseEnd")); 61 courseBean.setF_teacherId(rs.getString("f_teacherId")); 62 courseBean.setF_roomId(rs.getString("f_roomId")); 63 courseBean.setF_subjectId(rs.getString("f_subjectId")); 64 courseBean.setF_gradeId(rs.getString("f_gradeId")); 65 courseBean.setF_periodId(rs.getString("f_periodId"));
//courseListBean的获取,即courseLiseBean的数据注入
66 courseListBean.getCourseList().add(courseBean); 67 rs.next(); // 下一条记录 68 } 69 70 conn.close(); 71 return courseListBean; 72 }

 

 5.前台展示:

 1 <!-- 班级列表 -->
 2         <table width="675" border="0" align="center" cellpadding="4"
 3             cellspacing="1" bgcolor="#3D7BA3">
 4             <tr bgcolor="94C9E7" align="center">
 5                 <td>班级ID</td>
 6                 <td>班级名</td>
 7                 <td>学期</td>
 8                 <td>学分</td>
 9                 <td>课程开始周</td>
10                 <td>课程结束周</td>
11                 <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
12             </tr>
13 
14             <s:iterator value="#request.courseListBean.courseList">
15                 <tr bgcolor="ffffff" align="center">
16                     <td><s:property value="f_courseId" /></td>
17                     <td><s:property value="f_coursename" /></td>
18                     <td><s:property value="f_term" /></td>
19                     <td><s:property value="f_credit" /></td>
20                     <td><s:property value="f_courseStart" /></td>
21                     <td><s:property value="f_courseEnd" /></td>
22                     <td><a href="">查看</a> <a href="">删除</a></td>
23                 </tr>
24             </s:iterator>
25         </table>