最近很长一段时间都在用Spring+MyBatis开发,对于SSH方面的知识都有了一点生疏,正好乘此空闲搭建一个SSH的框架聊以回顾下旧的只是。
对此需要说的是,......下面是完整的搭建流程。
首先上图
创建一个maven项目
2. 配置pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.liwbn</groupId> <artifactId>ssh-1</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssh-1 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- struts2核心包 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.1.2</version> </dependency> <!-- struts2与spring整合的包 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.1.2</version> </dependency> <!-- 在 Struts2中要使用 Ajax获得Json数据。要使用Ajax必须引用此Jar --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.3.1.2</version> </dependency> <!-- Hibernate核心包 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.10.Final</version> </dependency> <!-- spring3可选的依赖注入,不可缺少 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.3</version> </dependency> <!-- 扩展Java类与实现Java接口 --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <!-- Spring包 --> <!-- Spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> <type>jar</type> </dependency> <!-- Spring在WEB上的MVC框架上加上这个包 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.3.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> --> <!-- 日志包 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> <scope>compile</scope> </dependency> <!-- log4j日志包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> <scope>compile</scope> </dependency> <!-- jsp接口 --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <!-- 连接池 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- servlet接口 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- Mysql数据库JDBC连接包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>ssh-1</finalName> </build> </project>
3. 配置spring需要的配置文件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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 定义受环境影响易变的变量 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <!-- 数据库配置 --> <value>classpath*:/db/db.properties</value> </list> </property> </bean> <!-- 用Bean定义c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <!-- 通过classpath的方式指向了映射文件所在的目录 --> <property name="mappingDirectoryLocations"> <value>classpath*:/hibernate/</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- 数据库操作基类,所有的类要注入基类 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 切入点表达式 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 切面 --> <aop:config> <aop:pointcut id="interceptorPointCuts" expression="execution(* us.xuhang.project.dao.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" /> </aop:config> </beans>
4. 配置struts2需要的配置文件struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.objectFactory" value="spring" /> <!-- 开发状态 --> <constant name="struts.devMode" value="true" /> <constant name="struts.ui.theme" value="simple" /> <!-- 国际化资源重新加载 --> <constant name="struts.i18n.reload" value="false" /> <!-- 配置文件重新加载 --> <constant name="struts.configuration.xml.reload" value="false" /> <!-- convention类从新加载 <constant name="struts.convention.classes.reload" value="true" /> --> <!-- 地区 --> <constant name="struts.locale" value="zh_CN" /> <!-- 国际化编码 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 扩展 --> <constant name="struts.action.extension" value="action,do" /> <!-- 启用动态方法调用,要实现零配置,就是需要动态方法调用。--> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- 上传限制 <constant name="struts.multipart.maxSize" value="10000000" />--> <!-- struts2拦截器日志记录 <package name="logger-package" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.work.log.Interceptor.LoggerInterceptor"> </interceptor> <interceptor name="dynameicData" class="com.work.unicom.dynameicds.DynamicDataSourceInterceptor"></interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="dynameicData"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"> </default-interceptor-ref> --><!-- 定义全局处理结果 <global-results> <result name="login" type="redirect">/login.jsp</result> <result name="exception" type="redirect">/common/500.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="exception" exception="java.lang.Exception" /> </global-exception-mappings> </package> --> <include file="struts-default.xml"/> <include file="cn/sss/ssh/struts/struts-*.xml"></include> </struts>
5. 配置Hibernate对象映射文件FrameRole.hbm.xml
<?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> <class name="cn.liwbn.ssh.entity.FrameRole"> <id name="id" length="11"> <generator class="assigned"></generator> </id> <property name="roleName" length="50"></property> <property name="roleDescription" length="200"></property> <property name="peId" type="int"></property> <property name="roleType" type="int"></property> <property name="type" type="int"></property> </class> </hibernate-mapping>
6. 配置applicationContext-bean.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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd" default-lazy-init="false"> <description>公共配置 </description> <bean id="indexAction" class="cn.sss.ssh.action.IndexAction" scope="prototype"/> </beans>
7. 配置struts配置文件struts-bean.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <!-- analysisAction --> <action name="index" class="indexAction"> <result>index.jsp</result> </action> </package> </struts>
8. 最后配置web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 定位applicationContext.xml的物理位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <!-- 过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 监听器Spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
至此,整个配置流程结束。