`
郁闷的小黑兔
  • 浏览: 28568 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Struts 2 + Spring 2 + JPA + AJAX

 
阅读更多

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

 

 

 

 

 

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>
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/quickstart" />
        <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>
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;
    }
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.devMode" value="true" />

    <package name="person" extends="struts-default">

        <action name="list" method="execute" class="personAction">
            <result>pages/list.jsp</result>
            <result name="input">pages/list.jsp</result>
        </action>

        <action name="remove" class="personAction" method="remove">
            <result>pages/list.jsp</result>
            <result name="input">pages/list.jsp</result>
        </action>

        <action name="save" class="personAction" method="save">
            <result>pages/list.jsp</result>
            <result name="input">pages/list.jsp</result>
        </action>
    </package>

</struts>
list.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>

<p>Persons</p>
<s:if test="persons.size > 0">
	<table>
		<s:iterator value="persons">
			<tr id="row_<s:property value="id"/>">
				<td>
					<s:property value="firstName" />
				</td>
				<td>
					<s:property value="lastName" />
				</td>
				<td>
					<s:url id="removeUrl" action="remove">
						<s:param name="id" value="id" />
					</s:url>
					<s:a href="%{removeUrl}" theme="ajax" targets="persons">Remove</s:a>
					<s:a id="a_%{id}" theme="ajax" notifyTopics="/edit">Edit</s:a>
				</td>
			</tr>
		</s:iterator>
	</table>
</s:if>
index.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<s:head theme="ajax" debug="true"/>
		<script type="text/javascript">
			dojo.event.topic.subscribe("/save", function(data, type, request) {
			    if(type == "load") {
					dojo.byId("id").value = "";
					dojo.byId("firstName").value = "";
					dojo.byId("lastName").value = "";
				}
			});

			dojo.event.topic.subscribe("/edit", function(data, type, request) {
			    if(type == "before") {
					var id = data.split("_")[1];

					var tr = dojo.byId("row_"+id);
					var tds = tr.getElementsByTagName("td");

					dojo.byId("id").value = id;
					dojo.byId("firstName").value = dojo.string.trim(dojo.dom.textContent(tds[0]));
					dojo.byId("lastName").value = dojo.string.trim(dojo.dom.textContent(tds[1]));
				}
			});
		</script>
	</head>
	<body>
	    <s:url action="list" id="descrsUrl"/>

        <div style="width: 300px;border-style: solid">
        	<div style="text-align: right;">
    			<s:a theme="ajax" notifyTopics="/refresh">Refresh</s:a>
    		</div>
    		<s:div id="persons" theme="ajax" href="%{descrsUrl}" loadingText="Loading..." listenTopics="/refresh"/>
        </div>

        <br/>

		<div style="width: 300px;border-style: solid">
			<p>Person Data</p>
			<s:form action="save" validate="true">
			    <s:textfield id="id" name="person.id" cssStyle="display:none"/>
				<s:textfield id="firstName" label="First Name" name="person.firstName"/>
				<s:textfield id="lastName" label="Last Name" name="person.lastName"/>
				<s:submit theme="ajax" targets="persons" notifyTopics="/save"/>
			</s:form>
		</div>
	</body>
</html>
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;
    }
}
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);
}
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);
    }

}
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;

 

分享到:
评论

相关推荐

    struts2+spring2+jpa+ajax

    Struts2、Spring2、JPA(Java Persistence API)和Ajax是Java Web开发中的四大关键技术,它们共同构建了一个高效、灵活且功能强大的应用程序框架。在这个项目中,这四者的组合运用旨在实现一个前后端分离、数据持久...

    help\Struts 2 + Spring 2 + JPA + AJAX.

    ### Struts 2 + Spring 2 + JPA + AJAX 技术栈详解 #### 一、技术背景与介绍 在企业级应用开发中,选择合适的技术栈是非常重要的一步。Struts 2 + Spring 2 + JPA + AJAX 这个组合是早期非常流行的一个Java Web...

    Struts 2 + Spring 2 + JPA + AJAX 示例

    Struts 2、Spring 2、JPA 和 AJAX 是企业级 Web 应用开发中的四大核心技术,它们在构建高效、可扩展的系统中扮演着重要角色。本示例结合这四种技术,提供了一个完整的应用实例,帮助开发者了解如何将它们整合在一起...

    整合 Struts 2 + Spring 2 + JPA + AJAX

    **整合 Struts 2 + Spring 2 + JPA + AJAX** 在现代Web应用程序开发中,Struts 2、Spring、JPA 和 AJAX 是四个非常重要的技术组件。它们各自扮演着关键角色,共同构建了一个功能强大且灵活的后端架构。 **Struts 2...

    经典的Struts2+Spring 2+JPA+AJAX学习项目

    Struts2+Spring 2+JPA+AJAX学习项目是一个经典的Java Web开发组合,用于构建高效、可扩展的应用程序。这个项目的核心组件包括: 1. **Struts2**: Struts2是一个基于MVC(Model-View-Controller)设计模式的Java Web...

    Eclipse开发 Struts 2 + Spring 2 + JPA + AJAX

    在本文中,我们将深入探讨如何使用Eclipse进行Java企业级应用开发,具体涉及Struts 2、Spring 2、JPA以及AJAX技术的集成。这些框架和技术的结合提供了强大的功能,包括MVC架构、依赖注入、持久化管理和异步通信。 ...

    Struts 2 + Spring 2 + JPA + AJAX示例

    Struts 2、Spring 2 和 JPA 是Java Web开发中的三个重要框架,它们共同构建了一个强大、灵活的后端架构。在这个示例中,它们与AJAX技术结合,提供了更丰富的用户交互体验。让我们详细了解一下这些技术以及它们如何...

    ssh2(struts2+spring2.5+hibernate3.3+ajax)带进度条文件上传(封装成标签)

    标题 "ssh2(struts2+spring2.5+hibernate3.3+ajax)带进度条文件上传(封装成标签)" 涉及到的是一个基于Java Web的项目,利用了Struts2、Spring2.5、Hibernate3.3和Ajax技术,实现了文件上传并带有进度条显示的功能...

    网上购物struts2+spring+jpa+ajax

    该项目是一个基于Struts2、Spring、JPA和Ajax技术实现的网上购物系统。这个系统的主要目的是为了演示如何在实际开发中整合这些技术,提供一个功能完善的电商应用框架。以下是对这些关键技术点的详细解释: **Struts...

    Struts2+AJAX+JPA+Spring合集(英文版)

    ### Struts2、AJAX、JPA与Spring的综合运用 在IT领域,尤其是在Web开发中,集成多种框架和技术以创建高效、可扩展且功能丰富的应用程序是常见的实践。本篇文章将深入探讨Struts2、AJAX、JPA与Spring这四种技术的...

    struts2+spring+hibernate 整合的jar包

    Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们的整合(SSH)极大地提升了开发效率和项目的可维护性。下面将详细讲解这三大框架的核心功能以及整合过程中的关键知识点。 1. **Struts2**:Struts2是一...

    Struts2 + Spring2.5 + JPA(hibernate) + AJAX+ 实例

    Struts2、Spring2.5、JPA(Hibernate)以及AJAX是构建高效、模块化且可维护的企业级Web应用程序的常用技术栈。这个实例项目整合了这些技术,旨在提供一个全面的开发环境,帮助开发者理解和掌握它们的协同工作方式。 ...

    struts2+spring3+hibernate4整合所用jar包

    Struts2、Spring3和Hibernate4是Java Web开发中的三大框架,它们的整合是构建高效、灵活的企业级应用的常用方式。这篇详细的知识点解析将深入探讨这三个框架的各自功能,以及如何将它们有效地整合在一起。 **Struts...

Global site tag (gtag.js) - Google Analytics