__P.Sこの例では、MySQLデータベースを使用してTomcat 6 Webコンテナにデプロイします。
1.プロジェクトの構成
この例のディレクトリ構造
2.テーブルスクリプト
顧客テーブルを作成し、2つのダミーレコードを挿入します。
DROP TABLE IF EXISTS `mkyongdb`.`customer`; CREATE TABLE `mkyongdb`.`customer` ( `CUSTOMER__ID` bigint(20) UNSIGNED NOT NULL AUTO__INCREMENT, `NAME` varchar(45) NOT NULL, `ADDRESS` varchar(255) NOT NULL, `CREATED__DATE` datetime NOT NULL, PRIMARY KEY (`CUSTOMER__ID`) ) ENGINE=InnoDB AUTO__INCREMENT=17 DEFAULT CHARSET=utf8; insert into mkyongdb.customer(customer__id, name, address, created__date) values(1, 'mkyong1', 'address1', now()); insert into mkyongdb.customer(customer__id, name, address, created__date) values(2, 'mkyong2', 'address2', now());
3. Hibernate Stuff
顧客テーブルのモデルクラスとHibernateマッピングファイル。
File:Customer.java
package com.mkyong.customer.model; import java.util.Date; public class Customer{ public long customerId; public String name; public String address; public Date createdDate; //getter and setter methods }
File:Customer.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.mkyong.customer.model.Customer" table="customer" catalog="mkyongdb"> <id name="customerId" type="long"> <column name="CUSTOMER__ID"/> <generator class="identity"/> </id> <property name="name" type="string"> <column name="NAME" length="45" not-null="true"/> </property> <property name="address" type="string"> <column name="ADDRESS" not-null="true"/> </property> <property name="createdDate" type="timestamp"> <column name="CREATED__DATE" length="19" not-null="true"/> </property> </class> </hibernate-mapping>
4.春のもの
ビジネスロジックとデータベースの相互作用のためのSpringのBOとDAOクラス
File:CustomerBo.java
package com.mkyong.customer.bo; import java.util.List; import com.mkyong.customer.model.Customer; public interface CustomerBo{ void addCustomer(Customer customer); List<Customer> findAllCustomer(); }
File:CustomerBoImpl.java
package com.mkyong.customer.bo.impl; import java.util.List; import com.mkyong.customer.bo.CustomerBo; import com.mkyong.customer.dao.CustomerDao; import com.mkyong.customer.model.Customer; public class CustomerBoImpl implements CustomerBo{ CustomerDao customerDao; public void setCustomerDao(CustomerDao customerDao) { this.customerDao = customerDao; } public void addCustomer(Customer customer){ customerDao.addCustomer(customer); } public List<Customer> findAllCustomer(){ return customerDao.findAllCustomer(); } }
File:CustomerDao.java
package com.mkyong.customer.dao; import java.util.List; import com.mkyong.customer.model.Customer; public interface CustomerDao{ void addCustomer(Customer customer); List<Customer> findAllCustomer(); }
File:CustomerDaoImpl.java
package com.mkyong.customer.dao.impl; import java.util.Date; import java.util.List; import com.mkyong.customer.dao.CustomerDao; import com.mkyong.customer.model.Customer; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao{ public void addCustomer(Customer customer){ customer.setCreatedDate(new Date()); getHibernateTemplate().save(customer); } public List<Customer> findAllCustomer(){ return getHibernateTemplate().find("from Customer"); } }
File:CustomerBean.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerBo" class="com.mkyong.customer.bo.impl.CustomerBoImpl" > <property name="customerDao" ref="customerDao"/> </bean> <bean id="customerDao" class="com.mkyong.customer.dao.impl.CustomerDaoImpl" > <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
5.春のデータベース
Springでデータベースの詳細を設定します。
ファイル:db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mkyongdb jdbc.username=root jdbc.password=password
File:DataSource.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>WEB-INF/classes/config/database/db.properties</value> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> </beans>
6.春の休止状態
`LocalSessionFactoryBean`を介してHibernateとSpringを統合します。
File:HibernateSessionFactory.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- Hibernate session factory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show__sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/mkyong/customer/hibernate/Customer.hbm.xml</value> </list> </property> </bean> </beans>
7. JSF 2.0
SpringのBOを呼び出してデータベースから顧客のレコードを追加または取得するJSFマネージドBean
File:CustomerBean.java
package com.mkyong; import java.io.Serializable; import java.util.List; import com.mkyong.customer.bo.CustomerBo; import com.mkyong.customer.model.Customer; public class CustomerBean implements Serializable{ //DI via Spring CustomerBo customerBo; public String name; public String address; //getter and setter methods public void setCustomerBo(CustomerBo customerBo) { this.customerBo = customerBo; } //get all customer data from database public List<Customer> getCustomerList(){ return customerBo.findAllCustomer(); } //add a new customer data into database public String addCustomer(){ Customer cust = new Customer(); cust.setName(getName()); cust.setAddress(getAddress()); customerBo.addCustomer(cust); clearForm(); return ""; } //clear form values private void clearForm(){ setName(""); setAddress(""); } }
`h:dataTable`を介して既存の顧客レコードを表示するためのJSFページと、新しい顧客レコードをデータベースに挿入するためのいくつかのテキストコンポーネント。
File:default.xhtml
<?xml version="1.0" encoding="UTF-8"?> <!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" > <h:head> <h:outputStylesheet library="css" name="table-style.css" /> </h:head> <h:body> <h1>JSF 2.0 + Spring + Hibernate Example</h1> <h:dataTable value="#{customer.getCustomerList()}" var="c" styleClass="order-table" headerClass="order-table-header" rowClasses="order-table-odd-row,order-table-even-row" > <h:column> <f:facet name="header"> Customer ID </f:facet> #{c.customerId} </h:column> <h:column> <f:facet name="header"> Name </f:facet> #{c.name} </h:column> <h:column> <f:facet name="header"> Address </f:facet> #{c.address} </h:column> <h:column> <f:facet name="header"> Created Date </f:facet> #{c.createdDate} </h:column> </h:dataTable> <h2>Add New Customer</h2> <h:form> <h:panelGrid columns="3"> Name : <h:inputText id="name" value="#{customer.name}" size="20" required="true" label="Name" > </h:inputText> <h:message for="name" style="color:red"/> Address : <h:inputTextarea id="address" value="#{customer.address}" cols="30" rows="10" required="true" label="Address" > </h:inputTextarea> <h:message for="address" style="color:red"/> </h:panelGrid> <h:commandButton value="Submit" action="#{customer.addCustomer()}"/> </h:form> </h:body> </html>
8. JSF 2.0 Spring
JSF 2.0をSpringと統合する(詳細はこちらを参照) –
JSF 2.0 Spring統合の例
File:applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- Database Configuration --> <import resource="classes/config/spring/beans/DataSource.xml"/> <import resource="classes/config/spring/beans/HibernateSessionFactory.xml"/> <!-- Beans Declaration --> <import resource="classes/com/mkyong/customer/spring/CustomerBean.xml"/> </beans>
File:faces-config.xml
<?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__2__0.xsd" version="2.0"> <application> <el-resolver> org.springframework.web.jsf.el.SpringBeanFacesELResolver </el-resolver> </application> <managed-bean> <managed-bean-name>customer</managed-bean-name> <managed-bean-class>com.mkyong.CustomerBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>customerBo</property-name> <value>#{customerBo}</value> </managed-property> </managed-bean> </faces-config>
File:web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app__2__5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app__2__5.xsd" id="WebApp__ID" version="2.5"> <display-name>JavaServerFaces</display-name> <!-- Add Support for Spring --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> <!-- Change to "Production" when you are ready to deploy --> <context-param> <param-name>javax.faces.PROJECT__STAGE</param-name> <param-value>Development</param-value> </context-param> <!-- Welcome page --> <welcome-file-list> <welcome-file>faces/default.xhtml</welcome-file> </welcome-file-list> <!-- JSF mapping --> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Map these files with JSF --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/** </url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>** .jsf</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>** .faces</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>** .xhtml</url-pattern> </servlet-mapping> </web-app>
9.デモ
それを実行し、顧客データを記入し、「送信」ボタンをクリックします。
ソースコードをダウンロードする
ダウンロードする – リンク://wp-content/uploads/2010/12/JSF-2-Spring-Hibernate-Integration-Example.zip[JSF-2-Spring-Hibernate-Integration-Example.zip](19KB)
リファレンス
-
link://jsf2/jsf-2-0-spring-integration-example/[JSF 2.0 Spring
統合例]。 link://struts/struts-spring-hibernate-integration-example/[Struts
Spring Hibernate統合の例]