`
blues1021
  • 浏览: 144977 次
  • 性别: Icon_minigender_1
  • 来自: 南宁
社区版块
存档分类
最新评论

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

    博客分类:
  • SSH
 
阅读更多

一、在.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 >&nbsp;请输入搜索条件:

              </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 id IN(SELECT courseId FROM StuCourse  WHERE stuId=?) AND courseType IN('校选人文','校选自然')");        
            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" >&nbsp;</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">&nbsp;</td>
  </tr>
  </table>

分享到:
评论

相关推荐

    Spring/Struts2整合项目

    整合Spring和Struts2的主要目标是利用Spring的强大功能来管理Struts2中的Action类以及相关的业务逻辑,同时借助Struts2的优秀表现力来处理用户界面和请求转发。 **整合步骤:** 1. **配置环境**:确保已经安装了...

    搭建 Struts2.0 + Hibernate3.2 + Spring2.5 说明

    &lt;result name="showdetails"&gt;showdetails.jsp &lt;result name="userError"&gt;userError.jsp &lt;result name="showCommodityClass"&gt;showCommodityClass.jsp &lt;result name="showCar" type="redirect"&gt;showCar.jsp ...

    Eclipse中SSH项目框架步骤

    3. 在“beans.xml”中配置SessionFactory和DAO。 至此,一个基本的SSH项目框架已经在Eclipse中搭建完成。运行项目,通过访问相应的URL,如“http://localhost:8080/SSHProject/hello.action”,应该能看到预期的...

    通过IDEA创建的SSH框架,实现了基本的查找和插入操作,适合初学者

    SSH框架,全称为Struts2、Spring和Hibernate,是Java Web开发中常用的一种集成框架,它结合了MVC模式的Struts2、依赖注入的Spring以及对象关系映射的Hibernate。这个框架组合可以帮助开发者更高效地构建应用程序,...

    JSF2.xdatatable分页控件与左侧菜单最简单应用

    使用帮助:http://blog.csdn.net/ptianfeng/article/details/8217176 JSF2.xdatatable分页控件与左侧菜单最简单应用  JSF2.x,功能强大,使用方便。全世界使用JSF的人越来越多。而且也有很多很好的控件给发出来...

Global site tag (gtag.js) - Google Analytics