Struts 2 + Spring 2 + JPA + AJAX(转自官方文档)
Tomcat
Install Tomcat before going forward. See Tomcat's installation guide if you have any problem installing it.
MySqlInstall 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 wayTo run the project this way you will need maven installed.
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.
DomainOur domain model will consist of just a simple "Person" class with a couple of fields.
your class will look like:
Person service.We will now write the class that will take care of CRUD operations on "Person" objects.
We will now create a simple Struts action that wraps PersonServices methods, and we will configure Struts to use Spring as the object factory.
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.
When the edit link is clicked on, it will publish the "/edit" topic, which will trigger a javascript function to populate the fields.
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: