ssh整合
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>
2)hibernate中的配置
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration><session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property><property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
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功能一样