您的位置:首页 > 编程语言 > Java开发

SSH各部分的作用

2011-09-20 14:09 246 查看
1.主要说明SSH各部分的作用:

1>Struts2,Struts总的来说就是两个字--"动作"

请求到达之后核心的过滤器会调用对应的action,找到相对应的result来实现跳转。

2>hibernate,hibernate总的来说就是三个字--"持久层"

主要负责持久层:通过hibernate.cfg.xml配置数据库的连接,配置javabean的数据库映射xml文件,书写持久层,这里的持久层Manager,通过spring注入的DAO数据库的实现完成一些方法,其中hibernate 的DAO要通过Spring提供的HibernateDaoSupport类,进行方法的实现与数据库的增删改,更新等操作.

在有了spring之后hibernate中的内容可以在spring的配置文件中配置。Hiernate.cfg.xml就不再需要。

3>spring,spring总的来说就是两个字--"管理",管理整个流程,

对事务的管理,对hibernate的管理(创建数据库的连接),对Struts的管理(管理Struts全部的动作),管理hibernate的Manager和Dao(主要是对Manager中的dao进行对象的注入).

2.主要的配置和代码:

1>.总的配置web.xml

web.xml是J2EE web方向的心脏,配置如下:

<?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">
<!—指定核心的过滤器-->
<filter>
<filter-name>dang</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>dang</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!—指定那个spring配置文件的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:base.xml</param-value>
</context-param>

<!—指定spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

2>Struts2的配置:

<struts>

<!--可以使用<include file=”XX.xml”>来调用其他的配置文件内容,使整个项目结构清晰。-->
<include file="struts-user.xml" />
<include file="struts-order.xml" />
<include file="struts-cart.xml" />
<include file="struts-main.xml" />

<!—与spring整合,指定action实例交由struts2-spring插件来管理-->
<constant name="struts.objectFactory" value="spring"></constant>

<package name="dang-default" extends="json-default">
<interceptors>
<interceptor name="checkUser" class="org.tarena.interceptor.CheckUserInterceptor"/>
<interceptor name="redirctUrl" class="org.tarena.interceptor.Redirectinterceptor"/>
<interceptor-stack name="logincheck">
<interceptor-ref name="checkUser"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>

<interceptor-stack name="redirectrInterceptor">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="redirctUrl"/>
</interceptor-stack>
</interceptors>

<default-action-ref name="main" />
<global-results>
<result name="error" type="redirect">/error.jsp</result>
<result name="not_login" type="redirectAction">
<param name="actionName">loginForm</param>
<param name="namespace">/user</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"/>
</global-exception-mappings>
<action name="main">
<result type="redirectAction">main/main.action</result>
</action>
</package>

</struts>

3>hibernate的配置

 主要是定义hibernate中的实体类和数据库表之间的关系—映射文件。

4>Spring的配置

<context:annotation-config /> 

  <bean id="userDao" class="org.tarena.dao.impl.UserDaoImpl" /> 

  <bean id="userService" class="org.tarena.service.UserServiceImpl" /> 

  <bean id="productDao" class="org.tarena.dao.impl.ProductDaoImpl" /> 

  <bean id="productService" class="org.tarena.service.ProductServiceImpl" /> 

  <bean id="orderDao" class="org.tarena.dao.impl.OrderDaoImpl" /> 

- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

  <property name="sessionFactory" ref="sessionFactory" /> 

  </bean>

<!—配置连接数据库的参数-->

- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

  <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 

  <property name="url" value="jdbc:mysql://localhost:3306/test" /> 

  <property name="username" value="root" /> 

  <property name="password" value="anmyz1986" /> 

  </bean>

<!—配置hibernate的sessionFactory实力,-->

- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

- <property name="dataSource">

  <ref bean="dataSource" /> 

 
4000
</property>

- <property name="hibernateProperties">

- <props>

  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 

  <prop key="hibernate.show_sql">true</prop> 

  </props>

  </property>

<!—定义映射文件,找到实体类与数据库中表的关系-->

- <property name="mappingResources">

- <list>

  <value>org/tarena/domain/Address.hbm.xml</value> 

  <value>org/tarena/domain/Category.hbm.xml</value> 

  </list>

  </property>

  </bean>

<!—定义事务管理,使用spring来处理数据库的事务-->

- <tx:advice transaction-manager="transactionManager" id="txAdvice">

- <tx:attributes>

  <tx:method name="*" /> 

  </tx:attributes>

  </tx:advice>

- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

  <property name="sessionFactory" ref="sessionFactory" /> 

  </bean>

- <aop:config>

  <aop:pointcut expression="execution(* org.tarena.service.*.*(..))" id="txPointCut" /> 

  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" /> 

  </aop:config>

3. ssh三大框架整合之后的流程

 1. 服务器启动,启动时加载web.xml,通过spring监听器加载对应的spring配置,将配置(或配置)好的bean实例化,放入到spring容器之中。

 2. 在加载spring配置文件时,会对应的加载spring配置文件中的hibernate映射文件,

 3. 浏览器端发送请求,通过struts2的过滤器(cleanUpFilter..DispatcherFilter核心),找到对应的actionMapper,判断请求是否为action请求

 4. 如果是action请求,便会在struts2-spring容器中查找对应名字的action实例(如果不是action实例,便会直接调用jsp等其他资源作为响应)。

 5.action实例会调用Action类中对应的方法,该方法中会调用对应的service中的业务逻辑方法来响应,action会找到spring容器中对应的service实例,通过setter方法来注入到action中。

 6.同样,service实例中需要的dao也会以同样的方式注入到service中。之后完成对应的数据库操作。

 当然在中间会调用struts2的拦截器,这个拦截器调用的时机没有变化。

 可以根据自己的理解来叙述。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息