快速搭建ssh项目环境的具体步骤

来源:互联网 发布:国家社科基金数据库 编辑:程序博客网 时间:2024/05/21 22:26

1.1. 项目环境搭建

1.1.1.     框架在项目中功能分配

MyEclipse中添加ssh2 capabilities以及导MyEclipse自带包的操作步骤参考:http://www.doc88.com/p-631428603878.html

1.1.2.     测试Spring

配置Bean

<bean id="date" class="java.util.Date" />

获取Spring配置文件

@Test

public void testSpring(){

    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

    Date date=(Date) context.getBean("date");

    System.out.println(date);

}

显示结果:

Wed Feb 13 14:39:23 CST 2013

1.1.3.     测试Hibernate

<hibernate-configuration>

    <session-factory>

        <propertyname="dialect">org.hibernate.dialect.MrySQLDialect</property>

        <propertyname="connection.url">jdbc:mysql://localhost:3306/shop</property>

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

        <propertyname="connection.password">root</property>

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

        <propertyname="myeclipse.connection.profile">mysql5</property>

        <property name="javax.persistence.validation.mode">none</property>

    </session-factory>

</hibernate-configuration>

l  生成了POJO类如下:

public class Category implements java.io.Serializable {

   private Integerid;

   private Stringtype;

   private Boolean hot;

    // 省略get set方法

}

l  生成orm映射文件如下:

<hibernate-mapping>

    <classname="cn.it.shop.pojo.Category"table="category"catalog="shop">

        <idname="id"type="java.lang.Integer">

            <columnname="id"/>

            <generatorclass="native"/>

        </id>

        <propertyname="type"type="java.lang.String">

            <columnname="type"length="20"/>

        </property>

        <propertyname="hot"type="java.lang.Boolean">

            <columnname="hot"/>

        </property>

    </class>

</hibernate-mapping>

l  测试Hibernate通过编程式事务来完成事务的管理, 能插入成功则说明环境OK

Session session = HibernateSessionFactory.getSession();

      Category category = new Category();

      category.setType("A类别");

      try {

         session.getTransaction().begin();

         session.save(category);

         session.getTransaction().commit();

      } catch (Exception e) {

         e.printStackTrace();

         session.getTransaction().rollback();

      }finally{

         session.close();

      }

1.1.4.     Spring与Hibernate整合

l  配置数据源

<!—配置成后,可以删除hibernate数据库的连接信息—>

<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">

      <propertyname="jdbcUrl"value="jdbc:mysql://localhost:3306/shop"/>

      <propertyname="password"value="root"/>

      <propertyname="user"value="root"/>

      <propertyname="driverClass"value="com.mysql.jdbc.Driver"/>

   </bean>

l  LocalSessionFactoryBean

<!-- SpringHiberante整合的目的是,Spring来管理声明式事务 -->

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <propertyname="configLocation"value="classpath:hibernate.cfg.xml"/>

<propertyname="dataSource"ref="dataSource"/>

</bean>

l  配置事务管理器transactionManager

<beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    <propertyname="sessionFactory"ref="sessionFactory"/>

</bean>

l  加入AOP标签

<beans

    xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p"

    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-3.0.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

    ">

</beans>

l  配置通知:

<tx:adviceid="advice"transaction-manager="transactionManager">

       <tx:attributes>

           <tx:methodname="save*"propagation="REQUIRED"rollback-for="Throwable"/>

           <tx:methodname="delete*"propagation="REQUIRED"/>

           <tx:methodname="update*"propagation="REQUIRED"/>

           <tx:methodname="*"read-only="true"/>

       </tx:attributes>

</tx:advice>

l  配置AOP切面表达式

<aop:config>

       <aop:advisoradvice-ref="advice"pointcut="execution(* cn.it.shop.service.impl.*.*(..))"/>

    </aop:config>

l  测试Spring与Hibernate集成

public class CategoryServiceImpl implements CategoryService{

   private SessionFactorysessionFactory;

   public void setSessionFactory(SessionFactory sessionFactory) {

      this.sessionFactory = sessionFactory;

   }

   public void save(Category category){

      Session session=sessionFactory.getCurrentSession();

      session.save(category);

   }

}

l  测试代码如下:

@Test

public void testHibernate_Spring(){

      ApplicationContext context = new ClassPathXmlApplicationContext(

      "applicationContext.xml");

      CategoryService categoryImpl=(CategoryService)context.getBean("categoryServiceImpl");

      Category category = new Category();

      category.setCtype("动物33");

      categoryImpl.save(category);

   }

1.1.5.     测试Struts框架

注意:如果Struts没有与Spring整合则不要添加struts2-spring-plugin添加后,反而创建Action的时候会报错.

l  添加Struts.xml配置文件, 设置基本的参数, 和Struts常量

<struts>

    <constantname="struts.devMode"value="true"/>

    <packagename="shop"extends="struts-default">

       <!-- action配置在此处 -->

    </package>

</struts>   

l  配置Struts过滤器

<filter>

       <filter-name>struts</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

       <filter-name>struts</filter-name>

       <url-pattern>*.action</url-pattern>

</filter-mapping>

l  创建Action测试

public String login(){

       System.out.println("---进入到了Action---");

       returnSUCCESS;

}

<packagename="shop"extends="struts-default">

       <!-- action配置在此处 -->

       <actionname="users"class="cn.it.shop.action.UsersAction"method="login">

           <resultname="success">/success.jsp</result>

       </action>

</package>

1.1.6.     Spring与Struts整合

Action 纳入到Spring的容器中.Spring来创建,这样Action所依赖的业务逻辑类也会注入进来, struts2-spring-plugin插件中修改了创建ActtionBean,改成了StrutsSpring


我们会发现struts2-spring-plugin只有一个类和一个struts-plugin.xml配置文件, 我们打开struts-plugin配置文件源码如下:struts-plugin.xml此配置文件在项目启动的时候会被加载

<struts>

    <beantype="com.opensymphony.xwork2.ObjectFactory"name="spring"class="org.apache.struts2.spring.StrutsSpringObjectFactory"/>

   

    <!--  修改了创建Struts Action的工厂类,默认的配置在default.properties配置文件中 -->

    <constantname="struts.objectFactory"value="spring"/>

    </package>   

</struts>

default.properties:的缺省值设置

### if specified, the default object factory can be overridden here

### Note: short-hand notation is supported in some cases, such as "spring"

###       Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here

# struts.objectFactory = spring

 

### specifies the autoWiring logic when using the SpringObjectFactory.

### valid values are: name, type, auto, and constructor (name is the default)

struts.objectFactory.spring.autoWire = name

l  配置监听器

<context-param>

    <!-- 默认是到根目录加载配置文件,所以要设置spring配置文件的实际目录 -->

    <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>

l  测试Spring与Struts集成

public String login(){

    // 如果能注入,则说明完成了Action与业务逻辑类的解耦,整合成功!

    System.out.println(usersServiceImpl);

    return SUCCESS;

}

0 0
原创粉丝点击