Tomcat
、
Struts
、
Hibernate
の環境構築:
1.JDKをインストールして、環境変数にJAVA_HOMEに「C:/Program Files/Java/jdk1.5.0」が設定されます。
2.TOMCATをインストールして。環境変数にTOMCAT_HOMEに「C:/Program Files/Apache Software Foundation/Tomcat 5.5」が設定されます。(jdkが1.5以上が必要)
3.環境変数のPATH変数に「%jAVA_HOME%/bin;%jAVA_HOME%/jre/bin;」が追加されます。
4.TOMCAT(
http://tomcat.apache.org/)のプラグイン(com.sysdeo.eclipse.tomcat_3.1.0.beta)をダウンロードして、
//eclipse/pluginsフォルダに入ります。
5.EclipseツールにTOMCATを設定して、ウィンドウ -> 設定にTOMCATタグを選択して、以下の設定通り:
Tomcatのバージョン: バージョン5.x
Tomcatホーム : Tomcaのインストールパース
6.EclipseでTomcatプロジェクトを作ります。ここでは、hibernate_Conという名前で作ります。 "server.xmlを更新する"をチェックするを忘れないように。
7.struts-1.2.7をダウンロードして、struts-blank.war(../struts-1.2.7/webapps/ struts-blank.war) の中の、WEB-INFディレクトリとindex.jspを、先ほど作ったhibernate_Conフォルダに上書きします(META-INFは不要です)。WEB-INFの下にweb.xmlや.tldファイルがあり、WEB-INF/libの下にstruts.jarがありますね。
8.hibernate-3.2.6.ga.zip(
http://www.hibernate.org/)をダウンロードして、展開したディレクトリ(hibernate3.jar)と、その下のlibディレクトリ(*.jar)から取り出して、 先ほど作ったhibernate_Con¥WEB-INF¥libの下にへ入ります。
9.Hibernateの設定ファイル、hibernate.cfg.xmlを書きます。hibernate展開したディレクトリに/etcフォルダにhibernate.cfg.xmlとlog4j.propertiesファイルを取り出して、先ほど作ったhibernate_Con/WEB-INF/srcの下に貼り付けます。
10. Oracle JDBC Driver ダウンロード(ojdbc14.jar): 先ほど作ったblueNet/WEB-INF/libフォルダに入っています。
http://www.oracle.com/technology/global/jp/software/tech/java/jdbc/index.html
Struts
アプリの作成:
① クラスパスの設定:プロジェクトを右クリック->プロパティー->Javaのビルド・パスを選択、ライブラリータブを選択して、Jarの追加ボタンを押す。strut.jar, hibernate3.jar, commons-collections-*.jar、commons-logging-*.jarを選択しOK。
② Actionクラスの作成:作るクラスはただ1つ。hibernate.TestAction.javaです。まずはクラスを作ります。
package hibernate;
public class HelloAction extends Action{}
次は、Actionクラスにperformメソッドを追加します。クラスの中身あたりで、右クリック->ソース->メソッドのオーバーライドを選択します。performが4つぐらいありますが、引数を確認して、execute (ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse)、引数にActionServletがなく、HttpServletRequestがあるもの、にチェックを入れ、OKしてください。次は、メソッドの中身を変更します。
package
hibernate;
public
class
TestAction
extends
Action {
public
ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws
Exception {
System.
out
.println(
"*****HibernateでDBへ連続します***********"
);
List list =(List)getEmployees();
request.setAttribute(
"EmployeeList"
,list);
return
(mapping.findForward(
"success"
));
}
public
List getEmployees() {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
String sql=
"from Employee"
;
Query query = session.createQuery(sql);
List list = query.list();
tx.commit();
HibernateUtil.closeSession();
return
list;
}
}
③
Hibernateへ連続するクラスの作成(hibernate/
HibernateUtil):
package
hibernate;
public
class
HibernateUtil {
private
static
Log
log
= LogFactory.getLog(HibernateUtil.
class
);
private
static
final
SessionFactory
sessionFactory
;
static
{
try
{
sessionFactory
=
new
Configuration().configure().buildSessionFactory();
}
catch
(Throwable ex) {
ex.printStackTrace();
throw
new
ExceptionInInitializerError(ex);
}
}
public
static
final
ThreadLocal
session
=
new
ThreadLocal();
public
static
Session currentSession()
throws
HibernateException {
Session s = (Session)
session
.get();
if
(s ==
null
||!s.isOpen()) {
s =
sessionFactory
.openSession();
session
.set(s);
}
return
s;
}
public
static
void
closeSession()
throws
HibernateException {
Session s = (Session)
session
.get();
session
.set(
null
);
if
(s !=
null
)
s.close();
}
}
④
DataBeanクラスの作成(hibernate/
Employee):
package
hibernate;
public
class
Employee {
private
int
id
;
private
String
neme
;
private
double
salary
;
private
String
year
;
public
int
getId() {
return
id
;
}
public
void
setId(
int
id) {
this
.
id
= id;
}
public
String getNeme() {
return
neme
;
}
public
void
setNeme(String neme) {
this
.
neme
= neme;
}
public
double
getSalary() {
return
salary
;
}
public
void
setSalary(
double
salary) {
this
.
salary
= salary;
}
public
String getYear() {
return
year
;
}
public
void
setYear(String year) {
this
.
year
= year;
}
}
⑤ Hibernateとデータベース関連の配置ファイル(hibernate/model.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
package=
"hibernate"
>
<class
name=
"hibernate.Employee"
table=
"XXX_EBS_TBL"
discriminator-value=
"C"
>
<id
name=
"id"
>
<generator
class=
"sequence"
/>
</id>
<property
name=
"neme"
/>
<property
name=
"salary"
/>
<property
name=
"year"
/>
</class>
</hibernate-mapping>
⑥ hibernate.cfg.xml配置ファイルの設置(/hibernate_Con/WEB-INF/src/hibernate.cfg.xml):
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- jdbc -->
<property
name=
"hibernate.connection.driver_class"
>
oracle.jdbc.driver.OracleDriver
</property>
<property
name=
"hibernate.connection.url"
>
jdbc:oracle:thin:@192.168.100.111:1521:prod
</property>
<property
name=
"hibernate.connection.username"
>
apps
</property>
<property
name=
"hibernate.connection.password"
>
apps
</property>
<property
name=
"hibernate.connection.pool_size"
>
100
</property>
<property
name=
"show_sql"
>
false
</property>
<property
name=
"dialect"
>
org.hibernate.dialect.Oracle10gDialect
</property>
<!-- Mapping files -->
<mapping
resource=
"hibernate/model.hbm.xml"
/>
</session-factory>
</hibernate-configuration>
⑦ 遷移先のJSPを作成:
<%@
page
language=
"java"
contentType=
"text/html; charset=windows-31j"
pageEncoding=
"windows-31j"
%>
<%@
taglib
prefix=
"logic"
uri=
"/tags/struts-logic"
%>
<%@
taglib
prefix=
"bean"
uri=
"/tags/struts-bean"
%>
<logic:present
name=
"EmployeeList"
property=
"list"
scope=
"request"
/>
<html>
<head>
<meta
http-equiv=
"Content-Type"
content=
"text/html; charset=windows-31j"
>
<title>
hello
</title>
</head>
<body>
<table>
<tr>
<logic:notEmpty
name=
"EmployeeList"
>
<logic:iterate
id=
"bean"
name=
"EmployeeList"
type=
"hibernate.Employee"
>
<tr>
<td><bean:write
name=
"bean"
property=
"id"
/></td>
<td><bean:write
name=
"bean"
property=
"neme"
/></td>
<td><bean:write
name=
"bean"
property=
"salary"
/></td>
<td><bean:write
name=
"bean"
property=
"year"
/></td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>
</body>
</html>
⑧ struts-config.xmlの編集:
<action-mappings>タグの中(下)に以下のactionを追加します。
<action
path=
"/hello"
type=
"hibernate.TestAction"
>
<forward
name=
"success"
path=
"/index.jsp"
/>
</action>
⑤ Tomcatを再起動し,以下のURLにアクセスします.