读书人

JSP跟Action类dao类间的关系details

发布时间: 2012-10-27 10:42:26 作者: rapoo

JSP和Action类,dao类间的关系details.

一、在.jsp中用form传入参数:

<html:form action="/stuUser?method=findCourse">
?? ??? ?<table border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#afdf69" width="100%">
?? ??? ??? ?<tr>
?? ??? ??? ??? ?<td height="28" colspan="4" bordercolor="#FFFFFF" bgcolor="#FFFFFF"><span > 请输入搜索条件:

????????????? </span></td>
?? ??? ??? ?</tr>
?? ??? ??? ?<tr>
?? ??? ??? ??? ?<td height="28" bordercolor="#FFFFFF" bgcolor="#FFFFFF" align="center">课程类型:
?? ??? ??? ??? ??? ?<html:select property="courseType">
?? ??? ??? ??? ??? ???? <html:option value=""></html:option>
?? ??? ??? ??? ??? ??? ?<html:option value="校选人文">校选人文</html:option>
?? ??? ??? ??? ??? ??? ?<html:option value="校选自然">校选自然</html:option>
?? ??? ??? ???????? </html:select>? ??? ??? ??? ? ?
?? ??? ??? ???? </td>
?? ??? ??? ??? ?<td height="24" bordercolor="#FFFFFF" bgcolor="#FFFFFF" align="center">有无余量:
? ??? ??? ??? ???????? <html:select property="left">
? ??? ??? ??? ???????????? <html:option value=""></html:option>
?? ??? ??? ??? ??? ??? ?<html:option value="有">有</html:option>
?? ??? ??? ??? ??? ??? ?<html:option value="无">无</html:option>
?? ??? ??? ???????? </html:select> ?
?? ??? ??? ???? </td>
??????????????? <td>上课时间 : <html:select property="week">
?????????????????????????? <html:option value=""></html:option>
?? ??????????????????????? <html:option value="周一">周一</html:option>
?? ??? ??? ??????????????? <html:option value="周二">周二</html:option>
?? ??? ??? ??????????????? <html:option value="周三">周三</html:option>
?? ??? ??? ??????????????? <html:option value="周四">周四</html:option>
?? ??? ??? ??????????????? <html:option value="周五">周五</html:option>
?? ??? ??? ??????????????? <html:option value="周六">周六</html:option>
?? ??? ??? ??????????????? <html:option value="周日">周日</html:option>
?? ????????????????????? </html:select>
?? ????????????????????? <html:select property="time">
?? ??????????????????????? <html:option value=""></html:option>
?? ??????????????????????? <html:option value="第1,2,3节">第1,2,3节</html:option>
?? ??? ??? ??????????????? <html:option value="第4,5节">第4,5节</html:option>
?? ??? ??? ??????????????? <html:option value="第6,7,8节">第6,7,8节</html:option>
?? ??? ??? ??????????????? <html:option value="第10,11,12节">第10,11,12节</html:option>
?? ????????????????????? </html:select>?????????????????? ?
?? ???????????? </td>
?? ??????????? ?
? ??? ??? ??? ? ??? ?<td height="24" bordercolor="#FFFFFF" bgcolor="#FFFFFF" align="center"><html:submit value="搜索"/></td>
?? ??? ??? ?</tr>
? ??? ?? </table>
??? </html:form>

二、在Action类中读取参数调用dao操作数据库方法或业务逻辑处理:

public ActionForward findCourse(ActionMapping mapping,ActionForm form,//用form作为一个传递参数set.
?? ??? ??? ??? ?HttpServletRequest request, HttpServletResponse response) {
?? ??? ??? ?
?? ??? ??? ?DynaActionForm courseForm = (DynaActionForm) form;
?? ??? ???? String courseType = courseForm.getString("courseType");
?? ??? ??? ?String left = courseForm.getString("left");
?? ??? ??? ?String week = courseForm.getString("week");
?? ???????? String time = courseForm.getString("time");
?? ??? ??? ?
?? ???????? Map<String,String> map = new HashMap<String,String>();
?? ??? ??? ?
?? ??? ??? ?map.put("courseType", courseType);
?? ??? ??? ?map.put("left", left);
?? ??? ??? ?map.put("week", week);
?? ??? ??? ?map.put("time", time);
?? ??? ??? ?
?? ??? ???? List<Course> list = courseDao.findBySearch(map);//调用点,为dao类方法
?? ??? ???? Iterator it = list.iterator();
?? ??? ???? int number = 0;
?? ??? ???? while(it.hasNext()){
?? ??? ??? ??? ?it.next();
?? ??? ??? ??? ?number++;
?? ??? ???? }
?? ??? ??? ?request.setAttribute("list", list);//和后面的迭代设置list
?? ??? ??? ?request.setAttribute("number", number);
?? ??? ??? ?return findSch(mapping, form, request, response);//调用点,为action类中方法
?? ??? ?}

三、findBySearch在dao类中处理

?public List<Course> findBySearch(Map<String,String> map) {///////////根据map中的条件查找
?? ??? ?
?? ??? ?List<Course> list=null;
?? ??? ?String courseType=map.get("courseType");
?? ??? ?String remain = map.get("left");
?? ??? ?String week = map.get("week");
?? ??? ?String time = map.get("time");
?? ??? ?if(!courseType.equals("")){
?? ??? ??? ?String sql="FROM Course WHERE ";
?? ??? ???? sql += " courseType ="+"'"+courseType+"'";
?? ??? ???? if(remain.equals("")||remain.equals("有")){//courseType不为空,remain不为空
?? ??? ??? ??? ?sql += " AND remain > 0";
?? ??? ??? ?}
?? ??? ???? if(remain.equals("无")){
?? ??? ??? ??? ?sql += " AND remain = 0";
?? ??? ??? ?}
?? ??? ???? if(!week.equals("")){
?? ??? ??? ???? sql += " AND week ="+"'"+week+"'";
?? ??? ???? }
?? ??? ???? if(!time.equals("")){
?? ??? ???????? sql += " AND time ="+"'"+time+"'";
?? ???????? }
?? ??? ???? list = getHibernateTemplate().find(sql);
?? ??? ???? return list;
?? ??? ?}
?? ??? ?if(courseType.equals("")){
?? ??? ??? ?String sql="FROM Course WHERE courseType IN ('校选人文','校选自然') ";
?? ??? ??? ?if(remain.equals("")||remain.equals("有")){////////////////courseType不为空,remain不为空
?? ??? ??? ??? ?sql += " AND remain > 0";
?? ??? ??? ?}else if(remain.equals("无")){
?? ??? ??? ??? ?sql += " AND remain = 0";
?? ??? ??? ?}
?? ??? ??? ?if(!week.equals("")){
?? ??? ???????? sql += " AND week ="+"'"+week+"'";
?? ???????? }
?? ??? ??? ?if(!time.equals("")){
?? ??? ???????? sql += " AND time ="+"'"+time+"'";
?? ???????? }
?? ???????? list = getHibernateTemplate().find(sql);
?? ???????? return list;
?? ??? ?}
?? ??? ?else{
?? ??? ??? ?list=list =? getHibernateTemplate().find("FROM Course");
?? ??? ??? ?return list;
?? ??? ?
?? ??? ?}
?? ?}

四、 在同一个Action类中调用处理:

public ActionForward findSch(ActionMapping mapping, ActionForm form,///////找出可选的和已选的校选课
?? ??? ??? ??? ?HttpServletRequest request, HttpServletResponse response) {
?? ??? ??? ?
?? ??? ??? ?int stuId = (Integer)request.getSession().getAttribute("loginId");/////////学生序号
?? ??? ???? Map map = stuUserDao.findSchCourse();//dao类调用点
?? ??? ??? ?List<Course> list = (List)map.get("list");//////////////////////查询学生可选的校选课程
?? ??? ??? ?int number = (Integer)map.get("number");
?? ??? ???? List<Course> clist = stuUserDao.findSelectedSch(stuId);////////////查询学生所选校选课程,dao类调用点
?? ??? ??? ?
?? ??? ???? if(request.getAttribute("list")==null){
?? ??? ??????? request.setAttribute("list", list);//关键的返回值设置,使得在jsp中可以直接用${list.成员}将其取得值
?? ??? ??????? request.setAttribute("number", number);
?? ??? ???? }
?? ??? ???? request.setAttribute("clist", clist);//关键的返回值设置
?? ??? ??? ?return mapping.findForward("selectSch");//关键的转到选择校选课页面,也就是取list,clist的页面
?? ??? ?}

public List<Course> findSelectedSch(int stuId){////////////根据学生编号查找已选的校选修课程
?? ??? ?
?????????? Query q = getSession().createQuery("FROM? Course? WHERE idIN(SELECT courseId FROM StuCourse? WHERE stuId=?) AND courseTypeIN('校选人文','校选自然')");?? ??? ?
??? ??????? q.setInteger(0, stuId);
??? ??? ??? List<Course> list = q.list();
??? ??? ??? System.out.println("查询出学生已选的课程");
??? ??? ??? return list;
??? ???? }
???????

五、再调用了stuUserDao类:

public Map<String,Object> findSchCourse(){//////////////////查出学生可选的校选课
?? ??? ??? ?
?? ??? ??? ?Map map =new HashMap();
?? ??? ??? ?String sql = "FROM Course WHERE courseType IN('校选人文','校选自然') AND remain>0 AND selective='可选'";
?? ??? ??? ?List<Course> list = getHibernateTemplate().find(sql);
?? ??? ??? ?map.put("list", list);
?? ??? ??? ?Iterator it = list.iterator();
?? ??? ??? ?int i=0;
?? ??? ??? ?while(it.hasNext()){
?? ??? ??? ??? ?it.next();
?? ??? ??? ??? ?i++;
?? ??? ??? ?}
?? ??? ??? ?map.put("number", i);
?? ??? ??? ?return map;
?? ??? ?}

六、返回到.jsp

1.<html:form action="/stuUser.do?method=insertSch">
?? ??? ?<table width="760" align="center" cellspacing="0">
?? ??? ??? ?<tr>
?? ??? ??? ??? ?<td height="30" align="center" > </td>
?? ??? ??? ??? ?<td align="center" ><strong>课程名称</strong></td>
?? ??? ??? ??? ?<td align="center" ><strong>课程安排</strong></td>
?? ??? ??? ??? ?<td align="center" ><strong>上课时间</strong></td>
?? ??? ??? ??? ?<td align="center" ><strong>上课地点</strong></td>
?? ??? ??? ??? ?<td align="center" ><strong>授课教师</strong></td>
?? ??? ??? ??? ?<td align="center" ><strong>课程学分</strong></td>
?? ??? ??? ??? ?<td align="center" ><strong>课程类型</strong></td>
?? ??? ??? ??? ?<td align="center" ><strong>总量</strong></td>
?? ??? ??? ??? ?<td align="center" ><strong>余额</strong></td>
?? ??? ??? ??? ?<td align="center" ><strong>是否可选</strong></td>
?? ??? ??? ?</tr>
?? ??? ??? ?<%int i=0;%>
?? ??? ??? ?<logic:iterate id="list" name="list">
?? ??? ??? ?<%i++;request.setAttribute("nt",i); %><!-- 实现单选 -->
?? ??? ??? ??? ?<tr>
?? ??? ??? ??? ?? <td height="30" valign="middle">
????? ??? ??? ????? <html:checkbox? property="id"? value="${list.id}"?onclick="validate(${requestScope.nt})"></html:checkbox>
?? ??? ??? ??? ?? </td>
?? ??? ??? ??? ???? <td valign="middle" bgcolor="#ffffff">${list.courseName}</td>
?? ??? ??? ??? ???? <td valign="middle" bgcolor="#ffffff">第${list.startTime}-${list.finishTime}周</td>
?? ??? ??? ??? ???? <td valign="middle" bgcolor="#ffffff">${list.week}${list.time}</td>
?? ??? ??? ??? ???? <td valign="middle" bgcolor="#ffffff">${list.courseAddr}</td>
?? ??? ??? ??? ???? <td valign="middle" bgcolor="#ffffff">${list.courseTch}</td>
?? ??? ??? ??? ??? ?<td valign="middle" bgcolor="#ffffff">${list.courseCredit}</td>
?? ??? ??? ??? ??? ?<td valign="middle" bgcolor="#ffffff">${list.courseType}</td>
?? ??? ??? ??? ??? ?<td valign="middle" bgcolor="#ffffff">${list.total}</td>
?? ??? ??? ??? ??? ?<td valign="middle" bgcolor="#ffffff">${list.remain}</td>
?? ??? ??? ??? ??? ?<td valign="middle" bgcolor="#ffffff">${list.selective}</td>
?? ??? ??? ??? ?</tr>
?? ??? ??? ?</logic:iterate>
?? ??? ??? ?<tr>
?? ??? ??? ??? ?<td align="left"> <html:submit>提交</html:submit></td>
?? ??? ??? ?</tr>
?? ??? ?</table>
?? ?</html:form>
??? 2.<table width="780" align="center" cellspacing="0">
??????????? <tr>
?? ??? ??? ??? ?<td>已选课程</td>
?? ??? ??? ?</tr>
?? ??? ??? ?<tr>
?? ??? ??? ??? ?
?? ??? ??? ??? ?<td? ><strong>课程名称</strong></td>
?? ??? ??? ??? ?<td? ><strong>课程安排</strong></td>
?? ??? ??? ??? ?<td? ><strong>上课时间</strong></td>
?? ??? ??? ??? ?<td? ><strong>上课地点</strong></td>
?? ??? ??? ??? ?<td? ><strong>授课教师</strong></td>
?? ??? ??? ??? ?<td? ><strong>课程学分</strong></td>
?? ??? ??? ??? ?<td? ><strong>课程类型</strong></td>
?? ??? ??? ??? ?<td? ><strong>总量</strong></td>
?? ??? ??? ??? ?<td? ><strong>余额</strong></td>
?? ??? ??? ??? ?<td? ><strong>是否可选</strong></td>
?? ??? ??? ?</tr>
?? ??? ??? ?<c:if test="${!empty clist}">
?? ??? ??? ?<logic:iterate id="clist" name="clist">

?? ??? ??? ??? ?<tr>
?? ??? ??? ??? ? ?
?? ??? ??? ??? ???? <td valign="middle" bgcolor="#ffffff">${clist.courseName}</td>
?? ??? ??? ??? ???? <td valign="middle" bgcolor="#ffffff">第${clist.startTime}-${clist.finishTime}周</td>
?? ??? ??? ??? ???? <td valign="middle" bgcolor="#ffffff">${clist.week}${clist.time}</td>
?? ??? ??? ??? ???? <td valign="middle" bgcolor="#ffffff">${clist.courseAddr}</td>
?? ??? ??? ??? ???? <td valign="middle" bgcolor="#ffffff">${clist.courseTch}</td>
?? ??? ??? ??? ??? ?<td valign="middle" bgcolor="#ffffff">${clist.courseCredit}</td>
?? ??? ??? ??? ??? ?<td valign="middle" bgcolor="#ffffff">${clist.courseType}</td>
?? ??? ??? ??? ??? ?<td valign="middle" bgcolor="#ffffff">${clist.total}</td>
?? ??? ??? ??? ??? ?<td valign="middle" bgcolor="#ffffff">${clist.remain}</td>
?? ??? ??? ??? ??? ?<td valign="middle" bgcolor="#ffffff">已选</td>
?? ??? ??? ??? ??? ?<td? >
?? ??? ??? ??? ???? <html:link page="/stuUser.do?method=dropCourse"
?? ??? ??? ??? ???? paramId="id" paramName="clist" paramProperty="id">退选</html:link>
?? ??? ??? ???? </td>
?? ??? ??? ??? ?</tr>
?? ??? ??? ?</logic:iterate>
??????????? </c:if>

? <tr>
? ?? ?<td height="85" colspan="2" align="center" background="images/bottomU.gif"> </td>
? </tr>
? </table>

读书人网 >网络基础

热点推荐