...
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.
- Download the zipped project
- Download jta jar from here, and rename it to jta-1.0.1B.jar
- 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
- Bear with me, we are almost there
- cd into quickstart and run:
Code Block $ mvn jetty:run
- point your browser to http://localhost:8080/quickstart
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 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 | ||||
---|---|---|---|---|
| ||||
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.
- Download the zipped project
- Download jta jar from here, and rename it to jta-1.0.1B.jar
- 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
- Bear with me, we are almost there
- cd into quickstart and run:
Code Block $ mvn jetty:run
- Point your browser to http://localhost:8080/quickstart
- To create an eclipse project run:
Code Block $ mvn eclipse:eclipse
Doing it yourself
Create Eclipse project
- Open Eclipse
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".
...
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 | ||||
---|---|---|---|---|
| ||||
<!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
- 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>
- Add this to the dependencies node in pom.xml
Code Block XML XML
...
<dependency> <groupId>toplink.essentials</groupId> <artifactId>toplink-essentials</artifactId> <version>2.0-38</version> <exclusions> <exclusion>
...
...
<groupId>javax.transaction</groupId>
...
...
...
...
<artifactId>jta</
...
artifactId> </
...
exclusion>
...
</exclusions> </dependency>
- Replace the jpaVendorAdapter element in applicationContext.xml with this:
Code Block XML XML <property name="jpaVendorAdapter">
...
<bean
...
class="
...
org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
...
<property name="databasePlatform" value="oracle.toplink.essentials.platform.database.MySQL4Platform" /> <property name="generateDdl" value="true" />
...
...
...
...
<property name="showSql" value="true"
...
/
...
> </
...
bean> </
...
See the Struts documentation for details on existing validators, and how to write, and plug in, your own validators.
...
property>