运用三大框架,使用MyEclipse帮我们导入jar包会发现有些包冲突了或者有些包是多余的,让我们来手动配置三大框架吧!
1、下载三大框架的必备的jar包,下载地址:http://download.csdn.net/detail/harderxin/4420066
2、新建一个web工程SSHTest,将jar包导入到工程中,注意导入进去后在WEB-INFO目录的lib文件夹中要有相应的jar包,jar包如下以及我的目录结构
3、打开web.xml,在里面添加如下代码,配置如下信息是在web项目启动的时候可以加载那些spring和struts配置文件中的信息:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- 默认加载: /WEB-INF/applicationContext.xml -->
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 加载Spring配置文件:applicationContext-*.xml -->
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 配置Struts2过滤器,表示启动web项目的时候进行加载 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4、打开Struts.xml,配置Struts一些常量,并将struts交给Spring容器来管理
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 将struts2交给spring管理-->
<constant name="struts.configuration.xml.reload" value="true"/>
<constant name="struts.action.extension" value="action,do,webwork" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.multipart.maxSize" value="100971520"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.objectFactory.spring.autoWire" value="name"></constant>
<constant name="struts.objectFactory" value="spring"></constant>
<constant name="struts.custom.i18n.resources" value="messages"></constant>
<package name="test" namespace="" extends="struts-default">
<action name="testAction" class="testAction">
<result name="test">index.jsp</result>
</action>
</package>
</struts>
5、打开Spring,配置连接池和事务
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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">
<context:annotation-config/>
<context:component-scan base-package="com.ssh" />
<!--
如果不使用配置文件,可以使用如下方式配置数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/lundui?useUnicode=true&characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
-->
<!-- 读取配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<!-- 数据库连接池 -->
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc_driver}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 单个实体类配置
<property name="annotatedClasses">
<list>
<value>com.ssh.common.model.User</value>
<value>com.ssh.common.model.Log</value>
</list>
</property>
-->
<property name="packagesToScan">
<list>
<value>com.ssh.common.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="txManager" >
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="saveOrUpdate*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置哪些类哪些方法使用事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.repair.*.service.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.repair.*.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
<!-- JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="testAction" class="com.ssh.test.action.TestAction" scope="prototype"></bean>
<bean id="testService" class="com.ssh.test.service.impl.TestServiceImpl"></bean>
<bean id="testDao" class="com.ssh.test.dao.impl.TestDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
jdbc.propertie数据库配置文件信息:
package com.ssh.common.model;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -2699030436204817211L;
private Long id;
//用户名称
private String username;
//用户密码
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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;
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ssh.common.model">
<class name="User" table="tb_user">
<id name="id">
<generator class="native" />
</id>
<property name="username" column="username"/>
<property name="password" column="password"/>
</class>
</hibernate-mapping>
7、在上面struts和spring中,我们给相应的实体类进行了注册,下面来编写我们的类,在这里,我使用了三层架构,大家可以根据实情进行相应的配置和处理,里面的方法可以参考我配置好的项目包
8、将项目部署到Tomcat服务器,如果启动过程中没有报错,说明ssh三大框架配置OK!大家试一试吧
我自己配置好了一个三大框架的demo项目,大家可以在我的资源库中进行免费下载学习:
注意:使用数据库:mysql,在本地新建一个名称为lundui的数据库,然后在里面新建一张tb_user的表,里面包含信息:id、username、password,也可以进行jdbc自己配置