[转]使用maven配置的ssh框架
http://wenhao880204.iteye.com/blog/1743969
学习maven中,使用maven创建搭建了一个ssh框架,使用maven对ssh框架并没有什么影响,使用maven主要是用来管理jar包及它的各种依赖关系,写个ssh框架的程序员们应该都有体会,ssh框架的jar包非常多,不知道就出现了版本冲突什么的。使用maven就可以轻松的管理jar包及其依赖了,如果在eclipse中安装maven插件,就可以很容易看到jar包的依赖关系了。这里留下我搭建好ssh框架的pom.xml文件供以后使用参考。
由于没有安装数据库,使用了apache的一个非常轻量级的derby数据库,添加了dwr的支持和对密码加密的commons-codec,添加了junit4进行单元调试。pox.xml:
1 <project 2 xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <groupId>zwh.struts.maven</groupId> 7 <artifactId>Struts_Spring_Maven</artifactId> 8 <packaging>war</packaging> 9 <version>0.0.1-SNAPSHOT</version> 10 <name>Struts_Spring_Maven Maven Webapp</name> 11 <url>http://maven.apache.org</url> 12 <dependencies> 13 <!-- junit4用来进行单元测试 --> 14 <dependency> 15 <groupId>junit</groupId> 16 <artifactId>junit</artifactId> 17 <version>4.11</version> 18 <scope>test</scope> 19 </dependency> 20 <!-- struts2的核心包,其依赖包会自动下载 --> 21 <dependency> 22 <groupId>org.apache.struts</groupId> 23 <artifactId>struts2-core</artifactId> 24 <version>2.3.7</version> 25 </dependency> 26 <!-- struts2与spring的整合插件 --> 27 <dependency> 28 <groupId>org.apache.struts</groupId> 29 <artifactId>struts2-spring-plugin</artifactId> 30 <version>2.3.7</version> 31 </dependency> 32 <!-- spring的核心包,其他依赖包会自动下载 --> 33 <dependency> 34 <groupId>org.springframework</groupId> 35 <artifactId>spring-core</artifactId> 36 <version>3.0.5.RELEASE</version> 37 </dependency> 38 <!-- 使用spring的aop,aop要依赖aspectjweaver --> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-aop</artifactId> 42 <version>3.0.5.RELEASE</version> 43 </dependency> 44 <dependency> 45 <groupId>org.aspectj</groupId> 46 <artifactId>aspectjweaver</artifactId> 47 <version>1.7.1</version> 48 </dependency> 49 <!-- log4j日志记录 --> 50 <dependency> 51 <groupId>log4j</groupId> 52 <artifactId>log4j</artifactId> 53 <version>1.2.14</version> 54 </dependency> 55 <!-- 使用这里的工具类对字符串进行MD5加密 --> 56 <dependency> 57 <groupId>commons-codec</groupId> 58 <artifactId>commons-codec</artifactId> 59 <version>1.7</version> 60 </dependency> 61 <!-- 62 对action测试时添加这些依赖,但无法处理session的模拟,参考文章如下: 63 http://struts.apache.org/2.2.1.1/docs/struts-2-junit-plugin-tutorial.html 64 http://struts.apache.org/2.2.1.1/docs/junit-plugin.html 65 http://blog.sina.com.cn/s/blog_4758a28b0100l2tp.html 66 --> 67 <!-- 68 <dependency> 69 <groupId>org.apache.struts</groupId> 70 <artifactId>struts2-junit-plugin</artifactId> 71 <version>2.3.7</version> 72 <scope>test</scope> 73 </dependency> 74 <dependency> 75 <groupId>javax.servlet</groupId> 76 <artifactId>jsp-api</artifactId> 77 <version>2.0</version> 78 <scope>test</scope> 79 </dependency> 80 <dependency> 81 <groupId>javax.servlet</groupId> 82 <artifactId>servlet-api</artifactId> 83 <version>2.4</version> 84 </dependency> 85 --> 86 <!-- dwr的jar包 --> 87 <dependency> 88 <groupId>org.directwebremoting</groupId> 89 <artifactId>dwr</artifactId> 90 <version>2.0.10</version> 91 </dependency> 92 <!-- derby数据库,这里还包含了它的jdbc驱动 --> 93 <dependency> 94 <groupId>org.apache.derby</groupId> 95 <artifactId>derby</artifactId> 96 <version>10.9.1.0</version> 97 </dependency> 98 <!-- hibernate的核心包,使用hibernate4时报错就改用了hibernate3 --> 99 <dependency> 100 <groupId>org.hibernate</groupId> 101 <artifactId>hibernate-core</artifactId> 102 <version>3.3.1.GA</version> 103 </dependency> 104 <!-- spring整合hibernate需要添加上spring的orm的jar包, 由于spring-orm是 105 依赖于spring-jdbc的, 所以这里即使不配spring-jdbc,maven也会帮我们 106 下载下来这些依赖 107 --> 108 <dependency> 109 <groupId>org.springframework</groupId> 110 <artifactId>spring-jdbc</artifactId> 111 <version>3.0.5.RELEASE</version> 112 </dependency> 113 <dependency> 114 <groupId>org.springframework</groupId> 115 <artifactId>spring-orm</artifactId> 116 <version>3.0.5.RELEASE</version> 117 </dependency> 118 <!-- hibernate-core依赖于slf4j-api,而slf4j-api需要一个实现类,这个 119 实现类jar可以是slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, 120 slf4j-jdk14.jar ,logback-classic.jar其中之一 --> 121 <dependency> 122 <groupId>org.slf4j</groupId> 123 <artifactId>slf4j-log4j12</artifactId> 124 <version>1.5.2</version> 125 </dependency> 126 </dependencies> 127 <build> 128 <finalName>Struts_Spring_Maven</finalName> 129 </build> 130 </project>
spring配置文件applictionContext.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:p="http://www.springframework.org/schema/p" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> 11 12 <!-- derby创建用户名和密码参考:http://www.joyzhong.com/archives/643 --> 13 <bean 14 id="dataSource" 15 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 16 <property name="driverClassName"> 17 <value>org.apache.derby.jdbc.EmbeddedDriver</value> 18 </property> 19 <property name="url"> 20 <value>jdbc:derby:f:/zwh/mydb2</value> 21 </property> 22 <property name="username"> 23 <value>root</value> 24 </property> 25 <property name="password"> 26 <value>root</value> 27 </property> 28 </bean> 29 <bean 30 id="sessionFactory" 31 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 32 <property name="dataSource"> 33 <ref bean="dataSource"/> 34 </property> 35 <property name="hibernateProperties"> 36 <props> 37 <prop key="hibernate.dialect"> org.hibernate.dialect.DerbyDialect</prop> 38 <prop key="hibernate.show_sql">true</prop> 39 <prop key="hibernate.format_sql">true</prop> 40 </props> 41 </property> 42 <property name="mappingResources"> 43 <list> 44 <value>zwh/struts/maven/po/User.hbm.xml</value> 45 </list> 46 </property> 47 </bean> 48 <bean 49 id="userDao" 50 class="zwh.struts.maven.dao.impl.UserDaoDerbyImpl"> 51 <property name="sessionFactory"> 52 <ref bean="sessionFactory"/> 53 </property> 54 </bean> 55 <bean 56 id="userService" 57 class="zwh.struts.maven.service.impl.UserServiceImpl"> 58 <property name="userDao"> 59 <ref bean="userDao"/> 60 </property> 61 </bean> 62 <bean 63 id="loginAction" 64 class="zwh.struts.maven.action.LoginAction" 65 scope="prototype"> 66 <property name="userService"> 67 <ref bean="userService"/> 68 </property> 69 </bean> 70 <bean 71 id="serviceAdvice" 72 class="zwh.struts.maven.aop.ServiceAdvice"></bean> 73 <bean 74 id="daoAdvice" 75 class="zwh.struts.maven.aop.DaoAdvice"></bean> 76 <aop:config> 77 <aop:aspect 78 id="serviceAdviceAspect" 79 ref="serviceAdvice"> 80 <!-- around和before之间,around和after之间,谁配置在前面谁首先被执行 --> 81 <aop:before 82 pointcut="execution(* zwh.struts.maven.service.impl.*.*(..))" 83 method="before"/> 84 <aop:after-returning 85 pointcut="execution(* zwh.struts.maven.service.impl.*.*(..))" 86 method="afterReturning" 87 returning="rvt"/> 88 <aop:after 89 pointcut="execution(* zwh.struts.maven.service.impl.*.*(..))" 90 method="after"/> 91 <aop:around 92 pointcut="execution(* zwh.struts.maven.service.impl.*.*(..))" 93 method="around"/> 94 </aop:aspect> 95 <aop:aspect 96 id="daoAdviceAspect" 97 ref="daoAdvice"> 98 <aop:after-returning 99 pointcut="execution(* zwh.struts.maven.dao.impl.*.*(..))" 100 method="afterReturning" 101 returning="rvt"/> 102 </aop:aspect> 103 </aop:config> 104 105 </beans>
struts.xml文件:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" 4 "http://struts.apache.org/dtds/struts-2.1.7.dtd"> 5 6 <struts> 7 <!-- 8 --> 9 <constant name="struts.action.excludePattern" value="/dwr/.*,/dwr/test/.*"></constant> 10 <constant name="struts.custom.i18n.resources" value="mess"></constant> 11 <constant name="struts.i18n.encoding" value="GBK"></constant> 12 <constant name="struts.objectFactory" value="spring"></constant> 13 14 <package name="test" extends="struts-default"> 15 <global-results> 16 <result name="error" type="redirect">/failure.jsp</result> 17 <result name="failure" type="redirect">/failure.jsp</result> 18 </global-results> 19 20 <action name="login" class="loginAction"> 21 <result name="success">/welcome.jsp</result> 22 <result name="error" type="redirect">/failure.jsp</result> 23 </action> 24 25 </package> 26 </struts>
dwr.xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> 3 <dwr> 4 <allow> 5 <!-- --> 6 <create creator="spring" javascript="CheckInServer"> 7 <param name="beanName" value="userService"></param> 8 <include method="check"/> 9 </create> 10 </allow> 11 12 </dwr>
web.xml文件: