二、Maven分模块开发及私服介绍
2017-07-31 本文已影响94人
明天你好向前奔跑
Maven分模块开发&私服
一、Maven的项目开发(整合SSH框架练习)
1. 新建Maven Project项目
2. 在pom.xml中添加依赖,导入三大框架所有所需的jar包
这里,按Spring >>> Hibernate >>> Struts2 的顺序来添加依赖较好
3. 从Hibernate --- Action 完成开发
1. Hibernate的ORM 数据库表,持久化类,映射文件。 以cst_customer,Customer,Customer.hbm.xml为例
2. Spring 配置文件 , 完全整合 Hibernate
3. 动作类Action类,编写struts.xml,完全整合到applicationContext.xml中
4. web.xml 完成 Spring核心监听器,延迟加载机制及Struts2的前端控制器的配置
1.1 Maven - pom.xml
导入三大框架所需jar包
有两种方式,一个是手动一个一个的添加依赖,一个是直接复制pom.xml中的配置:
- 手动添加
从Spring - Hibernate - Struts2 - 其它 的顺序添加依赖。
Spring:
spring-context : (导入的依赖jar包有:)
context,core,bean,expression . spring-aop,aopaliance,logging 4+3个包。
spring-aspects :
aspectjweaver,spring-aspectj 2个包。
spring-orm :
orm,jdbc,tx 3个包。
spring-test :
test 1个包
spring-web :
web 1个包
共14个包。
-------------------------------------------------------------------------------
Hibernate:
hibernate-core:(新添加的jar包:)
antlr,dom4j,geronimo-jta,hibernate-commons-annotations,hibernate-core,hibernate-jpa,
jandex,javassist,jboss,xml-api 10个包
![img22.png](http:https://img.haomeiwen.com/i5303154/c098a56b87173cd8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
mysql :
mysql-connector 1个包
c3p0 :
c3p0 1个包
共12个包。
-------------------------------------------------------------------------------
Struts2 :36
strut2-core :
struts-core,xwork-core,commons-lang,ognl,asm,commons-fileupload,commons-io,freemark,javasist 3.11等
struts2-spring-plugin :
-------------------------------------------------------------------------------
其他:
servlet-api:
jsp-api:
junit:
slf4j-log4j12:
jstl
-------------------------------------------------------------------------------
<build>
<plugins>
<!-- 设置编译版本为1.7 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- maven内置 的tomcat7插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 可以灵活配置工程路径 -->
<path>/ssh</path>
<!-- 可以灵活配置端口号 -->
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
2. 直接上网拷贝pom.xml中的内容【使用这一种】
开发效率高,不容易出错。使用版本锁定等方式,有效解决版本冲突的问题:
<!-- 属性 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<hibernate.version>5.0.7.Final</hibernate.version>
<struts.version>2.3.24</struts.version>
</properties>
<!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 依赖管理 -->
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- 导入 struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
</dependency>
<!-- servlet jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 设置编译版本为1.7 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- maven内置 的tomcat6插件 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<!-- 可以灵活配置工程路径 -->
<path>/ssh</path>
<!-- 可以灵活配置端口号 -->
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
1.2 三大框架配置文件的整合
与之前的整合一致,区别是:
1. Customer.hbm.xml的hibernate的映射文件需要放到src/main/resources目录下,与src/main/java目录下实体类Customer的路径必须一致。
其它配置文件如applicationContext.xml,struts.xml,db.properties放在src/main/resouces根目录下
2. Spring的核心配置文件整合hibernate
与之前的区别是:
sessionFactory的bean中,加载映射文件使用 mappingLocations。
这样可以使用classpath:classpath:com/itdream/entity/*.hbm.xml
=============================================================================
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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 外部文件引入 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="${jdbc.dataSource}">
<property name="driverClass" value="${jdbc.driverClass}"/>
<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.hibernate5.LocalSessionFactoryBean">
<!-- 1. 注入数据源,连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 2. hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
<!-- 3. 加载hibernate的映射文件 -->
<property name="mappingLocations">
<list>
<value>classpath:com/itdream/entity/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- Spring事务管理 -->
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*"/>
<tx:method name="delete*"/>
<tx:method name="update*"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.itdream.service.impl.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!-- Spring装配bean -->
<!-- customer模块 -->
<bean id="customerDAO" class="com.itdream.dao.impl.CustomerDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="customerService" class="com.itdream.service.impl.CustomerServiceImpl">
<property name="customerDAO" ref="customerDAO"/>
</bean>
<bean id="customerAction" class="com.itdream.web.action.CustomerAction">
<property name="customerService" ref="customerService"/>
</bean>
</beans>
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
<!-- 自动加载配置文件 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 简单样式 -->
<constant name="struts.ui.theme" value="simple"/>
<!-- customer模块 -->
<package name="customer" namespace="/" extends="struts-default">
<action name="customer_*" class="customerAction" method="{1}">
<result name="findAll_success">/list.jsp</result>
</action>
</package>
</struts>
web.xml:
<!-- 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>
<!-- Spring延迟加载机制 -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Struts的前端控制器 -->
<filter>
<filter-name>Struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
db.properties:
jdbc.dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/maven
jdbc.username=root
jdbc.password=root
log4j.properties:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:\\mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout,file
具体实现省略。
二、 Maven实现模块开发
2.1 父工程maven-parent
img23.png新建Maven工程(Project),打包方式选择pom.
在父工程中添加依赖:
<!-- 属性 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<hibernate.version>5.0.7.Final</hibernate.version>
<struts.version>2.3.24</struts.version>
</properties>
<!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 依赖管理 -->
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- 导入 struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
</dependency>
<!-- servlet jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 设置编译版本为1.7 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- maven内置 的tomcat7插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/maven</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
发布到本地仓库(install)
2.2 创建子模块maven-dao
在父工程上new一个Maven模块(module),打包方式选择jar.以供service依赖。
以Customer查询所有客户findAll为例:实现类具体代码省略,主要展示配置文件的思路。
1. 如果需要测试,在pom.xml中添加依赖 Junit.jar
2. 实体类与映射文件的编写:略, 与以往不同的是注意它们放置在Maven工程中的位置,且它们所在的包结果要一致。
3. applicationContext-dao.xml(整合hibernate) :
img24.png
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 引入外部配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 连接池 -->
<bean id="dataSource" class="${jdbc.dataSource}">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 整合Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 1. hibernate的数据源,连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 2. hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
<!-- 3. hibernate的映射文件 -->
<property name="mappingLocations">
<list>
<value>classpath:com/itdream/maven/dao/Customer.hbm.xml</value>
</list>
</property>
</bean>
<!-- 向dao中注入Session -->
<bean id="customerDao" class="com.itdream.maven.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
这里与以前的配置不同的是,加载hibernate映射文件改为了mappingLocations,以前是mappingResources,改为Locations后可以使用通配符。
db.properties:
jdbc.dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/maven
jdbc.username=root
jdbc.password=root
-----------------------------------------------------------------------------
完成dao层的代码实现。
4. 发布到本地仓库,以供后面service层使用(企业中发布到私服供service团队使用)
2.3 子模块maven-service
1. 新建子模块maven-service,继承父工程,打包方式选择jar,以供web层依赖使用。
2. pom.xml中添加maven-dao依赖.如果需要测试,添加依赖Junit
3. 编写applicationContext-service.xml,完成Spring的事务管理及service的bean注册。
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!-- Spring管理事务 -->
<!-- Spring的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Spring事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" />
<tx:method name="delete*" />
<tx:method name="update*" />
<tx:method name="find*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面配置 -->
<aop:config>
<aop:pointcut expression="execution(* com.itdream.maven.service.impl.*ServiceImpl.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!-- Spring装配bean -->
<bean id="customerService" class="com.itdream.maven.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao" />
</bean>
</beans>
完成service层的代码实现。
4. 发布到本地仓库。
2.4 子模块maven-action
1. 新建子模块maven-action,这里打包方式选择 war .
2. 添加依赖maven-service,如果需要测试,添加Junit依赖。
3. 编写配置文件applicationContext-action.xml及struts.xml
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 使用简单样式 -->
<constant name="struts.ui.theme" value="simple" />
<!-- 自定义包,配置Action -->
<package name="customer" namespace="/" extends="struts-default">
<action name="customer_*" class="customerAction" method="{1}">
<result name="findAll_success">/list.jsp</result>
</action>
</package>
</struts>
applicationContext-action.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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Customer模块 -->
<bean id="customerAction" class="com.itdream.maven.web.action.CustomerAction">
<property name="customerService" ref="customerService"/>
</bean>
</beans>
完成页面及Action代码实现。
4. 修改web.xml。
1. 在运行web程序时,Spring监听器扫描web依赖的jar包中所有的applicationContext-*.xml.
2. Spring延迟加载机制
3. Struts的前端控制器
<!-- 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>
<!-- Spring的延迟加载 -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Struts的前端控制器 -->
<filter>
<filter-name>Struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
分模块开发完成。
三、 私服的安装,上传jar(子模块)及下载jar
3.1 nexus的安装
http://www.sonatype.org/nexus/archived/
下载后解压即可。
进入到解压后的nexus的bin目录:
D:\Develop\maven\nexus\nexus-2.12.0-01\bin
dos窗口执行nexus install完成安装。 如要卸载,nexus.bat uninstall.
开启nexus服务:
nexus.bat start
3.2 上传子模块到私服
1. 首先必须有安装Maven,在Maven的配置文件setting中添加访问私服的账号密码。
添加到settings.xml中
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
2. 在要上传的模块的pom.xml中配置上传的私服的路径地址。
添加到pom.xml中
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
修改的就是上传到私服的路径为公司的ip地址.
3. 上传到私服。
右键Run as 选择Maven build...,输入deploy上传。
3.3 下载子模块或jar包到本地仓库
需要连接私服。在配置了账号密码的基础上,配置要从哪个私服仓库下载。
到了不同公司要修改的是仓库地址的ip为公司私服的ip地址。
添加到settings.xml中
<profile>
<!--profile的id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库id,repositories可以配置多个仓库,保证id不重复-->
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载releases构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
2. 激活定义的仓库。
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>