28 2月 2007

Struts2 Note -- (1)struts-tags.tld

Struts2發佈了2.0.6,下載後沒有注意到一件事,自2.0.6後的版本,struts-tags.tld要自 source code中經maven2 build後才會產生,所以找了老半天還有點火…,還好看了一下core/src/main/resources/META-INF/README.txt,才發現這個版本是要透過maven2才能產生struts-tags.tld,只好自行run mvn來做了,需要的話可以參考Building with Maven,遇上了OutOfMemoryError別忘了為Maven2加上個MAVEN_OPTS=-Xmx512m參數來執行。 我個人是有裝Maven2,所以問題不大,不過如果是希望下載後就能使用而且沒有需求使用Maven2的朋友,還是可能會感到些許不便吧。

27 2月 2007

First Struts2 Project with Eclipse and Maven2

一個基本的Eclipse Struts Project至少需要以下的設定 pom.xml
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.elliot</groupId>
 <artifactId>firststruts</artifactId>
 <packaging>war</packaging>
 <name>firststruts</name>
 <version>1.0-SNAPSHOT</version>
 <url>http://maven.apache.org</url>
 <build>
  <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
  <testSourceDirectory>
   ${basedir}/src/test/java
  </testSourceDirectory>
  <resources>
   <resource>
    <directory>${basedir}/src/main/resources</directory>
   </resource>
  </resources>
  <testResources>
   <testResource>
    <directory>${basedir}/src/test/resources</directory>
   </testResource>
  </testResources>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.5</source>
     <target>1.5</target>
    </configuration>
   </plugin>

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
     <warName>firststruts</warName>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-core</artifactId>
   <version>2.0.6</version>
  </dependency>
 </dependencies>
 <repositories>
  <repository>
   <id>Struts2</id>
   <name>Struts2</name>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
   <url>
    http://people.apache.org/repo/m2-snapshot-repository
   </url>
  </repository>
 </repositories>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>firststruts</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>
 <jsp-config>
  <taglib>
   <taglib-uri>/struts-tags</taglib-uri>
   <taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
  </taglib>
 </jsp-config>
</web-app>
resources還需要有struts.properties struts.devMode = true struts.enable.DynamicMethodInvocation = false 然後實做自己的Action extends from com.opensymphony.xwork2.ActionSupport 加入到struts.xml中即可 struts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="first" namespace="/" extends="struts-default">
        <action name="first" class="org.elliot.action.FirstAction">
            <result name="Success">/jsp/first/firstActionResult.jsp</result>
        </action>
    </package>
</struts>

26 2月 2007

Blogger 上顯示程式碼

一直都為了在Blogger上張貼程式的格式困擾,直到今天晃到這幾篇Blog [筆記]在文章裡顯示優質的程式碼區 at 良人的大秘寶
【Blog】文章中引用程式碼的作法 at 想要旅行的念頭停不住
才有了解決的方式,加入以下的CSS設定
CODE, .code  { 
  display: block; /* fixes a strange ie margin bug */
  font-family: Courier New; 
  font-size: 8pt; 
  overflow:auto; 
  background: #f0f0f0 url(http://chenensign.googlepages.com/BG_CODE.gif) left top repeat-y;
  border: 1px solid #ccc;
  padding: 10px 10px 10px 21px;
  max-height:200px;
  line-height: 1.2em;
}

CMD, .cmd {
  display: block; /* fixes a strange ie margin bug */
  font-family: Courier New; 
  font-size: 8pt; 
  overflow:auto; 
  color: #ccc;
  background: #000 url(http://chenensign.googlepages.com/BG_CMD.gif) left top repeat-y;
  border: 1px solid #ccc;
  padding: 10px 10px 10px 21px;
  max-height:200px;
  line-height: 1.2em;
}
這樣只要在程式碼區域前後加上 <pre class="code"> </pre>就可以顯示較為明顯的程式碼了!

08 2月 2007

Hibernate Annotations - One-to-One mappedBy的影響

One-to-One mappedBy的影響 Hibernate Annotations的說明
The association may be bidirectional. In a bidirectional relationship, one of the sides (and only one) has to be
the owner: the owner is responsible for the association column(s) update. To declare a side as not responsible
for the relationship, the attribute mappedBy is used. mappedBy refers to the property name of the association on
the owner side.
在Hibernate中雙向的Relationship的維護上,必定有一方為Owner來記錄此Relationship. 而mappedBy就是為了宣告其本身不負責記錄Relationship,交由另一方記錄所使用的annotation attribute. Bidirectional 的 One-to-One通常還是有主從的概念,以下列Person為主,Passport為從來看 (1)將mappedBy設在Person(從)上
public class Person implements Serializable {
 @Id
 @GeneratedValue(generator = "uuid-gen")
 @GenericGenerator(name = "uuid-gen", strategy = "uuid")
 @Column(length = 32)
 private String id;
 
 @OneToOne(cascade= CascadeType.ALL)
 private Passport passport;
}
public class Passport implements Serializable {
 @Id
 @GeneratedValue(generator = "uuid-gen")
 @GenericGenerator(name = "uuid-gen", strategy = "uuid")
 @Column(length = 32)
 private String identificationCode;
 
 @OneToOne(mappedBy="passport")
 private Person owner;
}
Create Table SQL
create table Passport (identificationCode varchar(32) not null, primary key (identificationCode))
create table Person (id varchar(32) not null, version integer, name varchar(50), firstName varchar(50), lastName varchar(50), mutable bit, birthday date, age integer, modifyDate timestamp, description longvarchar, passport_identificationCode varchar(32), primary key (id))
alter table Person add constraint FK8E488775EB061869 foreign key (passport_identificationCode) references Passport
Hibernate SQL Execution
Hibernate: insert into Person (version, name, firstName, lastName, mutable, birthday, age, modifyDate, description, passport_identificationCode, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select this_.id as id0_1_, this_.version as version0_1_, this_.name as name0_1_, this_.firstName as firstName0_1_, this_.lastName as lastName0_1_, this_.mutable as mutable0_1_, this_.birthday as birthday0_1_, this_.age as age0_1_, this_.modifyDate as modifyDate0_1_, this_.description as descrip10_0_1_, this_.passport_identificationCode as passport11_0_1_, passport2_.identificationCode as identifi1_1_0_ from Person this_ left outer join Passport passport2_ on this_.passport_identificationCode=passport2_.identificationCode
Hibernate: insert into Passport (identificationCode) values (?)
Hibernate: update Person set version=?, name=?, firstName=?, lastName=?, mutable=?, birthday=?, age=?, modifyDate=?, description=?, passport_identificationCode=? where id=? and version=?
Hibernate會將passport_identificationCode建在Person的Table中,而在Select時,會一併帶出Passport的資料 (2)將mappedBy設在Passport(主)上
public class Person implements Serializable {
 @Id
 @GeneratedValue(generator = "uuid-gen")
 @GenericGenerator(name = "uuid-gen", strategy = "uuid")
 @Column(length = 32)
 private String id;
 
 @OneToOne(mappedBy="owner",cascade= CascadeType.ALL)
 private Passport passport;
}
public class Passport implements Serializable {
 @Id
 @GeneratedValue(generator = "uuid-gen")
 @GenericGenerator(name = "uuid-gen", strategy = "uuid")
 @Column(length = 32)
 private String identificationCode;
 
 @OneToOne
 private Person owner;
}
Create Table SQL
create table Passport (identificationCode varchar(32) not null, owner_id varchar(32), primary key (identificationCode))
create table Person (id varchar(32) not null, version integer, name varchar(50), firstName varchar(50), lastName varchar(50), mutable bit, birthday date, age integer, modifyDate timestamp, description longvarchar, primary key (id))
alter table Passport add constraint FK4C60F032538A0EEB foreign key (owner_id) references Person
Hibernate SQL Execution
Hibernate: insert into Person (version, name, firstName, lastName, mutable, birthday, age, modifyDate, description, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select this_.id as id0_1_, this_.version as version0_1_, this_.name as name0_1_, this_.firstName as firstName0_1_, this_.lastName as lastName0_1_, this_.mutable as mutable0_1_, this_.birthday as birthday0_1_, this_.age as age0_1_, this_.modifyDate as modifyDate0_1_, this_.description as descrip10_0_1_, passport2_.identificationCode as identifi1_1_0_, passport2_.owner_id as owner2_1_0_ from Person this_ left outer join Passport passport2_ on this_.id=passport2_.owner_id
Hibernate: insert into Passport (owner_id, identificationCode) values (?, ?)
Hibernate: update Person set version=?, name=?, firstName=?, lastName=?, mutable=?, birthday=?, age=?, modifyDate=?, description=? where id=? and version=?
Hibernate會將owner_id建在Passport的Table中,而在Select時,不會一併帶出Passport的資料 可能的話,應該還是將mappedBy設在(主)的那一方上,讓(從)的一方記錄relationship的欄位. 否則的話就如同Component,不如就整在同一Table裡,還可以省去join的時間.