亚洲成人av在线电影,亚洲精品在线观,在线视频中文字幕一区二区http://www.aygfsteel.com/bourn/category/25422.html 反者道之動,弱者道之用。  天下萬物生于有,有生于無。zh-cnTue, 04 Sep 2007 17:08:22 GMTTue, 04 Sep 2007 17:08:22 GMT60JDK5 介紹http://www.aygfsteel.com/bourn/articles/142537.html段氏段氏Tue, 04 Sep 2007 02:37:00 GMThttp://www.aygfsteel.com/bourn/articles/142537.htmlhttp://www.aygfsteel.com/bourn/comments/142537.htmlhttp://www.aygfsteel.com/bourn/articles/142537.html#Feedback0http://www.aygfsteel.com/bourn/comments/commentRss/142537.htmlhttp://www.aygfsteel.com/bourn/services/trackbacks/142537.html 本文中,我們將討論幾個在JDK1.5中新的語言特征,包括:

泛型(Generics)--為集合(collections)提供編譯時類型安全,無需每刻從Collections取得一個對象就進行強制轉換(cast)

增強的“for”循環(Enhanced For loop)--減少迭代器(iterator)的潛在錯誤(error-proneness)

自動置入/自動取出(Autoboxing/unboxing)--無需在基本類型(primitive types)(例如double)和包裝類型(wrapper types)(例如Double)之間人工地進行轉換。

類型安全的枚舉(Typesafeenums)--提供類型安全枚舉模式的各項好處。

靜態導入(Static import)--無需在使用其他類的靜態成員變量前綴其類名.這將使得代碼更為簡潔。

元數據(Metadata)--使編程人員避免編寫樣板化代碼(boiler plate code),并提供機會進行宣告式程式設計(declarative programming)。

讓我們詳細討論每個新特性,并看一些例子。

泛型(Generics)

泛型是JDK1.5中一個最“酷”的特征。通過引入泛型,我們將獲得編譯時類型的安全和運行時更小地拋出ClassCastExceptions的可 能。在JDK1.5中,你可以聲明一個集合將接收/返回的對象的類型。在JDK1.4中,創建雇員名字的清單(List)需要一個集合對象,像下面的語 句:

List listOfEmployeeName = new ArrayList();

在JDK1.5中,你將使用下面語句

List<String> listOfEmployeeName = new ArrayList<String>();

最“酷”的是,如果你試圖插入非string類型的值,你將在編譯時發現并且修正這類問題。沒有泛型,你會發現這樣一個bug,當你的客戶調用后會告訴你,你所編寫的程序拋出ClassCastException異常而崩潰。

另外,當你從集合中得到一個元素時你無需進行強制轉換。故原先為:

String employeeName = ((String) listOfEmployee.get(i));

而下面的語句將比上面的更加簡單:

String employeeName = listOfEmployee.get(i);

不清楚對象的類型而強制轉換對象是不合理的,并且更重要的是,它將在運行時失敗。假使用戶無意間傳入一個包含string buffers類型而非string類型的集合,那結果會怎樣呢。在Listing A中,客戶被要求傳入一個編譯器無法強制的strings類型集合。Listing B中顯示了同樣的方法使用泛型是如何實現的。

Listing A

staticbooleancheckName(Collection employeeNameList, String name) {

for (Iteratori = employeeNamList.iterator(); i.hasNext(); ) {

String s = (String) i.next();

if(s.equals(name)){

return true;

//print employee name here ......

}

}

return false;

}

Listing B

staticbooleancheckName(Collection<String> employeeNameList, String name) {

for (Iteratori = employeeNamList.iterator(); i.hasNext(); ) {

if(i.next().equals(name)){

return true;

//print employee name here ......

}

}

return false;

}

現在,通過方法簽名可以清楚知道輸入集合必須只能包含strings。如果客戶試圖傳入一個包含string buffers的集合,程序將不會編譯。同時注意,該方法不包含任何強制轉換。它只需要短短一行,一旦你習慣泛型后,它也更加清晰。

在JDK當前版本下的For循環語法如下:

void printAll(Collection c) {

for (Iteratori = c.iterator(); i.hasNext(); ) {

Employee emp = (Employee)i.next();

System.out.println(emp.getName());

}

}

現在,用增強的For語句實現相同方法:

voidprintAll(Collection c) {

for (Object o : c)

System.out.println((TimerTask)o).getName());

}

在這類For循環中,你應該將":"看成"in",所以,在該例中可以看成"for Object o in c"。你可以發現這種For循環更具可讀性。

自動置入/自動取出(Autoboxing/unboxing)

Java有基本數據類型,在這些基本數據類型周圍又有包裝類。通常,編程人員需要將一種類型轉換成另一種。看看Listing C.中的代碼片斷。

Listing C

public class Employee {

private static final Integer CHILD = new Integer(0);

public static void main(String args[]) {

//code for adding n to an Integer

int n=10;

Integer age= new Integer(30);

Integer ageAfterTenYear= new Integer(age.intValue +10);

}

}

請注意,用于計算ageAfterTenYear的內循環代碼看上去是多么雜亂。現在,在Listing D.中看看相同的程序使用autoboxing重寫后的樣子。

Listing D

public class Employee {

public static void main(String args[]) {

int n=10;

Integer age= new Integer(30);

Integer ageAfterTenYear= age +10;

}

}

有一件事值得注意的:在先前,如果你取出(unbox)Null值,它將變為0。在次代碼中,編譯器將自動地轉換Integer為int然后加上10,接著將其轉換回Integer.。

類型安全的枚舉(Typesafeenums)

類型安全枚舉提供下列特性:

他們提供編譯時類型安全。

他們都是對象,因此你不需要將他們放入集合中。

他們作為一種類的實現,因此你可以添加一些方法。

他們為枚舉類型提供了合適的命名空間。

他們打印的值具有情報性(informative)― 如果你打印一個整數枚舉(intenum),你只是看見一個數字,它可能并不具有情報性。

例一:

enum Season { winter, spring, summer, fall }

例二:

public enum Coin {

penny(1), nickel(5), dime(10), quarter(25);

Coin(int value) { this.value = value; }

private final int value;

public int value() { return value; }

}

靜態導入(Static import)

靜態導入使代碼更易讀。通常,你要使用定義在另一個類中的常量(constants),像這樣:

importorg.yyy.pkg.Increment;

class Employee {

public Double calculateSalary(Double salary{

return salary + Increment.INCREMENT * salary;

}

}

當時使用靜態導入,我們無需為常量名前綴類名就能使用這些常量,像這樣:

import static org.yyy.pkg.Increment;

class Employee {

public Double calculateSalary(Double salary{

return salary + INCREMENT * salary;

}

}

注意,我們可以調用INCREMENT這一常量而不要使用類名Increment.。

元數據(Metadata)

元數據特征志于使開發者們借助廠商提供的工具可以進行更簡易的開發。看一看Listing E.中的代碼。

Listing E

importorg.yyy.hr;

public interface EmployeeI extends Java.rmi.Remote {

public String getName()

throwsJava.rmi.RemoteException;

public String getLocation ()

throwsJava.rmi.RemoteException;

}

public class EmployeeImpl implements EmployeeI {

public String getName(){

}

public String getLocation (){

}

}

通過元數據的支持,你可以改寫Listing E中的代碼為:

importorg.yyy.hr;

public class Employee {

@Remote public String getName() {

...

}

@Remote public public String getLocation() {

...

}

}

正像你所看到的,所有樣板化的代碼都不見了。

這些新特性和規格說明將在JDK1.5中實現。它將提供Java編程社區更多的選擇以編寫魯棒的、可擴展的代碼。認真的Java編程人員將感到著手去熟悉這一Java編程語言的新版本是很有益的。
本文來自:http://www.linuxpk.com/43851.html
 Java2標準版(Java 2 Platform, Standard Edition, J2SE)1.5即將正式推出,這一次的版本更新不同于以往,它帶來了很多里程碑式的革新,SUN將其綽號取名為“虎”。這一次的變革將是Java誕生以 來從未有過的,它給我們帶來了耳目一新的感覺。下面我們就來欣賞一下其中的部分典型變化:

1.自動包裝和解包(Autoboxing and unboxing)

代碼示例

往一個ArrayList中加入一個整數,1.5版本以前的版本寫法是:

List list = new ArrayList();

list.add( new Integer( 10 ) );

而在1.5版本中可以寫為:

list.add( 10 );

因為,在1.5版本中,對一個整數進行包裝,使之成為一個Integer對象(即包裝,boxing),然后加入到一個ArrayList中的做法被認 為是沒有必要的,反之,解包(unboxing)的做法也是沒有必要的,這樣的代碼只是增加了程序的文本長度而已,所以1.5版本支持了自動包裝和解包操 作,對于bool/Boolean,byte/Byte,double/Double,short/Short,int/Integer, long/Long,float/Float的相應包裝/解包操作都進行了支持,從而使代碼變得簡單。

2.更優化的循環語句(The inhanced for loop)

代碼示例

一個典型的遍歷數組的循環語句,1.5版本以前的寫法是:

for ( Iterator iterator = list.iterator(); iterator.hasNext(); )

{

Integer n = (Integer)iterator.next();

...

}//for

而在1.5版本中可以寫為:

for ( Integer n : list )

{

...

}//for

顯然1.5版本的寫法比以前是大大簡化了,但是在需要修改集合,比如刪除其中元素時不能采用這種寫法。之所以Java1.5版本沒有象C#那樣干脆定義 一個foreach關鍵詞,主要是因為SUN認為增加一個專門的關鍵詞成本太高了(too costly)。但1.4版本中就曾經增加了assert關鍵詞,1.5版本中也新增加了enum關鍵詞,因此這一解釋恐怕并不那么令人信服。

3.參數可變的方法和printf

代碼示例

當不能確定一個方法的入口參數的個數時,以往版本的Java中,通常的做法是將多個參數放在一個數組或者對象集合中作為參數來傳遞,1.5版本以前的寫法是:

int sum(Integer[] numbers)

{

int nSum = 0;

for(int i: numbers)

nSum += i;

return nSum;

}

...

//在別處調用該方法

sum(new Integer[] {12,13,20});

而在1.5版本中可以寫為:

int sum(Integer... numbers)

{

int nSum = 0;

for(int i: numbers)

nSum += i;

return nSum;

}

...

//在別處調用該方法

sum(12,13,20);

顯然,1.5版本的寫法更為簡易,也更為直觀,尤其是方法的調用語句,不僅簡化很多,而且更符合通常的思維方式,更易于理解。

1.5版本自身就有一個應用該特征的典型例子,即C風格的格式化輸出方法――printf。

代碼示例

輸出一個加法算式,1.5版本以前的寫法是:

int x = 5;

int y = 7;

int nSum = x + y;

System.out.println(x + " + " + y + " = " + nSum);

而在1.5版本中可以寫為:

System.out.printf("%d + %d = %d"n", x, y, nSum);

以上兩種寫法的輸出結構是一樣的,即“5 + 7 = 12”。

這種改變不僅僅是形式上的,printf還可以提供更為靈活、強大的輸出功能,比如限定按照兩位整數的形式輸出,可以寫為 “System.out.printf("%02d + %02d = %02d"n", x, y, nSum);”,輸出結果將是“05 + 07 = 12”。

4.枚舉

代碼示例

構建一個表示色彩的枚舉,并賦值,在1.5版本中可以寫為:

public enum MyColor{ Red, Yellow, Blue }

MyColor color = MyColor.Red;

for ( MyColor mycolor : MyColor.values() )

System.out.println( mycolor );

以往的Java版本中沒有enum關鍵詞,1.5版本中終于加入了進來,這確實是一個令人高興的改進。此外,enum還提供了一個名為values() 的靜態方法,用以返回枚舉的所有值的集合。所以,以上程序的輸出結果是把“Red”、“Yellow”、“Blue”分行輸出。

而 enum提供的靜態方法valueOf()則將以字符串的形式返回某一個具體枚舉元素的值,比如“MyColor.valueOf(“Red”)”會返回 “Color.Red”。靜態方法name()則返回某一個具體枚舉元素的名字,比如“MyColor.Red.name()”會返回“Red”。類似的 方法還有不少。此外,enum自身還可以有構造方法。

5.靜態引用

代碼示例

當我們要獲取一個隨即數時,1.5版本以前的寫法是:

import java.lang.Math; //程序開頭處

...

double x = Math.random();

而在1.5版本中可以寫為:

import static java.lang.Math.random; //程序開頭處



double x = random();

靜態引用使我們可以象調用本地方法一樣調用一個引入的方法,當我們需要引入同一個類的多個方法時,只需寫為“import static java.lang.Math.*”即可。這樣的引用方式對于枚舉也同樣有效。

6.總結

以上對J2SE1.5的部分新特征做了一些簡單的介紹。總而言之,1.5版本的Java確實給我們帶來了很多令人激動的變革,如同以上介紹的那樣,很多功能以前的版本也能實現,但是不能實現得這樣簡單、漂亮。相信這次變革會給Java帶來更多的追隨者。
本文來自:http://www.linuxpk.com/43860.html



段氏 2007-09-04 10:37 發表評論
]]>
Code Engine基本技術demo2http://www.aygfsteel.com/bourn/articles/142384.html段氏段氏Mon, 03 Sep 2007 09:12:00 GMThttp://www.aygfsteel.com/bourn/articles/142384.htmlhttp://www.aygfsteel.com/bourn/comments/142384.htmlhttp://www.aygfsteel.com/bourn/articles/142384.html#Feedback0http://www.aygfsteel.com/bourn/comments/commentRss/142384.htmlhttp://www.aygfsteel.com/bourn/services/trackbacks/142384.html關于如何生成字段的編輯頁面;

1.       首先應該讀取domain對象,從而獲取field列表;

    /**

     * getDomainFields via jsp name, and should remove the super fields, like lastUpdatedBy, lastUpdatedDate

     * @param name of jsp

     * @return list of field

     */

    public static List<Field> getDomainFields(String name) {

        List<Field> ret = new ArrayList<Field>();

        try {

            Class clz = Class.forName(CodeEngineConfig.getDomainPackage() + "." +

                    CodeEngineConfig.getControllerDomain(CodeEngineConfig.getJspRef(name)));

            for (Field field : clz.getDeclaredFields()) {

                if(!"lastUpdateDate".equalsIgnoreCase(field.getName())

                        && !"lastUpdateBy".equalsIgnoreCase(field.getName())){

                ret.add(field);

//                System.out.println("field.getName() + field.getType() = " + (field.getName() + field.getType()));

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

        return ret;

    }

2.       Field List傳遞給freemarker模板;之所以要傳遞field而不僅僅是name的原因是,因為希望在模板里根據fieldtype,從而生成不同的html,比如date可以生成使用js calendar的,boolean生成radio,或checkbox,其他的生成text

a)         定義macro

<#macro getHtml field>

 <#if field.type?ends_with('Boolean')>

<input type="radio" id="${field.name}" name="${field.name}" class="inp_txt_30" size="80" value="${'$'}{${domain?uncap_first + '.' + field.name}}">

 <#elseif field.type?ends_with('Date')>

<input type="text" id="${field.name}" name="${field.name}" readonly="readonly" class="inp_txt_30" size="17" value="<fmt:formatDate value="${'$'}{${domain?uncap_first + '.' + field.name}}" pattern="yyyy-MM-dd hh:mm"/>">

<button id="${field.name}Btn" class="button" >...</button>

<script type="text/javascript">

Calendar.setup(

{

inputField : "${field.name}",      // id of the input field

ifFormat    : "%Y-%m-%d %H:%M",       // the date format

showsTime: true,

button      : "${field.name}Btn"    // id of the button

}

);

</script>

 <#elseif field.type?ends_with('Text')>

<textarea id="${field.name}" name="${field.name}" rows="15" cols="80" style="width: 100%">${'$'}{${domain?uncap_first + '.' + field.name}}</textarea>

 <#else>

<input type="text" id="${field.name}" name="${field.name}" class="inp_txt_30" size="80" value="${'$'}{${domain?uncap_first + '.' + field.name}}">

 </#if>

</#macro>

       b)調用macro生成html

            <#list fields! as field>

              <tr>

                  <td class="txt_tit_s" ><spring:message code="${lbl + '.' + domain?uncap_first + '.' + field.name}"/></td>

                  <td><@getHtml field=field/></td>

              </tr>

            </#list>



段氏 2007-09-03 17:12 發表評論
]]>
開源框架的選型http://www.aygfsteel.com/bourn/articles/142111.html段氏段氏Sun, 02 Sep 2007 11:23:00 GMThttp://www.aygfsteel.com/bourn/articles/142111.htmlhttp://www.aygfsteel.com/bourn/comments/142111.htmlhttp://www.aygfsteel.com/bourn/articles/142111.html#Feedback0http://www.aygfsteel.com/bourn/comments/commentRss/142111.htmlhttp://www.aygfsteel.com/bourn/services/trackbacks/142111.html如果你想用開源框架來開發web項目,選擇一套適用的組件是很關鍵的;

不光要考慮開發的簡單,還要考慮日后的升級;

甚至足夠充分的文檔資料支持;還有現有團隊的技術能力;以及項目時間等;

MVC:第一要素我個人覺得是要簡單,因為在這個部分的中的代碼量,通常相對后端是很多的;一個容易上手,并且大家都熟悉并且不討厭;

           SpringMVC,我個人覺得是很完備的mvc,有著很強的靈活性,但正是這種靈活性,讓很多人無所適從;

           Struts 1 標簽很糟糕;form對象很別扭,繁瑣的配置;

           Struts2 沒有用過,如果他還有form我就不打算用;

學習springside(以前)使用Spring MultiActionController,減少了很多沒有必要的配置;在一個controller里面可以寫多個ActionMultiActionController還可以很靈活的從request中綁定Domain對象,非常的方便;

MultiActionController Controller可以滿足全部的需要;

JSP部分使用spring form tag

Tiles sitemesh 考慮到使用Ajax,而sitemesh是利用filter來修飾;選擇Tiles

ORMibatis,當前最實用,簡單的ORM;而且可以自動生成,又容易理解;何樂而不為;

FullTextSearch compass + lucene

OthersActiveMQ + ApacheCXF

Form/Ajax Request > Controller > Manager/Service > GenericDao

其他輔助工具:

EMS for mysql

SVN as version control

DB/web servermysql/resin



段氏 2007-09-02 19:23 發表評論
]]>
實際項目中的Ajaxhttp://www.aygfsteel.com/bourn/articles/141683.html段氏段氏Fri, 31 Aug 2007 04:12:00 GMThttp://www.aygfsteel.com/bourn/articles/141683.htmlhttp://www.aygfsteel.com/bourn/comments/141683.htmlhttp://www.aygfsteel.com/bourn/articles/141683.html#Feedback0http://www.aygfsteel.com/bourn/comments/commentRss/141683.htmlhttp://www.aygfsteel.com/bourn/services/trackbacks/141683.html

上一個項目使用的是spring MVC 客戶需要做Ajax應用;所以就找了一些資料研究了一下,比如DWRdojo prototypeJSON-RPC, trimpath 等等,發現很多不適合我們,比如DWR要生成客戶端js,服務器端還要部署,麻煩;dojo又太慢了;經過一輪淘汰剩下了prototypetrimpath;所以最終就選這2個了;

Prototype在書寫普通的js時候,有很多好處,比如簡單,實用的很多函數;比如$()系列;

Trimpath提供一個客戶端的js模板,如果從服務器回來的數據很復雜,要動態改變Html元素是比較費力的事情;用trimpath就方便許多;

在模板語言的世界里,總有2個東西:模板和模板中的數據;trimpath的模板接受的數據是javascript object,模板則定義在一個不顯示的textarea里面;

所以有個問題就是:怎么讓ajax調用返回一個javascript對象?

后來,我終于發現了(想起了劉若英)JSON;發現json是個好東東;比xml輕量級,又可以很容易的轉換為javascript對象,而且還有java api;唉,開源的世界多美妙;

所以解決方案就是,在springmvc框架中,用response返回json string,給ajax 客戶端,然后生成javascript對象,然后,調用trimpath模板,然后,動態修改頁面。

代碼片段:

    public ModelAndView getClient(HttpServletRequest request, HttpServletResponse response) throws Exception {

        JSONObject jsonObject = new JSONObject();

        Client client=clientMgr.getClientByPk(Long.parseLong(request.getParameter("clientId")));

        jsonObject.add("client", client);

        return ajaxResponse(jsonObject, response);

    }

    protected ModelAndView ajaxResponse(JSONObject jsonObject, HttpServletResponse response) throws Exception {

        response.setContentType("application/x-json;charset=UTF-8");

        response.getWriter().print(jsonObject);

        return null;

    }



段氏 2007-08-31 12:12 發表評論
]]>
Code Engine基本技術demohttp://www.aygfsteel.com/bourn/articles/141672.html段氏段氏Fri, 31 Aug 2007 03:38:00 GMThttp://www.aygfsteel.com/bourn/articles/141672.htmlhttp://www.aygfsteel.com/bourn/comments/141672.htmlhttp://www.aygfsteel.com/bourn/articles/141672.html#Feedback0http://www.aygfsteel.com/bourn/comments/commentRss/141672.htmlhttp://www.aygfsteel.com/bourn/services/trackbacks/141672.htmlCode Engine 主要用來生成web相關的代碼,可以把orm的部分集成進來,形成一條完整的生產線;

利用xpathfreemarker等技術,使得用戶通過xml配置文件,模板等簡單的方式,快速開發代碼和根據需求調整模板;

Dom4j對實現了xpath 1.0 非常的好用;

所需jar dom4j-1.6.1.jar jaxen-1.1-beta-7.jar

Code

import junit.framework.TestCase;

import org.dom4j.*;

import org.dom4j.io.SAXReader;

import java.util.List;

import java.util.Iterator;

import java.io.File;

/**

 * Created by IntelliJ IDEA.

 * User: duanbin

 * Date: 2007-8-15

 * Time: 9:47:17

 * To change this template use File | Settings | File Templates.

 */

public class XPathTest extends TestCase {

    public void testXPathViaDom4jXpathV1() {

        printSelectedNodeValue("D:""xpath""src""test.xml");

    }

    /**

     * 利用XPath操作XML文件,打印指定節點或者屬性的值, using xpath 1.0

     *

     * @param filename String 待操作的XML文件(相對路徑或者絕對路徑)

     */

    public void printSelectedNodeValue(String filename) {

        try {

            SAXReader saxReader = new SAXReader();

            Document document = saxReader.read(new File(filename));

            List list;

            list = document.selectNodes("http://qn1:college/@leader[.!='leader1']");

            for (Object aList : list) {

                Attribute attribute = (Attribute) aList;

                System.out.println("http://qn1:college/@leader: " + attribute.getValue());

            }

            //打印所有student節點的屬性age值,如果有的話

//            list = document.selectNodes("/students/student/@age");

//            for (Object aList : list) {

//                Attribute attribute = (Attribute) aList;

//                System.out.println("/students/student/@age" + attribute.getValue());

//            }

            //打印所有college節點值,如果有的話

//            list = document.selectNodes("/students/student");

//            for (Object aList1 : list) {

//                Element bookElement = (Element) aList1;

//                Iterator iterator = bookElement.elementIterator("college");

//                while (iterator.hasNext()) {

//                    Element titleElement = (Element) iterator.next();

//                    System.out.println("/students/student/college" + titleElement.getText());

//                }

//            }

            //測試節點的一些方法

//            list = document.selectNodes("http://city");

//            for (Object aList2 : list) {

//                Element titleElement = (Element) aList2;

//                System.out.print("http://telephonegetName:" + titleElement.getName());

//                System.out.print(" ##getNodeType:" + titleElement.getNodeType());

//                System.out.print(" ##getTextTrim:" + titleElement.getTextTrim());

//                System.out.print(" ##getNamespaceURI:" + titleElement.getNamespaceURI());

//                System.out.print(" ##getNodeTypeName:" + titleElement.getNodeTypeName());

//                System.out.print(" ##getQualifiedName:" + titleElement.getQualifiedName());

//                System.out.print(" ##getUniquePath:" + titleElement.getUniquePath());

//                System.out.println(" ##getPath:" + titleElement.getPath());

//            }

            //打印所有name節點值,如果有的話,與上面college的取法不一樣

//            list = document.selectNodes("/students/student/name");

//            Iterator iter = list.iterator();

//            for (Object aList3 : list) {

//                Element titleElement = (Element) aList3;

//                System.out.println("/students/student/name" + titleElement.getText());

//            }

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }

}

Test.xml:

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

<students xmlns:qn1="http://qn1.com">

    <student age="259911911911"><!--如果沒有age屬性,默認的為20-->

        <qn1:name>崔衛兵</qn1:name>

        <college>PC學院</college>

        <telephone>62354666</telephone>

        <notes>,1982年生,碩士,現就讀于北京郵電大學</notes>

        <addr><city>Beijing</city></addr>

    </student>

    <student>

        <name>cwb</name>

        <qn1:college leader="學院領導">PC學院</qn1:college><!--如果沒有leader屬性,默認的為leader-->

        <telephone>62358888</telephone>

        <notes>,1987年生,碩士,現就讀于中國農業大學</notes>

    </student>

    <student>

        <name>xxxxx</name>

        <college leader="">xxx學院</college>

        <telephone>66666666</telephone>

        <notes>注視中,注釋中</notes>

    </student>

    <student age="9911911911">

        <name>yyyyyy</name>

        <qn1:college leader="leader1">yyyy學院</qn1:college>

        <telephone>88888888</telephone>

        <notes>注視中111,注釋中222</notes>

    </student>

</students>

Freemarker基本示例:

import freemarker.template.Configuration;

import freemarker.template.DefaultObjectWrapper;

import freemarker.template.Template;

import java.io.File;

import java.io.Writer;

import java.io.OutputStreamWriter;

import java.io.FileOutputStream;

import java.util.Map;

import java.util.HashMap;

import org.apache.log4j.Logger;

/**

 * Created by IntelliJ IDEA.

 * User: duanbin

 * Date: 2007-8-21

 * Time: 22:30:13

 * To change this template use File | Settings | File Templates.

 */

public abstract class BaseGenerator {

    protected Configuration cfg;

    protected final Logger logger = Logger.getLogger(this.getClass());

    public BaseGenerator() {

        try {

            cfg = getConfiguration(CodeEngineConfig.getTemplateDir());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public abstract void generate();

    protected void generateFile(String template, Map root, String fileName){

           try {

                File dist = new File(fileName);

                FileOutputStream fos = new FileOutputStream(dist);

                Writer out = new OutputStreamWriter(fos);

    //            Writer out2 = new OutputStreamWriter(System.out);

                cfg.getTemplate(template).process(root, out);

                out.flush();

                out.close();

                logger.info("Generated File: " + fileName);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    protected Configuration getConfiguration(String tempDir) throws Exception {

        Configuration cfg = new Configuration();

        // Specify the data source where the template files come from.

        // Here I set a file directory for it:

        cfg.setDirectoryForTemplateLoading(

                new File(tempDir));

        // Specify how templates will see the data model. This is an advanced topic...

        // but just use this:

        cfg.setObjectWrapper(new DefaultObjectWrapper());

        return cfg;

    }

}

模板文件示例:

package ${package}.application.web;

import biz.web.framework.web.BaseController;

<#list managers! as mgr>

import ${package}.application.manager.${mgr};

</#list>

<#list services! as svc>

import ${package}.application.service.${svc};

</#list>

/**

 * Created by IntelliJ IDEA.

 * User: ${author!'admin'} "${r"${build.dir}"} "${'$'}{cfg.startDate}

 * Date: Sep 1, 2006

 * Time: 9:19:17 AM

 * To change this template use File | Settings | File Templates.

 */

public class ${name}Controller extends BaseController {

<#list managers! as mgr>

    protected ${mgr} ${mgr?uncap_first};

</#list>

<#list services! as svc>

    protected ${svc} ${svc?uncap_first};

</#list>

<#list managers! as mgr>

    public void set${mgr}(${mgr} ${mgr?uncap_first}) {

        this.${mgr?uncap_first} = ${mgr?uncap_first};

    }

</#list>

<#list services! as svc>

    public void set${svc}(${svc} ${svc?uncap_first}) {

        this.${svc?uncap_first} = ${svc?uncap_first};

    }

</#list>

     public ModelAndView list${name}(HttpServletRequest request, HttpServletResponse response) throws Exception {

        ModelAndView mav = new ModelAndView("update_${name?uncap_first}");

        Long user = getUserId();

        mav.addObject("userId", user);

        return mav;

    }

}



段氏 2007-08-31 11:38 發表評論
]]>
關于使用Tomcat Manager來deploy一個應用http://www.aygfsteel.com/bourn/articles/141548.html段氏段氏Thu, 30 Aug 2007 15:06:00 GMThttp://www.aygfsteel.com/bourn/articles/141548.htmlhttp://www.aygfsteel.com/bourn/comments/141548.htmlhttp://www.aygfsteel.com/bourn/articles/141548.html#Feedback0http://www.aygfsteel.com/bourn/comments/commentRss/141548.htmlhttp://www.aygfsteel.com/bourn/services/trackbacks/141548.html

初始的tomcat 5.0.28是無法使用manager
app的,因為沒有權限,找到/conf/tomcat-users.xml;
打開后編輯如下兩句,保存并啟動tomcat,
請求http://localhost:8080,
使用用戶名和密碼都是tomcat登錄即可;

加入:<role rolename="manager"/>
修改:<user username="tomcat" password="tomcat" roles="tomcat"/> 為
      <user username="tomcat" password="tomcat"
roles="tomcat,manager"/>




段氏 2007-08-30 23:06 發表評論
]]>
中文問題http://www.aygfsteel.com/bourn/articles/141546.html段氏段氏Thu, 30 Aug 2007 15:04:00 GMThttp://www.aygfsteel.com/bourn/articles/141546.htmlhttp://www.aygfsteel.com/bourn/comments/141546.htmlhttp://www.aygfsteel.com/bourn/articles/141546.html#Feedback0http://www.aygfsteel.com/bourn/comments/commentRss/141546.htmlhttp://www.aygfsteel.com/bourn/services/trackbacks/141546.html

之前一直以為我們搞定了中文,所以一直沒有懷疑我們的配置,今天和立國發現一個問題:
就是用ajax的方式去創建一個記錄,然后用jsp的方式去取數據,出現亂碼;
但是用ajax的方式取此記錄,正常;
后來發現,我們提交數據的方式,不管是ajax的,還是form表單提交,所使用的編碼通通沒有指定!!,雖然我們在頁面上加上了<%@
page contentType="text/html;charset=UTF-8" language="java"
%>,但這一句主要是負責response的數據顯示;
查了資料后才發現需要加上:<%@ page  pageEncoding="utf-8"
%>;
而之前我們都沒有指定request的charset,所以按照servlet標準,大多數web
server(Resin,Tomcat)默認按照iso-8859-1來處理,而我們的數據庫是utf-8的,所以放到數據庫中的數據并不是utf-8的;所以用jsp顯示時候出錯;但用ajax的方式為什么沒出錯,還沒有搞明白;
解決方案:
1.ajax方式
#1:prototype.js [line707]contentType:
'application/x-www-form-urlencoded;charset=UTF-8',
#2:BaseController [line135] response.setCharacterEncoding("UTF-8");
以上兩行保證了ajax請求和相應的方式一致,并都是utf8;
2.form表單方式;
#3:在jsp加上:<%@ page  pageEncoding="utf-8" %>;
#4:和<%@ page contentType="text/html;charset=utf-8" language="java"
%>
3.我在web。xml中加了一個encodefilter,保證了當請求中沒有指定charset的時候,使用utf-8方式,所以以上#1和#3處的指定charset是可選的!!
請大家注意更新以上3個文件;prototype.js,BaseController
.java, web.xml

 

 

同意以上所述,更新3個文件:prototype.js,
BaseController.java, web.xml ,頂。




段氏 2007-08-30 23:04 發表評論
]]>
Resin下debug的問題http://www.aygfsteel.com/bourn/articles/141545.html段氏段氏Thu, 30 Aug 2007 15:02:00 GMThttp://www.aygfsteel.com/bourn/articles/141545.htmlhttp://www.aygfsteel.com/bourn/comments/141545.htmlhttp://www.aygfsteel.com/bourn/articles/141545.html#Feedback0http://www.aygfsteel.com/bourn/comments/commentRss/141545.htmlhttp://www.aygfsteel.com/bourn/services/trackbacks/141545.html關于在Resin下debug的問題,也很簡單,首先將為httpd.exe建立一個快捷方式,然后右鍵屬性,在D:\resin-pro-3.0.18\httpd.exe
后面添加 -Xdebug -Xnoagent -Djava.compiler=NONE
-Xrunjdwp:transport=dt_socket,address=1183,suspend=n,server=y
語句,啟動resin后,就可發現第一行就會有提示的;

我發現直接按照以上的配置resin還有問題,就是無法加載applicationContext_manager.xml;
我費了很大勁才搞明白,是resin用自己的xmlparser所以不識別spring2.0的xml
schema 配置;已經找到解決辦法:
1.將<web-app id="/wiczone" document-directory="你自己的wiczone war

目錄G:/IdeaProjects/wiczone/trunk/wiczone/war"/> 改為:
<web-app id="/wiczone"
document-directory="D:/wiczone/trunk/wiczone/war">
        <!-- xml -->
        <system-property javax.xml.parsers.DocumentBuilderFactory=
                                             "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"/>
                  <system-property javax.xml.parsers.SAXParserFactory=
                                             "org.apache.xerces.jaxp.SAXParserFactoryImpl"/>
                                <!--  xslt -->
        <system-property javax.xml.transform.TransformerFactory=
                                             "org.apache.xalan.processor.TransformerFactoryImpl"/>
      </web-app>;
2.得最新web-inf\lib下的jar包;多加了一個xml解析器;
另外,\trunk\web
server\resin下面有resin服務器,和resin配置文件;




段氏 2007-08-30 23:02 發表評論
]]>
Spring 2.0 AOP 配置http://www.aygfsteel.com/bourn/articles/141543.html段氏段氏Thu, 30 Aug 2007 14:57:00 GMThttp://www.aygfsteel.com/bourn/articles/141543.htmlhttp://www.aygfsteel.com/bourn/comments/141543.htmlhttp://www.aygfsteel.com/bourn/articles/141543.html#Feedback0http://www.aygfsteel.com/bourn/comments/commentRss/141543.htmlhttp://www.aygfsteel.com/bourn/services/trackbacks/141543.html

springside的文檔中有aop的配置;http://wiki.springside.org.cn/display/springside/Spring+Aop
里面有關于pointcut 表達式語言的表述;

里面也有官方文檔中文版的鏈接:http://www.redsaga.com/spring_ref/2.0/html/aop.html
我們用scheme-based aop 配置方式:
 <aop:config> ......    </aop:config>

Advisor方式:
假設我們有一個MethodBeforeAdvice TestAdvice
;用于打印將要執行的方面名;
public class TestAdvice implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object object)
throws Throwable {
        System.out.println("method.getName() = " + method.getName());
        System.out.println("TestAdvice testing..." );
    }

}

配置如下:
 <bean id="testAdvice"
class="biz.pxzit.application.service.TestAdvice"/>
 <aop:config>
      <aop:advisor pointcut="execution(* biz..*Mgr.save*(..))"
advice-ref="testAdvice" order="1"/>

      ......
   </aop:config>

order是可選的,指定執行的次序;上面的配置語意是,在執行biz開頭的package下面任意以Mgr結尾的managersave開頭的方法時候,執行testAdvice,因為是MethodBeforeAdvice
所以在save開頭方法執行前執行;

Aspect方式:
public class TestAdvice2  {

    public void goAfter(JoinPoint joinPoint) throws Throwable {
        System.out.println("TestAdvice2.goAfter
testing..."+joinPoint.getTarget());
        System.out.println("TestAdvice2.goAfter testing..." );
    }

}

配置如下:

 <bean id="testAdvice2"
class="biz.pxzit.application.service.TestAdvice2"/>
 <aop:config>
           ......
            <aop:aspect id="ddAspect" ref="testAdvice2">
            <aop:after method="goAfter"
                        pointcut="execution(* biz..*Mgr.*(..))"
                />
          .......
        </aop:aspect>

   </aop:config>
注意TestAdvice2的方法參數,這里用的是JoinPoint
joinPoint
,還有很多細節,具體可以看文檔;
要使用<aop:aspect >節點,還需asm.jar
3個,在springframeworklib下有)否則會報noclassfound
exception

注意我之前配置的pointcut表達式是錯的,正確的是execution(*
biz..*Mgr.*(..))"

 



段氏 2007-08-30 22:57 發表評論
]]>
主站蜘蛛池模板: 荥经县| 黔江区| 南漳县| 旌德县| 奇台县| 盱眙县| 凯里市| 蕉岭县| 天门市| 利津县| 泗阳县| 双江| 丁青县| 龙川县| 英德市| 松阳县| 大宁县| 安岳县| 任丘市| 定远县| 西昌市| 香港| 堆龙德庆县| 肥乡县| 安阳县| 揭西县| 革吉县| 库车县| 平山县| 新竹市| 江华| 边坝县| 临潭县| 邮箱| 同仁县| 呈贡县| 福安市| 时尚| 巴南区| 扎赉特旗| 威宁|