SSH框架从底层数据库到jsp页面响应流程及注意事项

来源:互联网 发布:天津外国语大学 知乎 编辑:程序博客网 时间:2024/05/01 16:57

SSH框架完整实现从底层到jsp页面流程及注意事项

本文主要是以后为自己留个印象,第一次完整的弄清楚ssh框架使用时的开发流程,本文主要以一个实现显示数据库中视频章节信息为例,一切从简,重在开发流程上。

函数文件的书写

注意:下面给出的函数都省略了对应接口文件的书写,请自行补充。
先给出ChapterDAO文件中的getChaptersByCourseid()函数

public List getChaptersByCourseid() {        // TODO Auto-generated method stub        Session session=sessionFactory.openSession();        Transaction ts=session.beginTransaction();        Query query=session.createQuery("from Chapter where courseid=1");        List chapters=query.list();        System.out.println("c");        System.out.println(chapters);        ts.commit();        session.close();        return chapters;    }

然后到ChapterService文件中的getChaptersByCourseid()函数

public List getChaptersByCourseid() {        // TODO Auto-generated method stub            return chapterDAO.getChaptersByCourseid();      }

最后就是action文件中的browseChapter()函数

public String browseChapter() throws Exception{        System.out.println("abc");        List chapters = chapterService.getChaptersByCourseid();        Map request=(Map) ActionContext.getContext().get("request");        request.put("chapters",chapters);        return SUCCESS;    }

上述文件都书写完整后,比较重要的就是application文件和struts文件的书写,也就是配置的问题,这里不处理好就会有各种各样问题。

配置文件相关

application文件相关

首先,mode文件需要保证没问题,这里使用的是chapter (其他的都是多的)

<property name="mappingResources">            <list>                <value>com/xuan/model/User.hbm.xml</value>                <value>com/xuan/model/Guashi.hbm.xml</value>                <value>com/xuan/model/Catalog.hbm.xml</value>                <value>com/xuan/model/Xuanke.hbm.xml</value>                <value>com/xuan/model/Userdetail.hbm.xml</value>                <value>com/xuan/model/Course.hbm.xml</value>                <value>com/xuan/model/Zhiliaolink.hbm.xml</value>                <value>com/xuan/model/Message.hbm.xml</value>                <value>com/xuan/model/Zhiliao.hbm.xml</value>                <value>com/xuan/model/Operate.hbm.xml</value>                <value>com/xuan/model/Chapter.hbm.xml</value>                <value>com/xuan/model/Forum.hbm.xml</value>                <value>com/xuan/model/Reforum.hbm.xml</value>                <value>com/xuan/model/Videoinfo.hbm.xml</value></list>        </property></bean>

然后是以下的配置

    <bean id="chapterDAO" class="com.xuan.dao.impl.ChapterDAO">        <property name="sessionfactory">            <ref bean="sessionFactory" />        </property>    </bean>    <bean id="chapterService" class="com.xuan.service.impl.ChapterService">        <property name="chapterDAO" ref="chapterDAO"></property>    </bean>     <bean id="courseAction" class="com.xuan.action.courseAction">        <property name="catalogService" ref="catalogService"></property>        <property name="courseService" ref="courseService"></property>        <property name="chapterService" ref="chapterService"></property>    </bean>

这里需要理解为什么这样写,这里是分别导入了dao service 以及action文件,以及他们之间的关系

Struts文件

<action name="browseChapter" class="courseAction" method="browseChapter"><result name="success">/chapter.jsp</result><interceptor-ref name="defaultStack"></interceptor-ref></action>

总之,大体按照以上流程开发即可,遇到问题,不要慌乱,一般都是些很简单的问题。

1 0