IDEA 使用Maven整合SSH

Maven作为版本控制工具,对于一个优秀的程序员来说是必不可少的。在对Maven进行学习时,网上的视频大都是以eclipse作为开发工具,而本人使用的是IDEA,因此在使用过程几乎是把MAVEN所有的坑都踩了一遍。

首先是在MAVEN的使用,IDEA本身已经自带MAVEN插件,所以在自己本地下载好MAVEN后就可以直接进行开发。首先是第一步整合Struts2.
IDEA 使用Maven整合SSH
新建MAVEN工程,这里有一点要注意:千万不要错选22-webapp,这会导致你的项目缺少一些东西。

随后填写工程名一路next直到finish就好了,当然其中有些注意点我这里就不写出来了。

随后IDEA会自动帮你完成目录的创建,同时右下角会有一个提示框,选择第二个,这样你在POM文件中添加的依赖IDEA会自动帮你完成下载。
IDEA 使用Maven整合SSH

等待IDEA的indexing工作完成后你会发现这个工作目录和自己平时使用的不太相同,没关系此时可以手动在main目录下创建resources和Java两个文件夹。其中resources用于存放工程的配置文件,Java则用来存放代码。注意这里创建文件夹后需要手动标记为资源文件夹,具体操作请自行百度。
IDEA 使用Maven整合SSH
完成上述操作后此时你的项目结构已经变成下图,这时就可以开始着手Struts2的jar包引入。
IDEA 使用Maven整合SSH
以下为Struts-core的依赖,同时还有一些辅助依赖。直接复制粘贴即可,也可以自己手动去*仓库查询。

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-core</artifactId>
  <version>2.5.16</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.20</version>
  <scope>provided</scope>
</dependency>

等待POM.xml文件中的字体不变红,完成对jar包的添加。
然后在web.xml中开始添加Struts2的核心监听器,需要注意的是,Struts2-core版本不同,核心监听器是有区别的,如果监听器配置报错,请自行百度解决。

随后的就是正常的Struts2配置过程,创建action,采用模型驱动,配置对应domain,在resources目录下配置struts.xml文件,

IDEA 使用Maven整合SSH
IDEA 使用Maven整合SSH

配置完成后对index.jsp编写,需要注意的是,如果你发现index.jsp中的el表达式没有进行智能提示,这时需要去web.xml中检查自己的版本是不是低于2.5,低于2.5会导致jsp默认无法使用el表达式,手动改成2.5以上版本即可。
IDEA 使用Maven整合SSH

最后配置好Tomcat,并点+将war添加
IDEA 使用Maven整合SSH
但是这时候还跑不起来,如果你直接启动Tomcat的话可能会报错,点击这里,开始配置struts2的配置文件

IDEA 使用Maven整合SSH
IDEA 使用Maven整合SSH
配置过程自己琢磨,完成后可以开始开始跑项目。如果过程中发现target目录下没有配置文件,可在IDEA右侧先clear,然后compile,在resource。此时发现resources文件夹中的配置文件出现在target目录中。

IDEA 使用Maven整合SSH
启动Tomcat,在弹出的页面中点击提交,发现访问成功。至此Struts2配置完成。接下来开始配置spring+Struts2
IDEA 使用Maven整合SSH

spring的加入相对比较简单,在已有的基础上,首先引入对应依赖,要是觉得自己去找太麻烦的话,可以直接复制粘贴下面依赖代码

<!-- struts2 spring  整合的核心包-->
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->
<dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-spring-plugin</artifactId>
  <version>2.5.14.1</version>
</dependency>


<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.2.6.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.12</version>
</dependency>


<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.0.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>5.0.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>5.0.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.0.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>5.0.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>5.0.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
  <groupId>cglib</groupId>
  <artifactId>cglib</artifactId>
  <version>3.2.6</version>
</dependency>
<!--spring aop包  注释方式使用事务管理 可以不引用-->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.0</version>
</dependency>

同样等IDEA全部index完后,先在web.xml配置spring核心监听器

IDEA 使用Maven整合SSH
在resources文件夹中创建spring配置文件,并命名为applicationContext.xml,文件名没有硬性要求,不过本人并没有进行测试,所以如果有什么问题请使用该名称。
IDEA 使用Maven整合SSH
相信各位已经有过SSH经验,所以在这里部分配置就不详细描述,Java目录下创建Dao,service,web,Domain,然后实现对应类。此后的配置与普通SSH的配置无异,但在这里有一点需要特别注意,也是本人踩的一个大坑:
IDEA 使用Maven整合SSH
在action中对service进行属性注入时不要在xml文件中配置,直接使用resource注解添加,本人此前在spring配置文件中使用的是property属性注入,不知道为什么在编译后它会失效所以也就没有完成注入,改为@resource注解的方式才能完成属性注入。然后同样是在modules中,先添加spring,在把对应的applicationContext.xml文件添加,apply,ok
IDEA 使用Maven整合SSH

配置完成后重启Tomcat,发现spring注入helloservice成功。

接下来是配置hibernate,此处本人并未完全弄懂,所以配置过程中可能会有些地方不对,虽然最后的确完成了功能。
首先是在右侧选中dataBase,添加Mysql,
IDEA 使用Maven整合SSH
(此处偷懒用的别人的图)
IDEA 使用Maven整合SSH
IDEA 使用Maven整合SSH

此时生成映射文件
选中左边侧边栏的Persistence菜单
IDEA 使用Maven整合SSH

IDEA 使用Maven整合SSH

如果在左边侧边栏没有该选项,请进行该配置,完成后可发现左边出现该选项

IDEA 使用Maven整合SSH
需要注意的是,生成的hbm.xml文件路径在Domain目录下,这里我手动把它放到了resources文件夹下,以便能加载到target中,可能生成的domain与自己的User不同,手动修改即可。domain具体代码如下,红线部分不影响运行。
IDEA 使用Maven整合SSH
此为hibernate.cfg.xml
IDEA 使用Maven整合SSH
此为hibernateContext.xml配置
IDEA 使用Maven整合SSH
IDEA 使用Maven整合SSH

Dao的实现类中,继承HibernateDaoSupport会报错,大概原因是与已有的sessionFactory冲突,所以退而求其次,改成如下:
IDEA 使用Maven整合SSH
重启Tomcat,项目运行成功,数据库也添加了对应记录

IDEA 使用Maven整合SSH
至此,IDEA下Maven的SSH整合完成!
最后附上完整的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Maven</groupId>
  <artifactId>Maven-Struts2</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>Maven-Struts2 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.16</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.20</version>
      <scope>provided</scope>
    </dependency>

    <!-- struts2 spring  整合的核心包-->
    <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-spring-plugin</artifactId>
      <version>2.5.14.1</version>
    </dependency>


    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.2.6.Final</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.12</version>
    </dependency>


    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/cglib/cglib -->
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.2.6</version>
    </dependency>
    <!--spring aop包  注释方式使用事务管理 可以不引用-->
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.9.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.0</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>



  </dependencies>

  <build>
    <finalName>Maven-Struts2</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>