Anda di halaman 1dari 15

Catatan Kecil Guestbook Framework SPRING

TAHAP PERSIAPAN
1. Import template Facelet
2. Tambahkan komponen server Tomcat
3. Tambahkan komponen Hibernate
4. Sisipkan coding ini di Hibernate :

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-
3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</propert
y>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/guestbook</
property>
<property
name="hibernate.connection.username">root</property>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>

5. Sisipkan coding ini di spring.ctx :


<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<tx:annotation-driven transaction-manager="hibernateTransaction"
/>

1
<bean
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionF
actoryBean" id="sessionFactory">
<property name="configLocation" value= "WEB-
INF/hibernate.cfg.xml"></property>
</bean>

<bean
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
id="hibernateTransaction">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>

6. Restart Tomcat
7. Selesai

TAHAP MEMBUAT TABLE GUESTBOOK


1. Tambahkan package “guestbook.entity” di “Java Resources”
2. Didalam package tersebut, tambahkan class “GuestBook”, dan add
“java.io.serialible”.
3. Dalam class tersebut sisipkan code ini, pilih menu source>generate gether dan
generate hashCode() :
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "name", nullable = false, length = 45)


private String name;

@Column(name = "email", nullable = false, length = 45)


private String email;

@Column(name = "website", nullable = true, length = 45)


private String website;

@Lob
@Column(name = "comment", nullable = false)
private String comment;
4. Hasil generated, code lengkapnya adalah :

package lat.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;

2
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name = "table_guestbook")
public class BukuTamu implements Serializable {
private static final long serialVersionUID = 6744738268773106628L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "name", nullable = false, length = 45)


private String name;

@Column(name = "email", nullable = false, length = 45)


private String email;

@Column(name = "website", nullable = true, length = 45)


private String website;

@Lob
@Column(name = "comment", nullable = false)
private String comment;

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 getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}

public String getWebsite() {


return website;
}

public void setWebsite(String website) {


this.website = website;

3
}

public String getComment() {


return comment;
}

public void setComment(String comment) {


this.comment = comment;
}

@Override
public int hashCode() {

final int prime = 31;


int result = 1;
result = prime * result + ((comment == null) ? 0 :
comment.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((website == null) ? 0 :
website.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BukuTamu other = (BukuTamu) obj;
if (comment == null) {
if (other.comment != null)
return false;
} else if (!comment.equals(other.comment))
return false;
if (email == null) {
if (other.email != null)
return false;
}else if (!email.equals(other.email))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (website == null) {
if (other.website != null)
return false;
} else if (!website.equals(other.website))
return false;
return true;

4
}
}
5. Tambahkan mapping ke hibernate code lengkapnya adalah :

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-
3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</propert
y>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/guestbook</
property>
<property
name="hibernate.connection.username">root</property>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="guestbook.entity.GuestBook"/>
</session-factory>
</hibernate-configuration>

6. Restart Tomcat, dan lihat table yang terbentuk di database “guestbook”


7. Selesai

TAHAP MEMBUAT DAO UNTUK MENYIMPAN GUESTBOOK


1. Buat package “guestbook.dao” di “Java Resources”
2. Buat interface “GuestBookDao” di package tersebut, dan masukan script berikut :

package guestbook.dao;

import java.util.List;
import guestbook.entity.GuestBook;

public interface GuestBookDao {


//ambil kelas GuestBook dan buat sebuah variable book
void save(GuestBook book);

List<GuestBook> getAll();
}

3. Buat class “GuestBookDaoHibernate” di package “guestbook.dao”, dan lakukan


setting berikut.

5
4. Browse “HibernateDaoSupport..”
5. Add “GuestBookDao...”
6. Masukan coding dibawah ini :

package guestbook.dao;

import guestbook.entity.GuestBook;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

public class GuestBookDaoHibernate extends HibernateDaoSupport


implements
GuestBookDao {

@Transactional(readOnly=true)
public List<GuestBook> getAll() {
return getHibernateTemplate().loadAll(GuestBook.class);
}

@Transactional(readOnly=false)
public void save(GuestBook book) {
getHibernateTemplate().save(book);

}
}

7. Ke file spring.ctx dan sisipkan code berikut :


<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:annotation-config />
<tx:annotation-driven transaction-manager="hibernateTransaction"
/>

<bean
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionF
actoryBean" id="sessionFactory">
<property name="configLocation" value= "WEB-
INF/hibernate.cfg.xml"></property>
</bean>

6
<bean
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
id="hibernateTransaction">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean class="guestbook.dao.GuestBookDaoHibernate"
id="guestBookDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>

8. Selesai

TAHAP MEMBUAT FORM UNTUK TAMPILAN GUESTBOOK


1. Buat package “guestbook.model”
2. Buat class “GuestBookInsert” di package tersebut
3. Masukan coding ke class tersebut, dan pilih menu source>generate gether

package guestbook.model;
public class GuestBookInsert {
private String comment;
private String email;
private String name;
private String website;
}

4. Tambahkan coding private GuestBookDao dao; ke class tersebut :


package guestbook.model;

import guestbook.dao.GuestBookDao;

public class GuestBookInsert {

private GuestBookDao dao;

private String comment;


private String email;
private String name;
private String website;
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}

7
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}

5. Menu Source>generate Gether>Dao>SetDao


package guestbook.model;

import guestbook.dao.GuestBookDao;

public class GuestBookInsert {

private GuestBookDao dao;

public void setDao(GuestBookDao dao) {


this.dao = dao;
}
private String comment;
private String email;
private String name;
private String website;
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

8
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
}

6. Masukan coding lengkap berikut :


package guestbook.model;

import lat.entity.BukuTamu;
import guestbook.dao.GuestBookDao;
import guestbook.entity.GuestBook;

public class GuestBookInsert {

private GuestBookDao dao;

public void setDao(GuestBookDao dao) {


this.dao = dao;
}
private String comment;
private String email;
private String name;
private String website;
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}

public String processSave(){


GuestBook book=new GuestBook();
book.setComment(comment);
book.setEmail(email);

9
book.setName(name);
book.setWebsite(website);

dao.save(book);

comment="";
email="";
name="";
website="";

return null;
}
}

7. Ke file faces-config.xml, dan masukan coding berikut :


<?xml version="1.0" encoding="UTF-8"?>

<faces-config 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-facesconfig_1_2.xsd"
version="1.2">

<managed-bean>
<managed-bean-name>guestbookinsert</managed-bean-name>
<managed-bean-
class>guestbook.model.GuestBookInsert</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>dao</property-name>
<property-class>guestbook.dao.GuestBookDao</property-
class>
<value>#{guestBookDao}</value>
</managed-property>
</managed-bean>

<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-
handler>
<el-
resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-
resolver>
<message-
bundle>com.echo.web.template.resource.Messages</message-bundle>
</application>

</faces-config>

8. Ke file home.xhtml, dan masukan coding berikut :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"

10
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:t="http://myfaces.apache.org/tomahawk">

<head>
<title>Buku Tamu</title>
</head>

<body>
<div><h1>Selamat Datang di Ri32</h1></div>

<h:form id="form_input">
<h:panelGrid columns="3">
<h:outputLabel value="Name" for="name"/>
<h:inputText value="#{guestbookinsert.name}"
id="name" required="true" requiredMessage="Nama Anda masih kosong!"/>
<h:message for="name"/>

<h:outputLabel value="Email" for="email"/>


<h:inputText value="#{guestbookinsert.email}"
id="email" required="true" requiredMessage="Email Anda masih kosong!"/>
<h:message for="email"/>

<h:outputLabel value="Website" for="website"/>


<h:inputText value="#{guestbookinsert.website}"
id="website"/>
<h:message for="website"/>

<h:outputLabel value="Comment" for="comment"/>


<h:inputTextarea value="#{guestbookinsert.comment}"
id="comment" required="true" requiredMessage="Pesan Anda masih
kosong!"/>
<h:message for="comment"/>

<h:outputText value=""/>
<h:commandButton
action="#{guestbookinsert.processSave}" value="Save"/>
<h:outputText value=""/>

</h:panelGrid>
</h:form>

</body>
</html>
9. Restart Tomcat, dan input datanya.
10. Selesai

TAHAP MENAMPILKAN DATA GUESTBOOK


1. Buat class “GuestBookLoad” di package “guestbook.model”
2. Masukan coding berikut :
package guestbook.model;

11
import guestbook.entity.GuestBook;
import java.util.List;

public class GuestBookLoad {


private List<GuestBook> list;

}
3. Menu source>generate gether..>list>getlist
package guestbook.model;

import guestbook.entity.GuestBook;
import java.util.List;

public class GuestBookLoad {


private List<GuestBook> list;

public List<GuestBook> getList() {


return list;
}
}
4. Sisipkan coding private GuestBookDao dao;
package guestbook.model;

import guestbook.dao.GuestBookDao;
import guestbook.entity.GuestBook;
import java.util.List;

public class GuestBookLoad {


private GuestBookDao dao;

private List<GuestBook> list;

public List<GuestBook> getList() {


return list;
}
}
5. Menu source>generate gether..>dao>setdao
package guestbook.model;

import guestbook.dao.GuestBookDao;
import guestbook.entity.GuestBook;
import java.util.List;

public class GuestBookLoad {


private GuestBookDao dao;

public void setDao(GuestBookDao dao) {


this.dao = dao;
}

private List<GuestBook> list;

public List<GuestBook> getList() {

12
return list;
}
}
6. Masukan coding list=dao.getAll();
package guestbook.model;

import guestbook.dao.GuestBookDao;
import guestbook.entity.GuestBook;
import java.util.List;

public class GuestBookLoad {


private GuestBookDao dao;

public void setDao(GuestBookDao dao) {


this.dao = dao;
}

private List<GuestBook> list;

public List<GuestBook> getList() {


list=dao.getAll();
return list;
}
}

7. Ke file faces-config, dan tambahkan coding :


<?xml version="1.0" encoding="UTF-8"?>

<faces-config 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-facesconfig_1_2.xsd"
version="1.2">

<managed-bean>
<managed-bean-name>guestbookinsert</managed-bean-name>
<managed-bean-
class>guestbook.model.GuestBookInsert</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>dao</property-name>
<property-class>guestbook.dao.GuestBookDao</property-
class>
<value>#{guestBookDao}</value>
</managed-property>
</managed-bean>

<managed-bean>
<managed-bean-name>guestbookload</managed-bean-name>
<managed-bean-class>guestbook.model.GuestBookLoad</managed-
bean-class>
<managed-bean-scope>request</managed-bean-scope>

13
<managed-property>
<property-name>dao</property-name>
<property-class>guestbook.dao.GuestBookDao</property-
class>
<value>#{guestBookDao}</value>
</managed-property>
</managed-bean>

CATATAN : guestBookDao diambil dari


“id=guestBookDao” pada file Spring.ctx.xml
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-
handler>
<el-
resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-
resolver>
<message-
bundle>com.echo.web.template.resource.Messages</message-bundle>
</application>

</faces-config>

8. Ke file home.xhtml, tambahkan coding :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:t="http://myfaces.apache.org/tomahawk">

<head>
<title>Buku Tamu</title>
</head>

<body>
<div><h1>Selamat Datang di Ri32</h1></div>
<div>
<h:form id="form_input">
<h:panelGrid columns="3">
<h:outputLabel value="Name" for="name"/>
<h:inputText value="#{guestbookinsert.name}"
id="name" required="true" requiredMessage="Nama Anda masih kosong!"/>
<h:message for="name"/>

<h:outputLabel value="Email" for="email"/>


<h:inputText value="#{guestbookinsert.email}"
id="email" required="true" requiredMessage="Email Anda masih kosong!"/>
<h:message for="email"/>

<h:outputLabel value="Website" for="website"/>


<h:inputText value="#{guestbookinsert.website}"
id="website"/>

14
<h:message for="website"/>

<h:outputLabel value="Comment" for="comment"/>


<h:inputTextarea value="#{guestbookinsert.comment}"
id="comment" required="true" requiredMessage="Pesan Anda masih
kosong!"/>
<h:message for="comment"/>

<h:outputText value=""/>
<h:commandButton
action="#{guestbookinsert.processSave}" value="Save"/>
<h:outputText value=""/>

</h:panelGrid>
</h:form></div>

<div>
<h:dataTable value="#{guestbookload.list}" var="book"
border="1">
<h:column>
<h:panelGroup rendered="#{book.website !=''}">
<a
href="#{book.website}">#{book.website}</a>
</h:panelGroup>
</h:column>

<h:column>
<h:panelGroup rendered="#{book.website !=''}">
#{book.name}
</h:panelGroup>
</h:column>

<h:column>
#{book.comment}
</h:column>
</h:dataTable>
</div>

</body>

</html>
9. Restart Tomcat, input data, dan otomatis akan menampilkan data tersebut
10. Selesai.

Created By Agus Sumarna


http://ri32.wordpress.com
http://labhouse.co.cc

15

Anda mungkin juga menyukai