出自:http://cwiki.apache.org/S2WIKI/struts-2-spring-2-jpa-ajax.html
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
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.
Prerequisites
- Struts 2
- Tomcat 5.5
- Eclipse
- Eclipse WTP
- Hibernate Core
- Hibernate Annotations
- Hibernate Entity Manager
- MySql Server
- Mysql JDBC Driver
- Spring 2.0
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 Eclipse project
- Open Eclipse. Seriously, you need to open Eclipse.
- Click File -> New -> Project. Under the "Web" folder, select "Dynamic Web Project" and click "Next".
- 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.
Jar | From |
---|---|
xwork.jar | Struts 2 |
struts2-api.jar | Struts 2 |
struts2-core.jar | Struts 2 |
struts2-Spring-plugin.jar | Struts 2 |
ognl.jar | Struts 2 |
freemarker-2.3.4.jar | Struts 2 |
mysql-connector-java.jar | MySql JDBC Driver |
spring.jar | Sping 2.0 |
antlr.jar | Hibernate Core |
asm.jar | Hibernate Core |
asm-attrs.jar | Hibernate Core |
cglib.jar | Hibernate Core |
dom4j.jar | Hibernate Core |
jdbc2_0-stdext.jar | Hibernate Core |
ehcache.jar | Hibernate Core |
hibernate3.jar | Hibernate Core |
xml-apis.jar | Hibernate Core |
commons-collections.jar | Hibernate Core |
ejb3-persistence.jar | Hibernate Annotations |
jta.jar | Hibernate Annotations |
hibernate-annotations.jar | Hibernate Annotations |
hibernate-entitymanager.jar | Hibernate Entity Manager |
javassist.jar | Hibernate Entity Manager |
jboss-archive-browsing.jar | Hibernate Entity Manager |
Right click on the project and select "Refresh" (to notify Eclipse of the jars that we just added).
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:
JPA configuration
- Create a folder named "META-INF" under the "src" folder.
- Create a file named "persistence.xml" under the "META-INF" folder and set its content to:
Spring
- Update the content of web.xml under /WebContent/WEB-INF/web.xml 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:
Look mom my action is a simple POJO!
The "Preparable" interface instructs Struts to call the "prepare" method if the "PrepareInterceptor" is applied to the action (by default, it is). The constructor of the action takes a "PersonService" as a parameter, which Spring will take care of passing when the action is instatiated.
- 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:
The div "persons" will load its content asynchronously, and will show "Loading..." while while the request is on progress (you can use the "indicator" attribute for better progress feedback), you can force it to refresh clicking on the "Refresh" link. The "submit" button, will make an asynchronous request to the action "save" ("save" method on PersonAction), and will publish the topic "/save" to which we subscribed to, using "dojo.event.topic.subscribe", to clear the input fields.
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:
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.