Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Add maven zipped project, add TopLink howto

...

NOTE: Following this tutorial verbatim will require use of a Struts 2 deployment greater than 2.0.3

Show me the code

You can just download the zipped Eclipse project, add the required dependencies to the lib folder under the /WebContent/WEB-INF/lib folder (relative to project's root folder) and import it into Eclipse.

Show me the code, the almost painless way

To run the project this way you will need maven installed.

  1. Download the zipped project
  2. Download jta jar from here, and rename it to jta-1.0.1B.jar
  3. Install the jta jar file running:
    Code Block
    
    $ mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta -Dversion=1.0.1B -Dpackaging=jar -Dfile=c:\path\to\jar\jta-1.0.1B.jar
    
  4. Bear with me, we are almost there
  5. cd into quickstart and run:
    Code Block
    
    $ mvn jetty:run
    
  6. point your browser to http://localhost:8080/quickstart

Prerequisites

Tomcat

Install Tomcat before going forward. See Tomcat's installation guide if you have any problem installing it.

MySql

Install and configure MySql. Create a database named "quickstart" and run the script below to create the "Person" table. Later, on applicationContext.xml, we'll use 'root' as the user name and password for the database, remember to replace those values with the right ones for your database.

...


CREATE TABLE 'quickstart'.'Person' (
  'id' INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  'firstName' VARCHAR(45) NOT NULL,
  'lastName' VARCHAR(45) NOT NULL,
  PRIMARY KEY('id')
)
ENGINE = InnoDB;

Prerequisites

Tomcat

Install Tomcat before going forward. See Tomcat's installation guide if you have any problem installing it.

MySql

Install and configure MySql. Create a database named "quickstart" and run the script below to create the "Person" table. Later, on applicationContext.xml, we'll use 'root' as the user name and password for the database, remember to replace those values with the right ones for your database.

Code Block
SQL
SQL

CREATE TABLE 'quickstart'.'Person' (
  'id' INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  'firstName' VARCHAR(45) NOT NULL,
  'lastName' VARCHAR(45) NOT NULL,
  PRIMARY KEY('id')
)
ENGINE = InnoDB;

Get the code

Show me the code

You can just download the zipped Eclipse project, add the required dependencies to the lib folder under the /WebContent/WEB-INF/lib folder (relative to project's root folder) and import it into Eclipse.

The maven way

To run the project this way you will need maven installed.

  1. Download the zipped project
  2. Download jta jar from here, and rename it to jta-1.0.1B.jar
  3. Install the jta jar file running:
    Code Block
    
    $ mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta -Dversion=1.0.1B -Dpackaging=jar -Dfile=c:\path\to\jar\jta-1.0.1B.jar
    
  4. Bear with me, we are almost there
  5. cd into quickstart and run:
    Code Block
    
    $ mvn jetty:run
    
  6. Point your browser to http://localhost:8080/quickstart
  7. To create an eclipse project run:
    Code Block
    
    $ mvn eclipse:eclipse
    

Doing it yourself

Create Eclipse project

  1. Open Eclipse

Create Eclipse project

  1. Open Eclipse. Seriously, you need to open Eclipse.
  2. Click File -> New -> Project. Under the "Web" folder, select "Dynamic Web Project" and click "Next".
  3. Enter the project name, "quickstart" from here on. The project will be running inside Tomcat, so we need to create a server configuration for it.
    1. Under "Target Runtime", click "New", select "Apache Tomcat 5.5" and click next.
    2. Enter Tomcat's installation directory and select an installed JRE (1.5 is required)
  4. Now you should be back to the project creation wizard, with Tomcat as your Target Runtime. Click "Next". Select "Dynamic Web Module" and "Java" facets, and click "Finish".

...

Because we don't want any John Doe on our database, we will add some basic client side validation to our form. In Struts 2, validation can be placed on xml files with the name pattern ActionName-validation.xml, located on the same package as the action. To add validation to an specific alias of an action (like a method), the validation file name follows the pattern ActionName-alias-validation.xml, where "alias" is the action alias name (in this case a method name, "save"). Add a file named "PersonAction-save-validation.xml" under /src/quickstart/action, and set its content to:-validation.xml" under /src/quickstart/action, and set its content to:

Code Block
XML
XML

<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
    <field name="person.firstName">
        <field-validator type="requiredstring">
            <message>First name is required!</message>
        </field-validator>
    </field>
    <field name="person.lastName">
        <field-validator type="requiredstring">
            <message>Last name is required!</message>
        </field-validator>
    </field>
</validators>

See the Struts documentation for details on existing validators, and how to write, and plug in, your own validators.

To run the project, Right click on your project and Run As -> Run on Server. You can debug it on the same way, Right click on the project and Debug As -> Debug on Server. Download and install Struts 2 Showcase to see more examples.

Using Toplink Essentials instead of Hibernate

  1. Add this to pom.xml
    Code Block
    XML
    XML
    
     <repositories>
         <repository>
             <id>java.net</id>
             <url>https://maven-repository.dev.java.net/nonav/repository</url>
             <layout>legacy</layout>
         </repository>
     </repositories>
    
  2. Add this to the dependencies node in pom.xml
    Code Block
    XML
    XML
    
    

...

  1. <dependency>
         <groupId>toplink.essentials</groupId>
         <artifactId>toplink-essentials</artifactId>
         <version>2.0-38</version>
         <exclusions>
             <exclusion>
              

...

  1.  

...

  1.   <groupId>javax.transaction</groupId>
              

...

  1.  

...

  1.  

...

  1.  

...

  1. <artifactId>jta</

...

  1. artifactId>
             </

...

  1. exclusion>
         

...

  1. </exclusions>
    </dependency>
    
  2. Replace the jpaVendorAdapter element in applicationContext.xml with this:
    Code Block
    XML
    XML
    
    <property name="jpaVendorAdapter">
        

...

  1. <bean 

...

  1. class="

...

  1. org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
            

...

  1. <property name="databasePlatform" value="oracle.toplink.essentials.platform.database.MySQL4Platform" />
            <property name="generateDdl" value="true" /> 

...

  1. 
     

...

  1.  

...

  1.  

...

  1.      <property name="showSql" value="true" 

...

  1. /

...

  1. >
        </

...

  1. bean>
    </

...

See the Struts documentation for details on existing validators, and how to write, and plug in, your own validators.

...

  1. property>
    

References

Struts
Spring JPA Doc
JPA and Spring Tutorial
Eclipse Dali