首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

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

2012-08-26 
Struts 2 + Spring 2 + JPA + AJAX(转自官方文档)TomcatInstall Tomcat before going forward. See Tomcat

Struts 2 + Spring 2 + JPA + AJAX(转自官方文档)
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.

, 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.

    Download the Download jta jar from , and rename it to jta-1.0.1B.jar Install the jta jar file running: To create an eclipse project run: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.
      Under "Target Runtime", click "New", select "Apache Tomcat 5.5" and click next. Enter Tomcat's installation directory and select an installed JRE (1.5 is required)
    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".
Dependencies

Your project should contain the folders "src", "build" and "WebContent". We are going to put all the required jars under "/WebContent/WEB-INF/lib". To add files to the "lib" folder, just copy them to ${workspace}\quickstart\WebContent\WEB-INF\lib, where ${workspace} is the location of your Eclipse workspace folder. The version has been removed from the jar files.

Domain

Our domain model will consist of just a simple "Person" class with a couple of fields.

    Create a new class named "Person" (File -> New -> Class), and enter "quickstart.model" for the package name. Add the fields "id" (int), "firstName" (String), and lastName ("String") with their setter/getter methods. Mark your class with the "@Entity" annotation, and the "id" field with the annotations "@Id" and "@GeneratedValue".

your class will look like:

Person service.

We will now write the class that will take care of CRUD operations on "Person" objects.

    Create a new interface (File -> New -> Interface), enter "PersonService" for the name, and "quickstart.service" for the namespace. Set its content to:
Create a file named "persistence.xml" under the "META-INF" folder and set its content to:
    Create a file named "applicationContext.xml" under /WebContent/WEB-INF, and set its content to:
Struts

We will now create a simple Struts action that wraps PersonServices methods, and we will configure Struts to use Spring as the object factory.

    Open the new class dialog (File -> New -> Class) and enter "PersonAction" for the classname, and "quickstart.action" for the namespace. Set its content to:
    Create a new file named "struts.xml" under the "src" folder. And set its content to:
The pages

We only have two pages, "index.jsp" and "list.jsp". "list.jsp" returns a table with a list of the persons on the database.We have this list on a different page because we are going to add some AJAX to spicy it up.

    Create a new file named "list.jsp" under /WebContent/pages/ and set its content to:

When the edit link is clicked on, it will publish the "/edit" topic, which will trigger a javascript function to populate the fields.

    Create a new file named "index.jsp" under /WebContent and set its content to:
Validation

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:




热点排行