日期:2014-05-16  浏览次数:20714 次

Struts 2 + Spring 2 + JPA + AJAX(转自官方文档)

On this tutorial we will demonstrate how to setup Struts 2 in Eclipse, and make it work with Spring, Java Persistence API (using Hibernate) and Struts 2 Ajax tags.

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

Prerequisites

  • Tomcat 5.5
  • Eclipse
  • MySql Server

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;

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:
    $ 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:
    $ mvn jetty:run
  6. Point your browser to http://localhost:8080/quickstart
  7. To create an eclipse project run:
    $ mvn eclipse:eclipse

    or (to create web project for WTP):

    mvn eclipse:eclipse -Dwtpversion=1.0

Doing it yourself

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 Runt