码迷,mamicode.com
首页 > 其他好文 > 详细

ssh整合

时间:2016-12-05 14:38:55      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:under   bean   内容   namespace   lock   utf-8   org   password   pos   

 struts2+spring整合:

   1、第一种方式:配置文件

  <!-- 核心控制器 -->

  <filter>

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

 </filter>

  <filter-mapping>

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

     <url-pattern>/*</url-pattern>

  </filter-mapping>

  <!-- 加载spring的配置文件 -->

   <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:applicationContext.xml</param-value>

  </context-param>

3)创建struts.xml配置文件及applicationContext.xml配置文件

   Struts.xml内容如下:

<struts>

       <!-- ture:表示修改了这个配置文件 就不用重新启动tomcat -->

    <constant name="struts.configuration.xml.reload" value="true"></constant>

     <package name="mcPack" extends="struts-default" namespace="/stu" strict-method-invocation="false">

         <action name="stu_*" class="cn.hd.action.StudentAction" method="{1}">

            <result name="success" >/index.jsp</result>

             <result name="input" >/stu.jsp</result>   

         </action>

          </package>

      </struts>  

 applicationContext.xml配置文件内容如下:

 

<?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:p="http://www.springframework.org/schema/p"

 

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

 

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

 

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

 

<!-- 获取 数据源 -->

 

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

 

destroy-method="close">

<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>

<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"></property>

<property name="user" value="j1607"></property>

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

</bean>

<bean id="template" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">

   <constructor-arg>

<ref bean="dataSource" />

</constructor-arg>

</bean>

<bean id="stuAction" class="cn.hd.action.StudentAction">

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

</bean>

<bean id="stuService" class="cn.hd.service.impl.StudentServiceImpl">

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

</bean>

<bean id="stuDao" class="cn.hd.dao.impl.StudentDaoImpl">

    <property name="template" ref="namedTemplate"></property>

</bean>

</beans>

创建dao层,service层,action层:这几层跟以前做学生模块的代码是一样的,唯独不同的是调用各层时,不用实例化,而是用spring的设值注入;

 

2、第二种方式:注解方式

 

1)在applicationContext.xml配置文件中加入扫描,记得加上context命名空间:

<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:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <!-- 扫描 -->

    <context:component-scan base-package="cn.hd.action,cn.hd.dao.impl"></context:component-scan>

    <context:component-scan base-package="cn.hd.service.impl"></context:component-scan>

<!-- 获取 数据源 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>

<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"></property>

<property name="user" value="j1607"></property>

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

</bean>

<bean id="template" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">

   <constructor-arg>

<ref bean="dataSource" />

</constructor-arg>

</bean>

<!--

<bean id="stuAction" class="cn.hd.action.StudentAction">

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

</bean>

<bean id="stuService" class="cn.hd.service.impl.StudentServiceImpl">

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

</bean>

 

<bean id="stuDao" class="cn.hd.dao.impl.StudentDaoImpl">

    <property name="template" ref="namedTemplate"></property>

</bean>

 -->

 

</beans>

 

 

 

注:

 

@Component:组件 容器 就是由spring来管理(它来帮你实例化)

 

@Repository:与@Component功能一样

 

@Service:与@Component功能一样

 

@Controller:与@Component功能一样

 

 

ssh整合

标签:under   bean   内容   namespace   lock   utf-8   org   password   pos   

原文地址:http://www.cnblogs.com/joyous-day/p/6133565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!