2008年10月28日 星期二

VC 2008 設定


Project > properties > configuration properties > C/C++
General裡面有可以設定include,如果有別的.h檔要包,可以在這邊設
Precompile header 可以設定取消 Precompile header

Project > properties > configuration properties > Linker
Input 可以設定.lib , 不管使用static或dynamic的時候都要在這邊設

Tool > option > Projects and Solutions >VC++ Directories
可以設定VC2008的預設Include和Library路徑

2008年10月24日 星期五

裝飾者模式(decorator)

比較類似包裝,一層層包裝起來
被包裝者與包裝者都繼承一個interface或抽象類別
被包裝的物件因此可以直接當原本的物件來用

包裝是用合成而得來的,不是用繼承
這裡用的繼承只是讓他們有同個father

2008年10月23日 星期四

觀察者模式(Observer)

一個一對多的關係
當資料在主題這邊有改變的時候,便會通知各個有訂閱的觀察者
而主題跟觀察者的關係是用interface連接起來
所以觀察者只要implement 這個interface就好
而在runtime的時候,可以加入訂閱新的觀察者
也可以取消訂閱現有觀察者,主題完全不會受到影響
由於鬆綁(Loose Coupling)的關係
主題不需要知道觀察者implement的細節

策略模式(strategy)

基礎且實用的一個Pattern
將以後可能會改變的部份分離封裝起來
並利用interface制定相連接的規格
之後每次改變只要去implement這個interface就可以了
而不同的implement也可以在runtime的時候作切換

oo守則: 多用合成,少用繼承
因為合成比起繼承來說,有更大的彈性

oo守則: 針對interface寫程式,而不是針對implement寫程式

2008年10月15日 星期三

custom tag



Classic custom Tag

有三個Inteface : Tag, IterationTag,BodyTag

在Tag中function被呼叫的順序
setPageContext()
setParent()
setter methods 有attribute的話
doStart() 作initial,且用EVAL_BODY跟SKIP_BODY決定是否作body
doEndTag() cleanupm且用EVAL_PAGE跟SKIP_PAGE決定是否作剩下的JSP
release() 全部結束,不再用到才會呼叫

如果有attribute:
要declare variable
要設定是不是mandatory,要有default value,要有setter

IterationTag
加入doAfterBody() EVAL_BODY_AGAIN跟SKIP_BODY

BodyTag
本來body內的東西只能選擇作或不做,BodyTag可以修改它
另外新增setBodyContent(), doInitBody(),EVAL_BODY_BUFFERED

2008年10月14日 星期二

C++ 程式最佳化技巧


unsigned類型用於:
  除法和餘數
  循環計數
  數組下標
signed類型用於:
  整數到浮點的轉化

for (;;) 比 while(1)來的好

避免不必要的整數除法

++i比i++好

local function盡量設為static






2008年10月2日 星期四

JSTL

 對presentation logic也可以reuse

Tag handler分為兩種: classic tag interface & Simple tag interface

使用tag library需要URI
URI有三種:
absolute http://xx.xx:8080/xx
root relative /xx/xx
Non-root relative xx/xx

如果是用JSP syntax:
<%@ taglib prefix="test" url="sampleLib.tld"%>

如果是用XML syntax:
<jsp:root
xmlns:jsp="http://..........."
xmlns:test="sampleLib.tld"
version="2.0">
JSP PAGE.....
< /jsp:root>

由於要避免一旦改了tld檔,每個jsp的路徑都得改
所以可以在web.xml設定

如果tld檔被放在JAR檔裡面的話,要取名為taglib.tld

taglib map
分為explicit mapping,implicit mapping,well-know mapping
explicit lib:
在web.xml裡面宣告
<taglib>
<taglib-uri>
http://xx.xx/studykit
</taglib-uri>
<taglib-location>
/myLibs/studyKit.tld 或 yourLibs/sample.jar
</taglib-location>

</taglib>
< >

mapping 次序

如果有找到對應的mapping
root relative就從application找起
non-root relative就從application/WEB-INF找起
如果在web.xml中沒找到對應的mapping
absolute就產生error
root relative就從application找起
non-root relative就從jsp檔案的地方找起

tag分為四種
Empty tag
tag with attributes
tag with JSP code (body)
tag with nested tag (switch & case)


JSP Standard Tag Library(JSTL)
下面有core,xml,fmt,sql,functions等library
這邊只介紹core
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

catch:
一般exception是送到error page,如果tag之間的code發生exception,會傳到var裡定義的variable
out:
有點像expression
set:
EL不能從各種scope中set跟remove attribute
所以可以用這個action來代替
可以使用
<c:set var="num" value="${4x4}"/>

<c:set var="num">
${4x4}
</c:set>
而用在Map或JavaBean的時候
<c:set target="customer1" property="zipcode">
44124
</c:set>

<c:set target="customer1" property="zipcode" value="44124"/>
remove:
<c:remove var="num" scope="session"/>
如果沒設scope,預設page->request->session->application
if:
用test來設定true和false,沒有 else
choose:
裡面包when跟otherwise,跟switch-case一樣
forEach:
兩種用法
第一種
用begin end step來做for迴圈
第二種
類似for (int element : collection)
只是變成
<c:forEach var="element" items="${collection}">
<c:set var="element" value="100"/>
</c:forEach>
forToken:
類似forEach,不過是用在String上,用delims隔開
參數有var,items,delims

url,import,redirect:
可以夾param在tag之間(c:param name=xx value=xx)

tld檔

taglib
tlib
jsp-version
short-name
uri 可以implicit設定uri,在JAR檔的狀況會自動產生mapping
tag 可以多個,也可以不同tag用同個class
name
tag-class
body-content
description
attribute
name
required 是否mandatory
rtexprvalue 是否可以用requestion time expression

body-content有三種
empty 中間不可以有東西,不過可以<></>
JSP 可以有text,html,script,action,或別的custom tag
tagdependent 用別的語言

JavaBean


JavaBean的條件
1.有個public且沒有argument的constructor
2.每個變數都有public getXXX和setXXX的function

如果有implement java.io.Serializable,就可以將JavaBean給serialize
副檔名會用.ser, 檔案會被放在/WEB-INF或其子目錄下
然後可以用java.beans.Beans.instantiate()來實例化

jsp:useBean
id 例如address
scope (預設是page scope)
class 例如BusinessAddress
type 例如AddressBean (可用父class或inteface)
beanName 例如businessData.John 或 AddressBean
搭配組合:
class
type
class 和 type
beanName 和 type
(beanName可以餵request time expression,class不行)

在用beanName的時候
還是會先check 設定的scope裡面有沒有要的bean(所以才需要type)
沒有的話,才去檔案裡找

只用type的話
如果依據id找到的bean不合type,會丟出ClassCastException
如果找不到,會丟出java.lang.InstantiaionException

用jsp:useBean包起來的指令,可用來作initial
這些指令只有在scope 裡找不到bean的時候,才會執行

jsp:setProperty
name 同useBean的id
property property名稱
value 手動assign
param 從request得到

value和param不能同時有
都沒有的話代表用跟property同名的param
此時也可以將property設為* , 則會從request中設所有的property
如果request中找不到,則就沒事發生

jsp:getProperty
只有name跟property
jsp:getProperty只能用來print
如果要用在程式中或request time expression
只能透過Java的function來呼叫,也就是getXXX()

由於request傳遞的parameter只能是String
所以如果JavaBean裡面不是String的話,jsp engine會自動作轉換
除此之外,一般的狀況也會作自動轉換
但對於request time expression, javaBean就不提供自動轉換的功能

而JavaBean也可以接受array
一般request裡面的parameter有多個value的時候
Javabean若是一般variable,便會只接受第一個
要是是Array,便會全部接受
(String to Char的轉換是只取String的第一個字母)
而將array get出來則只會print出array的位址
(這時就得用Java的function來呼叫)

2008年10月1日 星期三

EL in JSP

EL是用 ${vara} 來print出vara
可是EL不能declare variable
只能用現有的implicit varialbe

要用

EL的implicit variable:
pageContext
pageScope
requestScope
sessionScope
applicationScope
param
paramValues
header
headerValues
cookies
除了pagecontext以外,都是Map
它並沒有給直接的access到HttpSession等...

EL operators
${header["host"]} 跟
${header['host']} 跟
${header.host} 是一樣的

tag library descriptor
要用function的話,要用tld
裡面包括
function
name
function-class
function-signature

然後在JSP裡面用directive的taglib prefix="myString" uri="URL....."
便可以用${myString:functionName()}來呼叫