Struts 2 + Spring 2 + JPA + AJAX

來源:互聯網
上載者:User
Added by Musachy Barroso, last edited by Ted Husted on Mar 20, 2007  ( view change)

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 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;
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.java

package quickstart.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Person {
@Id
@GeneratedValue
private Integer id;
private String lastName;
private String firstName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
}

@Entity will let the provider know that this class can be persisted. @Id marks the "id" field as the primary key for this class. @GeneratedValue will cause the id field to be generated by the provider (Hibernate). Classes and fields are by default mapped to tables and columns with the same name, see JPA's documentation for more details. 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: PersonService.java

package quickstart.service;

import java.util.List;

import quickstart.model.Person;

public interface PersonService {
public List<Person> findAll();

public void save(Person person);

public void remove(int id);

public Person find(int id);
}
Create a new class (File -> New -> Class), enter "PersonServiceImpl" for the name and "quickstart.service" for the namespace. Set its content to: PersonServiceImpl.java
package quickstart.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import quickstart.model.Person;

@Transactional
public class PersonServiceImpl implements PersonService {
private EntityManager em;

@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}

@SuppressWarnings("unchecked")
public List<Person> findAll() {
Query query = getEntityManager().createQuery("select p FROM Person p");
return query.getResultList();
}

public void save(Person person) {
if (person.getId() == null) {
// new
em.persist(person);
} else {
// update
em.merge(person);
}
}

public void remove(int id) {
Person person = find(id);
if (person != null) {
em.remove(person);
}
}

private EntityManager getEntityManager() {
return em;
}

public Person find(int id) {
return em.find(Person.class, id);
}

}

@PersistenceContext will make Spring inject an EntityManager into the service when it is instantiated. The @PersistenceContext annotation can be placed on the field, or on the setter method. If the class is annotated as @Transactional, Spring will make sure that its methods run inside a transaction. 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: persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="punit">
</persistence-unit>
</persistence>

JPA configuration can be set on this file. On this example it will be empty because the datasource configuration will be on the Spring configuration file. Spring Update the content of web.xml under /WebContent/WEB-INF/web.xml to: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="person" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>person</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>

This will make the container redirect all requests to Struts "FilterDispatcher" class. "index.jsp" is set as the home page, and Spring's "ContextLoaderListener" is configured as a listener. Create a file named "applicationContext.xml" under /WebContent/WEB-INF, and set its content to: applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="personService" class="quickstart.service.PersonServiceImpl" />

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="personAction" scope="prototype"
class="quickstart.action.PersonAction">
<constructor-arg ref="personService" />
</bean>
</beans>

Note that the "class" attribute of the bean "personAction" is set to the name of the action class, and the "personService" bean will be passed as a parameter to the action constructor. Change the "url", "username" and "password" in the "dataSource" bean to the appropiate values for your database. For more details on the rest of the beans on this file, see Spring's documentation. The "scope" attribute is new in Spring 2, and it means that Spring will create a new PersonAction object every time an object of that type is requested. In Struts 2 a new action object is created to serve each request, that's why we need scope="prototype". 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: PersonAction.java

package quickstart.action;

import java.util.List;

import quickstart.model.Person;
import quickstart.service.PersonService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public class PersonAction implements Preparable {
private PersonService service;
private List<Person> persons;
private Person person;
private Integer id;

public PersonAction(PersonService service) {
this.service = service;
}

public String execute() {
this.persons = service.findAll();
return Action.SUCCESS;
}

public String save() {
this.service.save(person);
this.person = new Person();
return execute();
}

public String remove() {
service.remove(id);
return execute();
}

public List<Person> getPersons() {
return persons;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public void prepare() throws Exception {
if (id != null)
person = service.find(id);
}

public Person getPerson() {
return person;
}

public void setPerson(Person person) {
this.person = person;
}
}

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:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.