数字面板 > easyjf wiki > EasyJWeb > A CRUD example of EasyjWeb > 查看
  easyjf wiki 登录 | 注册   查看当前页的可打印版本。  
  A CRUD example of EasyjWeb
添加者: 瞌睡虫.cn, 最后更新者: sirty 于 十二月 11, 2007  (查看变更)
标签: 
(没有)

The motivation behind this tutorial is to provide a simple step-by-step guide for getting started with Easyjf in CRUD.

As convention, the CURD is stand for Create, Read/Retrieve ,Update and delete. Relative frameworks being used are: Easyjweb, Spring2, JPA (java persistence API)

Ok,Let us start experimenting the convinent and easy java web development brought by Easyjf. Please do not hesitate to contact us should you have any questions. Our official website is http://www.easyjf.com

prerequisites:

Before our jouney, please ensure that follows products are installing on your host.

  • jdk 5
    tomcat 5.x
    eclipse 3.x
    mysql server 5.x

dependency of java products:

  • antlr-2.7.6.jar
    asm-1.5.3.jar
    cglib-2.1_3.jar
    commons-collections-3.2.jar
    commons-fileupload-1.0.jar
    commons-logging-1.0.4.jar
    dom4j-1.6.1.jar
    easydbo-0.9.1.jar
    easyjweb-core-1.0.1.jar
    easyjweb-ext-1.0.1.jar
    hibernate-3.2.2.ga.jar
    hibernate-annotations-3.2.1.ga.jar
    hibernate-entitymanager-3.2.1.ga.jar
    javassist-3.3.ga.jar
    jaxen-1.1-beta-4.jar
    jboss-archive-browsing-5.0.0alpha-200607201-119.jar
    jta-1.0.1B.jar
    log4j-1.2.14.jar
    mysql-connector-java-5.0.5.jar
    persistence-api-1.0.jar
    spring-2.0-rc2.jar
    velocity-1.4.jar

The first step: Domain Model

A person model

In this application, a person model is created to describle the entity and its relative properties: name and gender.

The code as follows:

Person.java

package com.easyjf.demo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class Person { @Id
 @GeneratedValue(strategy=GenerationType.TABLE)
 private Long id;
 private String name;
 private String sex; public Long getId() {   return id;  }  public void setId(Long id) {   this.id = id;  }  public String getName() {   return name;  }  public void setName(String name) {   this.name = name;  }  public String getSex() {   return sex;  }  public void setSex(String sex) {   this.sex = sex;  }
}

The second step: Service interface

We create a service interface to abastractly describles the relative logical operations (CRUD) regarding person.

IPersonService*.java*

package com.easyjf.demo;
import java.io.Serializable;
import java.util.List;public interface IPersonService {   public List findAll();  return a list of person.   public void save(Person p);// persisitence of Person instance.   public void remove(Serializable id);//to delete a person who owns specific ID  public Person find(Serializable id);//to find a person who owns specific ID   }

The third step: implementation of service interface

This is a service class to implement the service interface which is created at last step.

PersonServiceImpl*.java*

package com.easyjf.demo;
import java.io.Serializable;
import java.util.List;import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;import org.springframework.transaction.annotation.Transactional;@Transactional
public class PersonServiceImpl implements IPersonService { @PersistenceContext
 private EntityManager em; //a JPA entity manager @SuppressWarnings("unchecked")
 public List<Person> findAll() {   Query q = this.getEm().createQuery("select p FROM Person p");   return q.getResultList();  }  public Person find(Serializable id) {   return this.getEm().find(Person.class, id);  }  public void remove(Serializable id) {  Person p = this.find(id);
  if (p != null) {    this.getEm().remove(p);   }
 }   public void save(Person p) {  if (p.getId() == null) {    this.getEm().persist(p);   }else {    this.getEm().merge(p);   }
 }   public EntityManager getEm() {   return em;  }  public void setEm(EntityManager em) {   this.em = em;  } }

The fourth step: implemtation of control layer in MVC

At this step, we will create control layer action to conect others layers: modeling,model and view. it is extend from AbstractPageCmdAction. It has similar purposes as DispatchAction in Struts.

PersonAction*.java*

public class PersonAction extends AbstractPageCmdAction { @Inject
 private IPersonService personService; public Page doInit(WebForm f, Module m) {   return go("list");  }  public void doList(WebForm form) {   List list = this.personService.findAll();   form.addResult("list", list);  }  public void doSave(WebForm form) {   Person p = form.toPo(Person.class);   this.personService.save(p);   go("list");  }  public void doNew() {   page("edit");  }  public void doEdit(WebForm form) {   Long id = new Long(form.get("id").toString());   Person p = this.personService.find(id);   form.addPo(p);  }  public void doDelete(WebForm form) {   Long id = new Long(form.get("id").toString());   this.personService.remove(id);   go("list");  }  public void setPersonService(IPersonService personService) {   this.personService = personService;  }
}

5.0 The fifth steps: confirugation defination

5.1 jpa configuration

Create a JPA configuration file named "persistence.xml" and set its content to:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 version="1.0">
 <persistence-unit name="app-unit"
  transaction-type="RESOURCE_LOCAL">
 </persistence-unit>
</persistence>

5.2 There is definiation of Spring configuration

filepath: /src/main/java/application.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.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><bean
  class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean id="transactionManager"
  class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory"
   ref="entityManagerFactory" />
 </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url"
   value="jdbc:mysql://localhost/demo?createDatabaseIfNotExist=true" />
  <property name="username" value="root" />
  <property name="password" value="" />
 </bean> <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="generateDdl" value="true" />
    <property name="showSql" value="true" />
   </bean>
  </property>
 </bean> <bean id="personService" class="com.easyjf.demo.PersonServiceImpl" />
</beans>

 5.3 Update the content of web.xml, then all satisfied request will be handle by Easyjweb
This updating covers:
the definition of the Easyjf servlet named "ActionServlet"
the URL mapping for the calls to this servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
 <!-Definiation of ActionServle in EasyJWeb  ->
 <servlet>
  <servlet-name>easyjf</servlet-name>
  <servlet-class>com.easyjf.web.ActionServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>easyjf</servlet-name>
  <url-pattern>*.ejf</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>easyjf</servlet-name>
  <url-pattern>/ejf/*</url-pattern>
 </servlet-mapping>
 <!- Filter of handling characterset ->
 <filter>
  <filter-name>CharsetFilter</filter-name>
  <filter-class>com.easyjf.web.CharsetFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
   <param-name>ignore</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>CharsetFilter</filter-name>
  <servlet-name>easyjf</servlet-name>
 </filter-mapping> 
 
</web-app>

5.4 create a easyjweb confiuraton   filepath: /src/main/webapp/WEB-INF/easyjf-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<easyjf-web xmlns="http://www.easyjf.com/schema/easyjf/web"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.easyjf.com/schema/easyjf/web http://www.easyjf.com/schema/easyjf/web/easyjf-web-0.0.1.xsd">
 <beans>
  <!- start: declare spring as part of Easyjweb container. ->
  <bean name="springContainer"
   class="org.springframework.web.context.support.XmlWebApplicationContext">
   <property name="configLocations">
    <list>
     <value>WEB-INF/classes/application.xml</value>
    </list>
   </property>
  </bean>
  <bean name="innerSpringContainer"
   class="com.easyjf.container.impl.SpringContainer">
   <property name="factory" ref="springContainer" />
  </bean>
  <!- end: declare spring as part of Easyjweb container. ->
 </beans> <modules>
  <!- start of easyjweb module definition ->
  <module name="person" path="/person"
   action="com.easyjf.demo.PersonAction">
  </module>
  <!- end of easyjweb odule definition  ->
 </modules>
</easyjf-web>

6.0 The sixth step: view(presentation) layer

This is final step in this turtorial. Two pages are required in presentation layer. you may change them as you want.

6.1 edit.html,filepath:/src/main/webapp/WEB-INF/views/person/edit.html

This page is used for inputing new person data or editing of exist person information.

<form method="post" action="/ejf/person/save" name="edit">
<input type="hidden" name="id" value="$ {id}" />
name:<input type="text" maxlength="20" size="20" value="$! {name}" name="name"><br />
gender:<input type="text" maxlength="20" size="20" value="$! {sex}" name="sex"><br />
<input type="submit" value="sumbit" name="submit">
<input type="button" value="return" name="return" onclick="window.location.href='/ejf/person/list';">
 </form>

6.2 list.html,filepath /src/main/webapp/WEB-INF/views/person/list.html,
This page is used for listing all person data on the database.
The main html code as follows

<a href="/ejf/person/new" ><b>insert</b></a><br />
#foreach ( $info in $list )
name:$info.name <br />
gender:$info.sex $info.id <br />
<a href="/ejf/person/edit/$info.id" >edit</a>
<a href="/ejf/person/delete/$info.id" >delete</a>
<hr>
#end

The seventh step

 This is final stop of this tutorial. Nothing more, just visit your result at:

http://localhost:8080/myapp/ejf/person&nbsp;or http://localhost:8080/myapp/person.ejf

Resources:

Depency http://www.easyjf.com/download/easyjweb/easyjweb-quickstart-crud-libs.zip

sourcecode  http://www.easyjf.com/download/easyjweb/easyjweb-quickstart-crud-src.zip

complete package (sourcecode with jars):http://www.easyjf.com/download/easyjweb/easyjweb-quickstart-crud.zip

Site running on a free Atlassian Confluence Open Source Project License granted to EasyJWeb. Evaluate Confluence today.
程序版权:confluence,系统中文提供:EasyJF开源知识库 - 错误报告 - 联系管理员