知识屋:更实用的电脑技术知识网站
所在位置:首页 > 操作系统 > linux

SSH1环境搭建及示例

发布时间:2014-09-05 16:27:40作者:知识屋

1.前期准备
  Eclipse 3.7.0(下载地址:http://www.eclipse.org/downloads/)
  Tomcat7.0(下载地址:http://tomcat.apache.org/download-70.cgi)
  MySql Server5.0(下载地址:http://dev.mysql.com/downloads/)
  struts-1.2.9(下载地址:http://struts.apache.org/download.cgi#struts221)
  spring-2.0(下载地址:http://sourceforge.net/projects/springframework/files/)
  hibernate-3.2.0(下载地址:http://sourceforge.net/projects/hibernate/files/)
 
2.创建Web工程
  创建Web工程后并建立几个包
  ssh.dan.web/ssh.dan.manager/ssh.dan.dao/ssh.dan.domain
 
3.配置Struts1
  引入Struts1的相关jar包拷贝到WEB-INF/lib目录下
     Struts_Home/lib/*.jar
 引入Struts1的配置文件
     在webapps文件夹中解压struts-blank.war,将其中的struts-config.xml拷贝到项目中(WEB-INF下)   
 
4.配置Spring
 引入Spring的相关jar包拷贝到WEB-INF/lib目录下
   SPRING_HOME/dist/spring.jar
   SPRING_HOME/lib/log4j/log4j-1.2.14.jar
   SPRING_HOME/lib/jakarta-commons/commons-logging.jar
   SPRING_HOME/ lib/junit/junit.jar
   SPRING_HOME/ lib/aspect/*.jar
  引入Spring的配置文件
   将spring_home/samples/jpetstore/war/WEB-INF下的applicationContext.xml拷贝到项目中(src下),为了对其他的框架进行配置,我们将其复制为四份,分别命名为applicationContext-common.xml, applicationContext-daos.xml,applicationContext-managers.xml, applicationContext-actions.xml
 
5.配置Hibernate
  引入Hibernate的相关jar包拷贝到WEB-INF/lib目录下
    Hibernate_Home/lib/*.jar
   Hibernate_Home/hibernate3.jar
  再将mysql驱动拷贝到WEB-INF/lib目录下
   Mysql_Home/src/lib/jdbc2_0-stdext.jar
  引入Hibernate的配置文件
   将Hibernate_Home/etc下的hibernate.cfg.xml拷贝到项目中(src下)
 
6.编写代码
  基本框架建立好后,我们添加几个类,展示一下目录:
 /
  下面是示例代码
   先建立数据库ssh_test,再编写实体类建表
   建立表时需要用到一些工具,我们建立一个工具包
   domain包
       User.java
 
[java] view plaincopy
package ssh.dan.domain; 
 
public class User { 
     
    private String userName; 
     
    private String passWord; 
 
    public String getUserName() { 
        return userName; 
    } 
 
    public void setUserName(String userName) { 
        this.userName = userName; 
    } 
 
    public String getPassWord() { 
        return passWord; 
    } 
 
    public void setPassWord(String passWord) { 
        this.passWord = passWord; 
    } 
 

 
      User.hbm.xml
 
[java] view plaincopy
<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC  
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="ssh.dan.domain.User" table="t_user"> 
        <id name="userName"> 
            <generator class="assigned"/> 
        </id> 
        <property name="passWord"/> 
    </class> 
</hibernate-mapping> 
 
    配置文件 www.zhishiwu.com
      hibernate.cfg.xml(配置数据库及映射文件)
[html] view plaincopy
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
 
<hibernate-configuration> 
    <session-factory> 
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh_test</property> 
        <property name="hibernate.connection.username">root</property> 
        <property name="hibernate.connection.password">dandan</property> 
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
        <property name="hibernate.show_sql">true</property> 
        <property name="hibernate.hbm2ddl.auto">update</property> 
         
        <mapping resource="ssh/dan/domain/User.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 
 
 
    dao包
      UserDao.java
 
[java] view plaincopy
package ssh.dan.dao; 
 
import ssh.dan.domain.User; 
 
public interface UserDao { 
 
    /**
     * 添加用户
     * @param user
     */ 
    public void addUser(User user);  
     
    /**
     * 根据用户名查找用户
     * @param userName
     * @return
     */ 
    public User findUserByName(String userName); 
     

 
      UserDaoImpl.java(使用了spring框架对Hibernate的集成的HibernateDaoSupport类)
 
[java] view plaincopy
package ssh.dan.dao; 
 
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 
 
import ssh.dan.domain.User; 
 
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { 
 
    @Override 
    public void addUser(User user) { 
        getHibernateTemplate().save(user); 
    } 
 
    @Override 
    public User findUserByName(String userName) { 
        return (User)getHibernateTemplate().load(User.class, userName);              
    } 
 

 
    manager包
      UserManager.java
[java] view plaincopy
package ssh.dan.manager; 
 
import ssh.dan.domain.User; 
 
public interface UserManager { 
 
    public void addUser(User user); 
     
    public User findUserByName(String userName); 

 
 
      UserManagerImpl.java(使用setter注入方式)
[java] view plaincopy
package ssh.dan.manager; 
 
import ssh.dan.dao.UserDao; 
import ssh.dan.domain.User; 
 
public class UserManagerImpl implements UserManager { 
 
    private UserDao userDao; 
     
    @Override 
    public void addUser(User user) { 
        try{     
            if(findUserByName(user.getUserName()) != null){ 
                userDao.addUser(user); 
            } 
        }catch(Exception e){ 
            e.printStackTrace(); 
        } 
    } 
     
    @Override 
    public User findUserByName(String userName) {  
        User user = null; 
        try{             
            user = userDao.findUserByName(userName); 
        }catch(Exception e){ 
            e.printStackTrace(); 
        } 
        return user;         
    } 
     
    public void setUserDao(UserDao userDao) { 
        this.userDao = userDao; 
    }    
 

 
 
    web包
      UserActionForm.java
 
[java] view plaincopy
package ssh.dan.web; 
 
import org.apache.struts.action.ActionForm; 
 
public class UserActionForm extends ActionForm { 
 
    private String userName; 
     
    private String passWord; 
 
    public String getUserName() { 
        return userName; 
    } 
 
    public void setUserName(String userName) { 
        this.userName = userName; 
    } 
 
    public String getPassWord() { 
        return passWord; 
    } 
 
    public void setPassWord(String passWord) { 
        this.passWord = passWord; 
    } 

 
      UserAction.java
 
[java] view plaincopy
package ssh.dan.web; 
 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.apache.struts.action.Action; 
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward; 
import org.apache.struts.action.ActionMapping; 
 
import ssh.dan.domain.User; 
import ssh.dan.manager.UserManager; 
 
 
 
public class UserAction extends Action { 
     
    private UserManager userManager; 
     
    @Override 
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) 
            throws Exception { 
        UserActionForm uaf = (UserActionForm)form; 
        User user = new User(); 
        user.setUserName(uaf.getUserName()); 
        user.setPassWord(uaf.getPassWord()); 
             
        userManager.addUser(user); 
        return mapping.findForward("success"); 
    } 
 
 
    public void setUserManager(UserManager userManager) { 
        this.userManager = userManager; 
    } 

 
    配置文件
      applicationContext-common.xml(配置sessionFactory和事务管理器):
[html] view plaincopy
<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:aop="http://www.springframework.org/schema/aop" 
         xmlns:tx="http://www.springframework.org/schema/tx" 
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
 
    <!-- 配置SessionFactory --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
        <property name="configLocation"> 
            <value>classpath:hibernate.cfg.xml</value> 
        </property> 
    </bean> 
     
    <!-- 配置事务管理器--> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
        <property name="sessionFactory"> 
            <ref bean="sessionFactory"/>           
        </property> 
    </bean> 
     
    <!-- 那些类那些方法使用事务--> 
    <aop:config> 
        <aop:pointcut id="allManagerMethod" expression="execution(* ssh.dan.manager.*.*(..))"/> 
        <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/> 
    </aop:config> 
     
    <!-- 事务的传播特性-->   
    <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
        <tx:attributes> 
            <tx:method name="add*" propagation="REQUIRED"/> 
            <tx:method name="del*" propagation="REQUIRED"/> 
            <tx:method name="modify*" propagation="REQUIRED"/> 
            <tx:method name="*" propagation="REQUIRED" read-only="true"/> 
        </tx:attributes> 
    </tx:advice> 
 
</beans> 
 
 
      applicationContext-daos.xml(声明dao包中的类):
 
[html] view plaincopy
<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:aop="http://www.springframework.org/schema/aop" 
         xmlns:tx="http://www.springframework.org/schema/tx" 
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
 
    <bean id="userDao" class="ssh.dan.dao.UserDaoImpl"> 
        <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
 
</beans> 
 
      applicationContext-managers.xml(声明manager包中的类,并注入相应的dao类):
 
[html] view plaincopy
<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:aop="http://www.springframework.org/schema/aop" 
         xmlns:tx="http://www.springframework.org/schema/tx" 
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
 
    <bean id="userManager" class="ssh.dan.manager.UserManagerImpl"> 
        <property name="userDao" ref="userDao"/> 
    </bean>    
 
</beans> 
 
      applicationContext-actions.xml(声明action类,并注入相应的manager类):
 
[html] view plaincopy
<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:aop="http://www.springframework.org/schema/aop" 
         xmlns:tx="http://www.springframework.org/schema/tx" 
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
 
    <bean name="/user" class="ssh.dan.web.UserAction"> 
        <property name="userManager" ref="userManager"/> 
    </bean> 
 
</beans> 
 
      struts-config.xml(配置action和actionform,使用spring中的代理来配置action的type):
 
[html] view plaincopy
<?xml version="1.0" encoding="ISO-8859-1" ?> 
 
<!DOCTYPE struts-config PUBLIC 
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" 
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> 
<struts-config> 
    <form-beans> 
        <form-bean name="userForm" type="ssh.dan.web.UserActionForm"/> 
    </form-beans> 
         
    <action-mappings> 
         
        <action path="/user" 
                type="org.springframework.web.struts.DelegatingActionProxy" 
                name="userForm" 
                scope="request" 
        >     
            <forward name="success" path="/success.jsp"/> 
        </action> 
             
    </action-mappings> 
</struts-config> 
 
      web.xml(配置struts的actionservlet以及spring集成的ContextLoaderListener,CharacterEncodingFilter和OpenSessionInViewFilter):
[html] view plaincopy
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.4"  
    xmlns="http://java.sun.com/xml/ns/j2ee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
 
   <!-- Standard Action Servlet Configuration (with debugging) --> 
  <servlet> 
    <servlet-name>action</servlet-name> 
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 
    <init-param> 
      <param-name>config</param-name> 
      <param-value>/WEB-INF/struts-config.xml</param-value> 
    </init-param> 
    <init-param> 
      <param-name>debug</param-name> 
      <param-value>2</param-value> 
    </init-param> 
    <init-param> 
      <param-name>detail</param-name> 
      <param-value>2</param-value> 
    </init-param> 
    <load-on-startup>2</load-on-startup> 
  </servlet> 
 
 
  <!-- Standard Action Servlet Mapping --> 
  <servlet-mapping> 
    <servlet-name>action</servlet-name> 
    <url-pattern>*.do</url-pattern> 
  </servlet-mapping> 
   
   
  <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:applicationContext-*.xml</param-value> 
  </context-param> 
   
  <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener> 
   
  <filter> 
    <filter-name>CharacterEncodingFilter</filter-name> 
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
    <init-param> 
        <param-name>encoding</param-name> 
        <param-value>GB18030</param-value> 
    </init-param> 
  </filter> 
   
  <filter-mapping> 
    <filter-name>CharacterEncodingFilter</filter-name> 
    <url-pattern>*.do</url-pattern> 
  </filter-mapping> 
     
  <filter> 
    <filter-name>OpenSessionInViewFilter</filter-name> 
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 
  </filter> 
   
  <filter-mapping> 
    <filter-name>OpenSessionInViewFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
  </filter-mapping> 
</web-app> 
 
              
                
   新建数据库ssh_test,启动Tomcat后表就建立好了,然后开始运行,结果:
 
   index界面:
 /
  user_add界面:
 /
  success界面:
 /
  数据库结果:

/


作者 赵丹丹的专栏
(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜