aijava

welcome to my online log ! open java new world! Taste java charm........
posts - 1, comments - 4, trackbacks - 0, articles - 42
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

JB整合SSH

Posted on 2006-11-05 21:29 阅读(198) 评论(0)  编辑  收藏 所属分类: Spring框架学习

在网上经常看到用Eclipse整合SSH然后我就在学校用JB整合了一个SSH的例子演示出来,很有成就感! 呵呵
打开JB建一个工程然后〉建站点〉建AcionForm , Acion , JSP这样就把Struts框架搭建出来了!
然后就是把Hibernate的Jar 和Spring 的Jar 导进工程当中建好的Struts框架如图:
然后开始编写一个接口UserDAO.java
package SpringPack;

public interface UserDAO {
  public abstract boolean isValidUser(String name,String password);
}

编写一个类继承该接口UserDAOImp.java
package SpringPack;

import org.springframework.orm.hibernate.support.*;
import net.sf.hibernate.SessionFactory;
import java.util.List;
import Hibernate.hibernateTest;
import net.sf.hibernate.*;

public class UserDAOImp extends HibernateDaoSupport implements UserDAO{

  public boolean isValidUser(String name,String pwd){
    hibernateTest t = null;
    try {
      t = new hibernateTest();
    }
    catch (HibernateException ex) {
      ex.printStackTrace();
    }
    if(t.checkUser(name,pwd)) return true;
    return false;
  }
}

然后编写一个Bean用来映射数据表的字段
package Hibernate;

public class user {
  private String username;
  private String password;
  public user() {
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getUsername() {
    return username;
  }

  public String getPassword() {
    return password;
  }
}

编写hibernate.cfg.xml文件用于连接数据库的配置文件:
<?xml version='1.0' encoding='big5'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "
http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd
">

<hibernate-configuration>

    <session-factory>


        <property name="show_sql">true</property>

        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="connection.url">jdbc:mysql://localhost/myDB</property>

        <property name="connection.username">root</property>

        <property name="connection.password">test</property>


        <mapping resource="user.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


然后下面编写一个描述数据库映射表字段信息的mapping文件User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "
http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd
">

<hibernate-mapping>

    <class name="Hibernate.user" table="user">

        <id name="username" type="string" unsaved-value="null">
            <column name="username" sql-type="varchar(20)" />
            <generator class="uuid.hex"/>
        </id>

        <property name="password" type="string" column="password"/>

    </class>

</hibernate-mapping>
好了现在Hibernate已基本搭建出来了,下一步我们要用Spring来整合Hibernate 和Struts 操作步骤很简单编写一个applicationContext.xml文件放在站点的根目录即可:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"
http://www.springframework.org/dtd/spring-beans.dtd
">
<beans>
  <bean name="/loginAction"
    class="StrutsPack.LoginAction"
    singleton="false">
  </bean>
</beans>
要整合Struts还要在Struts-config.xml文件中做一些修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "
http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd
">
<struts-config>
  <form-beans>
    <form-bean name="loginActionForm" type="StrutsPack.LoginActionForm" />
  </form-beans>
  <action-mappings>
    <action  name="loginActionForm" path="/loginAction" scope="session" type="org.springframework.web.struts.DelegatingActionProxy" validate="false">
      <forward name="Success" path="/success.jsp" />
      <forward name="Failure" path="/Failure.jsp" />
    </action>
  </action-mappings>
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
  </plug-in>
</struts-config>
好了现在还要编写一个Hibernate的辅助文件如下:
package Hibernate;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import java.util.List;
import java.util.ListIterator;

public class hibernateTest {
  Session session;
  public hibernateTest()
  throws HibernateException{
    SessionFactory factory=new Configuration().configure().buildSessionFactory();
     session=factory.openSession();


  }
  public boolean checkUser(String name,String pwd){
  List count = null;
  try {
    Query q = session.createQuery(
        "select count(*) from user where username=? and password=?");
    q.setString(1, name);
    q.setString(2, pwd);
    count = q.list();
  }
  catch (HibernateException ex) {
    ex.printStackTrace();
  }
  ListIterator list=count.listIterator();
  Integer s=(Integer)list.next();
  if(s.intValue() > 0 ) return true;
  return false;
  }
}

然后就可以在LoginAction.java中调用了LoginAction.java文件如下:
package StrutsPack;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import SpringPack.*;

public class LoginAction
    extends Action {
  private static final String SUCCESS="Success";
  private static final String FAILURE="Failure";

  private UserDAO userdao=new UserDAOImp();

  public ActionForward execute(ActionMapping mapping,
                               ActionForm actionForm,
                               HttpServletRequest servletRequest,
                               HttpServletResponse servletResponse) {
    LoginActionForm loginform = (LoginActionForm) actionForm;

    if(userdao.isValidUser(loginform.getUsername(),loginform.getPassword())){
    return mapping.findForward(SUCCESS);
    }
    return mapping.findForward(FAILURE);
  }

  public UserDAO getUserDAO() {
    return userdao;
}
public void setUserDAO(UserDAO userDAO) {
  this.userdao = userDAO;
}

}
至此JB开发一个简单的SSH就结束了感觉就是两个字麻烦!还是使用Eclispe开发方便!下面是具体的path:

http://freehost13.websamba.com