Creazione di una semplice web application utilizzando maven, jpa2, spring, struts2, mysql

In questo tutorial verrà spiegato come sviluppare una semplice web application utilizzando tecnologie come maven, jpa2, struts2, spring e mysql.

Ambiente di lavoro:

  • MySQL Server
  • Eclipse + da “Eclipse Market Place”  “Maven Integration for Eclipse”
  • JDK 1.7

Creazione tabella mysql:

Prima di iniziare tutto il lavoro creiamo una tabella che chiameremo user

CREATE  TABLE IF NOT EXISTS `user` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(255) NOT NULL ,
  `password` VARCHAR(255) ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;
									

 

Creazione di un nuovo progetto Maven su Eclipse:

– creiamo un nuovo progetto “maven” su eclipse

– impostiamo  maven-archetypes-webapp

– impostiamo grupId e artifactId come in figura



Struttura progetto e configurazione pom.xml

Una volta generato il progetto maven, Eclipse ci propone tutto il progetto come in figura

 Il prossimo passo sarà quello di configurare il file pom.xml in modo da personalizzare il building e includere le tecnologie che ci interessano.

In particolare includiamo le dipendenze per: struts2, spring, mysql connector, servlet e jsp, hibernate, log4j e junit.

Dopo di che impostiamo anche il <build> in modo da utilizzare alcuni plugin tra cui il web server jetty.

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>it.sample</groupId>
  <artifactId>webapp</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <struts2.version>2.1.8</struts2.version>
    <hibernate.em.version>3.4.0.GA</hibernate.em.version>
    <spring.version>2.5.6</spring.version>
        <spring.jpa.version>2.0.8</spring.jpa.version>
        <mysql.connector.version>5.1.13</mysql.connector.version>
    <junit.version>4.4</junit.version>
        <log4j.version>1.2.16</log4j.version>
  </properties>


  <dependencies>
        <!--  Struts 2 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-junit-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>

        <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</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-jpa</artifactId>
            <version>${spring.jpa.version}</version>
        </dependency>
        
        <!-- MySQL JDBC Connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.connector.version}</version>
    </dependency>
        
        <!-- Servlet & Jsp -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        
        <!-- Hibernate dependencies -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.em.version}</version>
        </dependency>
 
        <!-- Other dependencies -->

        <!-- slf4j required for hibernate-annotations 3.4.0 -->    
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.5.6</version>
      </dependency>
      <!-- concrete Log4J Implementation for SLF4J API-->
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.5.6</version>
      </dependency>
        
      <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>${log4j.version}</version>
          <scope>runtime</scope>
     </dependency>
  
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>${junit.version}</version>
          <scope>test</scope>
      </dependency>
  </dependencies>
  

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1-beta-1</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.0.0</version>
        <configuration>
          <contextPath>/</contextPath>
          <scanIntervalSeconds>3</scanIntervalSeconds>
          <scanTargets>
            <scanTarget>
              src/main/webapp/WEB-INF/web.xml
                  </scanTarget>
          </scanTargets>
        </configuration>
      </plugin>
        </plugins>
    <finalName>webapp</finalName>
  </build>

</project>

									

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *