Creazione di una semplice web application utilizzando maven, jpa2, spring, struts2, mysql (parte 2)

una volta configurato correttamente il file POM.xml possiamo iniziare la configurazione di tutti gli altri file necessari per il buon funzionamento della nostra applicazione web

Configurazione web.xml

sotto la cartella WEB-INF inserire il seguente file web.xml

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

<web-app id="web-app" version="2.4" 
         xmlns="http://java.sun.com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>Semplice WebApp usando struts, jpa, spring</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>
  
  <filter>
     <filter-name>struts-cleanup</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
  </filter>   
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
     <filter-name>struts-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>   
    <!-- Listeners -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

									

 Configurazione applicationContext.xml e struts.xml

Questi file devono essere inseriti sotto la cartella /src/main/resources.

ApplicationContext.xml (file di configurazione di spring)

<?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: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-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <!-- Enable JPA Support -->
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <!-- Define EntityManagerFactory and Datasource -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/test" />
        <property name="username" value="*****" />
        <property name="password" value="*********" />
    </bean>

    <!-- Define Transaction Manager -->
    <bean id="transactionManager"
          class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    
    <!-- Define the UserDao -->
    <bean id="userDao" 
          class="it.sample.webapp.dao.jpa.impl.UserDaoImpl" 
          scope="singleton" />
    
    <!-- Define the UserService -->
    <!-- enable the configuration of transactional behavior based on annotations -->
    <bean id="userService" 
          class="it.sample.webapp.service.impl.UserServiceImpl" scope="singleton">
          <property name="userDao" ref="userDao" />
    </bean>      

  <!-- Example of SAF2 action instantiated by Spring -->
    <bean id="sampleAction" class="it.sample.webapp.action.SampleAction" scope="prototype">
          <property name="userService" ref="userService" />
    </bean>
</beans>

									

Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"       
          "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <include file="struts-default.xml"/>
  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="true" />
    <constant name="struts.objectFactory" value="spring" />

  <package name="sample" namespace="/sample" extends="struts-default">
        <default-interceptor-ref name="defaultStack"/>
    <action name="sample" class="sampleAction">
      <result>/WEB-INF/sample/sample.jsp</result>
    </action>

    <!-- Add additional "example" package actions here. -->

  </package>
  <!-- Add addition packages and configuration here. -->
</struts>

									

Sotto la cartella /src/mai/webapp/META-INF inserire il file persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

    <persistence-unit name="webapp">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>
									

Le classi inserite all’interno dei file di configurazione saranno sviluppate nei prossimi step.