使用MyEclipse整合SSH模式:登陆范例

2025-02-26 20:50:47
推荐回答(1个)
回答1:

  ssh(struts spring hibernate)模式就是把三个框架合理地结合在一起 以发挥各自在不同位置上的作用 而如何把他们整合在一起就是关键 其实把他们整合在一起并不难 但不是任何时候都需要把他们整合来使用 这要看场合 我们应该合理的使用 合适就好 但不管怎么说它都是一种不错的模式 用MyEclipse工具会很方便地把它们整合在一起 下面简单说一下过程

  MyEclipse整合SSH过程概述

   先加struts包和配置文件 可以手动添加 也可以在myeclipse的myeclipse菜单里面把struts的环境添加上去 后面的spring会用到struts的配置文件

   添加spring 可以像添加struts一样把spring的环境添加上去 不过一定要添加spring的web包 可以把spring的配置文件和struts的配置文件一起放在WEN INF目录下

   添加Hibernate 添加Hibernate的时候对待Hibernate的配置文件我们有两种处理方式 一 按往常一样新建一个配置文件 把它放在src目录下(一般情况) 二 无需新建配置文件 而是把Hibernate的配置信息加入到spring的配置文件里面 这种方式需要建立一个dataSource数据库连接 进行ssh整合一般使用第二种方式

   把三个框架都添加上去之后 就需要把他们联系起来 下面以一个登录的例子说明他们是怎样配合工作的

  MyEclipse整合SSH登陆范例

   ) 首先从前台发送一个登录的请求 「如下」

   < body>< form action= loginAction do method= post >      < table border= >    < tr>< td>name:< /td>    < td>< input type= text name= name />< /td>< /tr>    < tr>< td>password:< /td>    < td>< input type= password name= password />< /td>< /tr>    < tr>< td colspan= align= center >         < input type= submit value= login />    < input type= reset value= reset />    < /td>    < /tr>      < /table>< /form>< /body>

   ) 请求被struts截获 转到struts的配置文件寻找相对应的action「如下」 这时发现找到的是一个代理action 这个代理action就会找到用< plug in>标签注册的spring插件 从而找到spring的配置文件

  

  < action mappings>    < ! login action >        < action name= loginActionForm path= /loginAction             scope= request             type= springframework web struts DelegatingActionProxy >            < forward name= success path= /success jsp >< /forward>            < forward name= failed path= /failed jsp redirect= true >< /forward>        < /action>    < /action mappings>

  < message resources        parameter= yourpany struts ApplicationResources />    < plug in        className= springframework web struts ContextLoaderPlugIn >        < set property property= contextConfigLocation             value= /WEB INF/applicationContext xml />    < /plug in>

   ) 在spring的配置文件中寻找跟path属性的名称相同的action 「如下」 在action调用了userService类 userService又调用了userDAO 在userDAO中我们需要做用户的登录验证

  

  < ! 依赖注入action >    < bean name= /loginAction class= dc action LoginAction >        < property name= userService ref= userService >< /property>    < /bean>

  < ! 依赖注入UserDAO >    < bean id= userDAO class= dc dao UserDAOImpl >        < property name= sessionFactory ref= SessionFactory >< /property>    < /bean>

  < ! 依赖注入UserService >    < bean id= userService class= dc dao UserServiceImpl >        < property name= dao ref= userDAO >< /property>    < /bean>

  前面说过 Hibernate的配置信息是添加在spring的配置文件里的 在上面的userDAO中引用的sessionFactory就需要用到这些信息「如下」

   < ! 配置连接数据库和事物管理参数 >    < bean id= dataSource         class= mons dbcp BasicDataSource >        < property name= driverClassName             value= mysql jdbc Driver >        < /property>        < property name= url             value= jdbc:mysql:// : /test >        < /property>        < property name= username value= root >< /property>        < property name= password value= root >< /property>    < /bean>    < bean id= SessionFactory         class= springframework orm hibernate LocalSessionFactoryBean >        < property name= dataSource >            < ref bean= dataSource >< /ref>        < /property>        < property name= hibernateProperties >            < props>                < prop key= hibernate dialect >                    hibernate dialect MySQLDialect                < /prop>                < prop key= hibernate show_sql >true< /prop>                < prop key= hibernate format_sql >true< /prop>            < /props>        < /property>        < property name= mappingResources >            < list>                < value>/dc/pojo/User hbm xml< /value>< /list>        < /property>    < /bean>

  userDAO的验证方法「如下」 在这个类中继承了一个HibernateDaoSupport类 继承后在配置文件中就可以添加一个sessionFactory属性 用于获取HibernateSessionFactory

   public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {    public User checkUser(String name String password) {        String sql = from User u where u name=? and password=? ;        List userList = this getHibernateTemplate() find(sql toString()                 new Object[]{name password});        return userList size() == ? null : (User) userList get( );    }}

   ) 找到action后处理用户的登录 「如下」 userService调用了getUserByParam方法 而在getUserByParam方法中又调用了userDAO的checkUser方法 验证通过后转到成功页面 否则转到失败页面

   //用户登录验证    public ActionForward execute(ActionMapping mapping ActionForm form             HttpServletRequest request HttpServletResponse response) {        LoginActionForm laf = (LoginActionForm) form;        String name = laf getName();        String password = laf getPassword();        User user=userService getUserByParam(name password);        if(user!=null){            HttpSession session=request getSession();            session setAttribute( user user);            return mapping findForward( success );        }        return mapping findForward( failed );    }

  这就是一个简单的流程 当然还有一些细节的东西需要我们去摸索 在这过程中 三个框架都发挥了各自的作用 如果在一些比较复杂的应用里面这是很方便的 这种模式使一个系统变得灵活 易于扩展和维护 所以得到了比较广泛的应用

lishixinzhi/Article/program/Java/ky/201311/28247