一、Maven与Struts2整合
1、根据Maven(一)创建出原始的Maven项目,未进行任何操作
2、在src/main/resources下面添加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.i18n.encoding" value="UTF-8"></constant>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 主题 -->
<constant name="struts.ui.theme" value="simple"></constant>
<!-- 扩展名 -->
<constant name="struts.action.extension" value="action"></constant>
<!-- 通用package -->
<package name="customer" namespace="/" extends="struts-default">
<action name="" class="" method="">
<result name=""></result>
</action>
</package>
</struts>
3、在项目src-main-webapp下面添加WEB-INF文件夹,并导入web.xml文件,在其中配置struts2核心过滤器
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- struts2核心过滤器 -->
<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.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
4、添加依赖,添加servlet-api.jar和jsp-api.jar,注意选择scope为provided
参考源码maven_02第一个版本(maven与struts2整合):https://github.com/AmazeLee/Maven.git
二、整合ssh项目
1、整合struts和maven-同上
依赖传递
只添加了一个struts2-core依赖,发现项目中出现了很多jar,这种情况 叫 依赖传递
2、导入spring依赖
(1)导入依赖
(2)导入之后会存在以来版本冲突问题
(3)依赖版本冲突的解决
调节原则包含AB
A 路径近者优先原则(自己添加jar包)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
B 第一声明优先原则(放前面)
<!-- spring-beans.4.2.4 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- spring-beans.3.0.5 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24</version>
</dependency>
C 排除原则
D 版本锁定原则
3、构建项目
(1)导入准备好的pom.xml
(2)创建数据库,执行准备好的sql脚本,建实体类并写实体类映射文件
(3)导入Hibernate核心配置文件,spring核心配置文件,并进行相应修改
(4)完成dao代码,接口以及实体类,在application.xml中进行配置
(5)daojunit测试
在需要进行junit类上ctrl+n
CustomerDaoImplTest.java
package com.sh.ssh.dao.impl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sh.ssh.dao.CustomerDao;
import com.sh.ssh.entity.Customer;
public class CustomerDaoImplTest {
@Test
public void test() {
ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
CustomerDao customerDao = (CustomerDao) app.getBean("customerDao");
Customer customer = customerDao.findById(23L);
System.out.println(customer.getCustName());
}
}
(6)完成action代码,在application.xml中进行action的配置,在struts.xml中进行配置
(7)在web.xml中配置让spring随web启动而创建的监听器,配置spring配置文件位置参数
源码参考maven_02第二个版本(maven整合ssh完毕):https://github.com/AmazeLee/Maven.git
三、分模块开发
1、创建父工程,把需要的jar都放到父工程的pom.xml中
父工程目录:
2、创建子工程
(1)dao模块,创建实体类和实体类映射文件,创建dao层接口和实现类,并在application-dao.xml进行配置,并进行junit测试
(2)service模块,创建service层接口和实现类,添加dao层依赖,在applicationContext-service.xml中进行配置,进行junit测试
(3)web模块,选择war,创建完之后在webapp下新建WEB-INF导入web.xml,创建action,在applicationContext-action.xml、struts.xml中进行配置,修改web.xml中的spring配置文件位置参数<param-value>
注意:很容易出现版本不兼容报错,在运行不出时候,调不出可以尝试换换jdk或者tomcat版本
源码参考maven-parent分支:https://github.com/AmazeLee/Maven.git
四、私服 nexus
1、下载安装nexus
(1)下载地址:http://www.sonatype.org/nexus/,下载开源版本
NEXUS OSS [OSS = Open Source Software,开源软件——免费]NEXUS PROFESSIONAL -FREE TRIAL [专业版本——收费]。
(2)解压到一个文件夹里面,cmd进入命令行界面进行安装,开启服务
启动失败的解决方法:
(3)浏览器访问
(4)仓库类型
Virtual 虚拟仓库
Proxy 代理仓库
Hosted 宿主仓库 本地仓库
Group 组
需求:
把dao放到私服上,然后service从私服上下载
Proxy 代理仓库
Hosted 宿主仓库 本地仓库
Group 组
需求:
把dao放到私服上,然后service从私服上下载
2、浅尝私服——需求 :将ssh_dao的这个工程打成jar包,并放入到私服上去.
(1)上传dao
第一步: 需要在客户端即部署dao工程的电脑上配置 maven环境,并修改 settings.xml 文件,配置连接私服的用户和密码 。
此用户名和密码用于私服校验,因为私服需要知道上传都 的账号和密码 是否和私服中的账号和密码 一致。
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
第二步: 配置项目pom.xml
配置私服仓库的地址,本公司的自己的jar包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为release则上传到私服的release仓库,如果版本为snapshot则上传到私服的snapshot仓库<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>
第三步 run as-maven build-deploy进行发布
(2)下载dao
第一步 修改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>
进行激活
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>