2006年03月15日

Charlie 想戒毒,于是将毒品交给了Locke。

=====
第一个小片段:

CHARLIE:
Did you hear
what I said? I want my drugs back. I need ‘em.

LOCKE:
Yet you gave
them to me. Hmm.

CHARLIE:
And I bloody
well regret it. I’m sick, man. Can’t you see that?

LOCKE:
I think you’re a
lot stronger than you know, Charlie. And I’m going to prove it to
you. I’ll let you ask me for your drugs three times. The third
time, I’m going to give them to you. Now, just so we’re clear,
this is one.

CHARLIE:
Why? Why? Why
are you doing this? To torture me? Just get rid of them and have
done with it?

LOCKE:
If I did that
you wouldn’t have a choice, Charlie. And having choices, making
decisions based on more than instinct, is the only thing that
separates you from him [indicating the boar].

=====
第二个小片段:

LOCKE:
Then why aren’t
you with them? You didn’t come here to tell me about Jack, did
you?

CHARLIE [looking
like he going to cry]:
I want my stash, Locke. I
can’t stand feeling like this.

LOCKE:
Come here. I’m
going to show you something. [They walk to a plant with a cocoon
on it]. What do you supposed is in that cocoon, Charlie?

CHARLIE:
I don’t know, a
butterfly, I guess?

LOCKE:
No, it’s much
more beautiful than that. That’s a moth cocoon. It’s ironic,
butterflies get all the attention; but moths — they spin silk,
they’re stronger, they’re faster.

CHARLIE:
That’s
wonderful, but. . .

LOCKE:
You see this
little hole? This moth’s just about to emerge. It’s in there
right now, struggling. It’s digging it’s way through the thick
hide of the cocoon. Now, I could help it, take my knife, gently
widen the opening, and the moth would be free. But it would be
too weak to survive. The struggle is nature’s way of
strengthening it. Now this is the second time you’ve asked me for
your drugs back [he holds the heroin up]. Ask me again and it’s
yours.

=====
第三个小片段:

CHARLIE:
Give them to me.

LOCKE:
This is the
third time. Are you sure you really want them?

CHARLIE:
I’ve made my
choice.

[Locke hands the
drugs to Charlie.
Charlie looks at them and
throws them in the fire. Locke smiles.]

LOCKE:
I’m proud of
you, Charlie. Always knew you could do it.


2006年03月14日

windows操作系统,能够站在统治地位,其中有一个原因就是它的丰富强大详尽的API。

Firefox能在IE垄断的情况下开始蚕食并扩张地盘,也是因为开放。

在Web2.0大张旗鼓地时代,其自身的一个特点就是开放API接口。

开放,使广大用户可以修改定制自己想要的东西,满足自我价值实现的同时,希望分享自己的劳动成果,主动做了传播者的角色。某某软件不错,是吗?推荐你用一下,但是,该软件based on WinXP。WinXP没怎么做广告,因为所有基于它开发的软件都在给他作广告。

开放的另一个表现就是开源,开源的东西,多半不做广告,也能传的很远。

开放的缺点是不稳定和技术的部分公开。但是,牺牲这些而带来的市场,可能是很难估量的。

只以IT背景阐述下开放带来的益处,其他方面,有所雷同,你准备好敞开你的胸怀了吗?

2006年03月13日

看了几集“Desperate Housewives”,经常听到一个词叫“Dirty Laundry”,根据上下文理解,含义应该是那种每个人不可告人的小秘密吧。

这里是我在网上找到的解释
[noun] personal matters that could be embarrassing if made public.

2006年03月03日

Template Method,模板方法,听名字已经有那么点意思了。

从早晨起床,到上学/班,都会做些什么呢?使自己清醒,着装,梳洗,早饭,出门准备。这些就是该做的事,并且约定俗成。不同的人,对于这些方法的实现是不同的。比如,张三只需要揉一下眼睛就清醒了,李四却需要十个闹钟闹十遍才行。史密斯先生梳洗过程中使用黑妹牙膏刷牙,他夫人则使用黑人牙膏。钱老板吃的是牛奶面包,孔乙己吃的是茴香豆。即使是同一个人,可能今天为了装乞丐穿得破破烂烂,明天就能西装笔挺地骗吃骗喝。

早上的这段时间,所需要做的事情是固定的,但是,实现的细节却根据人与时间的不同而千差万别,该是一个模板方法的典型例子。

具体的实现和应用,去Google一下吧。




2006年02月22日

看到一篇较为古老的“文章”, 于是自己写代码实现了一下,发现果然有很大的差别。

我写了一个简单的测试程序,核心部分就是

            // Read One
            while ((isStream.read()) != -1) readLen++;
           
            // Read Buffer

            while ((readOnce = isStream.read(buffer, 0, buffer.length)) != -1)
            { readLen+=readOnce; }


思路是对一个大小为100,000字节的文件全部读取一遍,一种是采用ReadOne方式,另一种是采用ReadBuffer的方式,然后通过消耗的时间来比较哪种方法更高效。

在标准模拟器上的结果如下:

ReadOne:
ReadOne


ReadBuf:
Read Buffer

采用ReadOne方式消耗了2043毫秒,而采用ReadBuf方式消耗了<0毫秒。

另选用了两款手机进行三次测试,结果如下:

Nokia6260
    ReadOne: 125     94     188
    ReadBuf: 31     31     15

MotoE398
    ReadOne: 6102     6135     6192
    ReadBuf: 18     17     18

所以,采用ReadBuf的方式,还是对性能可能有显著的提高。

附上源码,如果你有手机,也可以测试一下。
/*
 * HelloMidlet.java
 *
 */

package hello;

import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.InputStream;

/**
 *
 * @author Vince
 */
public class HelloMidlet extends MIDlet implements javax.microedition.lcdui.CommandListener {
    String fileStringName="/test.bin";
    /** Creates a new instance of HelloMidlet */
    public HelloMidlet() {
    initialize();
    }
   
    private javax.microedition.lcdui.Form helloForm;                    
    private javax.microedition.lcdui.Command exitCommand;
    private javax.microedition.lcdui.Command readOneCommand;
    private javax.microedition.lcdui.Command readBufCommand;
    private javax.microedition.lcdui.StringItem stringItem1;
    private javax.microedition.lcdui.StringItem stringItem2;                  
   
    /** This method initializes UI of the application.                     
     */
    private void initialize() {
    getDisplay().setCurrent(get_helloForm());
    }                   
   
    /** Called by the system to indicate that a command has been invoked on a particular displayable.                     
     * @param command the Command that ws invoked
     * @param displayable the Displayable on which the command was invoked
     */
    public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {                   
    // Insert global pre-action code here

    if (displayable == helloForm) {                    
        if (command == exitCommand) {                  
        // Insert pre-action code here
        exitMIDlet();                      
        // Insert post-action code here
        } else if (command == readOneCommand) {                    
        // Insert pre-action code here
        try {
            InputStream isStream = getClass().getResourceAsStream("/hello/test.bin");
            long preTime = System.currentTimeMillis();
            int readLen = 0;
            while ((isStream.read()) != -1) readLen++;
            long postTime = System.currentTimeMillis();
            isStream.close();
            isStream = null;
            get_stringItem2().setText("get"+readLen+"; elapse:" + (postTime – preTime));
        }
        catch (Exception e) {
            helloForm.append(new StringItem("Exception", e.getMessage()));
        }
        // Do nothing                      
        // Insert post-action code here
       
        } else if (command == readBufCommand) {                    
        // Insert pre-action code here
        byte[] buffer = new byte[4096];
        try {
            InputStream isStream = getClass().getResourceAsStream("/hello/test.bin");
            long preTime = System.currentTimeMillis();
            int readLen = 0;
            int readOnce = 0;
            while ((readOnce = isStream.read(buffer, 0, buffer.length)) != -1)
            readLen+=readOnce;
            long postTime = System.currentTimeMillis();
            isStream.close();
            isStream = null;
            get_stringItem2().setText("get"+readLen+"; elapse:" + (postTime – preTime));
        }
        catch (Exception e) {
            helloForm.append(new StringItem("Exception", e.getMessage()));
        }
        // Do nothing                       
       
        // Insert post-action code here
        buffer = null;
        }                      
    }                    
    // Insert global post-action code here
}                  
   
    /**
     * This method should return an instance of the display.
     */
    public javax.microedition.lcdui.Display getDisplay () {                        
    return javax.microedition.lcdui.Display.getDisplay(this);
    }                       
   
    /**
     * This method should exit the midlet.
     */
    public void exitMIDlet () {                        
    getDisplay().setCurrent(null);
    destroyApp(true);
    notifyDestroyed();
    }                       
   
    /** This method returns instance for helloForm component and should be called instead of accessing helloForm field directly.                       
     * @return Instance for helloForm component
     */
    public javax.microedition.lcdui.Form get_helloForm() {
    if (helloForm == null) {                     
        // Insert pre-init code here
        helloForm = new javax.microedition.lcdui.Form(null, new javax.microedition.lcdui.Item[] {                      
        get_stringItem1(),
        get_stringItem2()
        });
        helloForm.addCommand(get_exitCommand());
        helloForm.addCommand(get_readOneCommand());
        helloForm.addCommand(get_readBufCommand());
        helloForm.setCommandListener(this);                    
        // Insert post-init code here
    }                     
    return helloForm;
    }                   
   
   
    /** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.                       
     * @return Instance for exitCommand component
     */
    public javax.microedition.lcdui.Command get_exitCommand() {
    if (exitCommand == null) {                     
        // Insert pre-init code here
        exitCommand = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT, 1);                     
        // Insert post-init code here
    }                     
    return exitCommand;
    }                   

    /** This method returns instance for readOneCommand component and should be called instead of accessing readOneCommand field directly.                       
     * @return Instance for readOneCommand component
     */
    public javax.microedition.lcdui.Command get_readOneCommand() {
    if (readOneCommand == null) {                     
        // Insert pre-init code here
        readOneCommand = new javax.microedition.lcdui.Command("ReadOne", javax.microedition.lcdui.Command.ITEM, 1);                     
        // Insert post-init code here
    }                     
    return readOneCommand;
    }                   
 
    /** This method returns instance for readBufCommand component and should be called instead of accessing readBufCommand field directly.                        
     * @return Instance for readBufCommand component
     */
    public javax.microedition.lcdui.Command get_readBufCommand() {
    if (readBufCommand == null) {                      
        // Insert pre-init code here
        readBufCommand = new javax.microedition.lcdui.Command("ReadBuf", javax.microedition.lcdui.Command.ITEM, 2);                      
        // Insert post-init code here
    }                      
    return readBufCommand;
    }                    

    /** This method returns instance for stringItem1 component and should be called instead of accessing stringItem1 field directly.                        
     * @return Instance for stringItem1 component
     */
    public javax.microedition.lcdui.StringItem get_stringItem1() {
    if (stringItem1 == null) {                      
        // Insert pre-init code here
        stringItem1 = new javax.microedition.lcdui.StringItem("Welcome", "Choose \"ReadOne\" menu item or \"ReadBuf\" menu item to see the time spending of reading stream(100,000 bytes)");                      
        // Insert post-init code here
    }                      
    return stringItem1;
    }                    

    /** This method returns instance for stringItem2 component and should be called instead of accessing stringItem2 field directly.                        
     * @return Instance for stringItem2 component
     */
    public javax.microedition.lcdui.StringItem get_stringItem2() {
    if (stringItem2 == null) {                      
        // Insert pre-init code here
        stringItem2 = new javax.microedition.lcdui.StringItem("Microsecond", "0");                      
        // Insert post-init code here
    }                      
    return stringItem2;
    }                    
   
    public void startApp() {
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
    }
   
}



2006年02月03日

首先,去除“自私”一词的贬义色彩,然后,人是自私的。何以见得?

家有一猫,不日,抱起,双手伸出阳台。视其鬼哭狼嚎状,遂抱回并以小鱼安慰之。

假设以我对猫的行为理解没有错误的话,它是在害怕,害怕什么呢?我家猫猫随我时日不短,假设耳濡目染掌握了一些基本的逻辑思维,并能够估计到这个高度已经超越了它能够挑战的极限的话,那么,它害怕的就是死亡。

任何的活物,具有自我了断能力的活物,都该怕死,如若不然,该物种就不会存活至今。出于对死亡的恐惧和对自我个体生命的维护,这种天生的能力便是自私的基本构成。

在每个人的心底,都有着要维护的东西。除去上述的生命,还有糖果,学校,英雄的形象,家族的荣誉,国家,后代的幸福,世界的和平等等等等。

画一个圈,把自己包括进去,圈内便是要维护的东西(不以多少来定圈的大小),属于自私的范畴,圈内与圈外的关系,是格格不入的关系。

很多时候,我们在苦苦思索解决圈内外的矛盾的时候,是否意识到,一个简单的方法就是把圈子画大一点,还有矛盾?再大一点。当圈子无限扩大,没有边界,就成了无私。

想一想自己的圈子画在什么地方,是否画的对,应该是一件很重要的事。

2006年01月30日

1.     Explain the difference between a hot backup and a cold backup and the benefits associated with each.



A hot backup is basically taking a backup of the database while it is
still up and running and it must be in archive log mode. A cold backup
is taking a backup of the database while it is shut down and does not
require being in archive log mode. The benefit of taking a hot backup
is that the database is still available for use while the backup is
occurring and you can recover the database to any point in time. The
benefit of taking a cold backup is that it is typically easier to
administer the backup and recovery process. In addition, since you are
taking cold backups the database does not require being in archive log
mode and thus there will be a slight performance gain as the database
is not cutting archive logs to disk.



2.     You have just had to restore from backup and do not have any
control files. How would you go about bringing up this database?



I would create a text based backup control file, stipulating where on
disk all the data files where and then issue the recover command with
the using backup control file clause.



3.     How do you switch from an init.ora file to a spfile?



Issue the create spfile from pfile command.



4.     Explain the difference between a data block, an extent and a segment.



A data block is the smallest unit of logical storage for a database
object. As objects grow they take chunks of additional storage that are
composed of contiguous data blocks. These groupings of contiguous data
blocks are called extents. All the extents that an object takes when
grouped together are considered the segment of the database object.



5.     Give two examples of how you might determine the structure of the table DEPT.



Use the describe command or use the dbms_metadata.get_ddl package.



6.     Where would you look for errors from the database engine?



In the alert log.



7.     Compare and contrast TRUNCATE and DELETE for a table.



Both the truncate and delete command have the desired outcome of
getting rid of all the rows in a table. The difference between the two
is that the truncate command is a DDL operation and just moves the high
water mark and produces a now rollback. The delete command, on the
other hand, is a DML operation, which will produce a rollback and thus
take longer to complete.



8.     Give the reasoning behind using an index.



Faster access to data blocks in a table.



9.     Give the two types of tables involved in producing a star schema and the type of data they hold.



Fact tables and dimension tables. A fact table contains measurements
while dimension tables will contain data that will help describe the
fact tables.



10. . What type of index should you use on a fact table?



A Bitmap index.



11. Give two examples of referential integrity constraints.



A primary key and a foreign key.



12. A table is classified as a parent table and you want to drop and
re-create it. How would you do this without affecting the children
tables?



Disable the foreign key constraint to the parent, drop the table, re-create the table, enable the foreign key constraint.



13. Explain the difference between ARCHIVELOG mode and NOARCHIVELOG mode and the benefits and disadvantages to each.



ARCHIVELOG mode is a mode that you can put the database in for creating
a backup of all transactions that have occurred in the database so that
you can recover to any point in time. NOARCHIVELOG mode is basically
the absence of ARCHIVELOG mode and has the disadvantage of not being
able to recover to any point in time. NOARCHIVELOG mode does have the
advantage of not having to write transactions to an archive log and
thus increases the performance of the database slightly.



14. What command would you use to create a backup control file?



Alter database backup control file to trace.



15. Give the stages of instance startup to a usable state where normal users may access it.



STARTUP NOMOUNT – Instance startup



STARTUP MOUNT – The database is mounted



STARTUP OPEN – The database is opened



16. What column differentiates the V$ views to the GV$ views and how?



The INST_ID column which indicates the instance in a RAC environment the information came from.



17. How would you go about generating an EXPLAIN plan?



Create a plan table with utlxplan.sql.



Use the explain plan set statement_id = ‘tst1′ into plan_table for a SQL statement



Look at the explain plan with utlxplp.sql or utlxpls.sql



18. How would you go about increasing the buffer cache hit ratio?



Use the buffer cache advisory over a given workload and then query the
v$db_cache_advice table. If a change was necessary then I would use the
alter system set db_cache_size command.



19. Explain an ORA-01555



You get this error when you get a snapshot too old within rollback. It
can usually be solved by increasing the undo retention or increasing
the size of rollbacks. You should also look at the logic involved in
the application getting the error message.



20. Explain the difference between $ORACLE_HOME and $ORACLE_BASE.



ORACLE_BASE is the root directory for oracle. ORACLE_HOME located beneath ORACLE_BASE is where the oracle products reside.





21.  How would you determine the time zone under which a database was operating?



select DBTIMEZONE from dual;

22.  Explain the use of setting GLOBAL_NAMES equal to TRUE.



Setting GLOBAL_NAMES dictates how you might connect to a database. This
variable is either TRUE or FALSE and if it is set to TRUE it enforces
database links to have the same name as the remote database to which
they are linking.



23.  What command would you use to encrypt a PL/SQL application?



WRAP



24.  Explain the difference between a FUNCTION, PROCEDURE and PACKAGE.



A function and procedure are the same in that they are intended to be a
collection of PL/SQL code that carries a single task. While a procedure
does not have to return any values to the calling application, a
function will return a single value. A package on the other hand is a
collection of functions and procedures that are grouped together based
on their commonality to a business function or application.



25.  Explain the use of table functions.



Table functions are designed to return a set of rows through PL/SQL
logic but are intended to be used as a normal table or view in a SQL
statement. They are also used to pipeline information in an ETL process.



26.  Name three advisory statistics you can collect.



Buffer Cache Advice, Segment Level Statistics, & Timed Statistics



27.  Where in the Oracle directory tree structure are audit traces placed?



In unix $ORACLE_HOME/rdbms/audit, in Windows the event viewer



28.  Explain materialized views and how they are used.



Materialized views are objects that are reduced sets of information
that have been summarized, grouped, or aggregated from base tables.
They are typically used in data warehouse or decision support systems.



29.  When a user process fails, what background process cleans up after it?



PMON



30.  What background process refreshes materialized views?



The Job Queue Processes.



31.  How would you determine what sessions are connected and what resources they are waiting for?



Use of V$SESSION and V$SESSION_WAIT



32.  Describe what redo logs are.



Redo logs are logical and physical structures that are designed to hold
all the changes made to a database and are intended to aid in the
recovery of a database.



33.  How would you force a log switch?



ALTER SYSTEM SWITCH LOGFILE;

34.  Give two methods you could use to determine what DDL changes have been made.



You could use Logminer or Streams



35.  What does coalescing a tablespace do?



Coalescing is only valid for dictionary-managed tablespaces and
de-fragments space by combining neighboring free extents into large
single extents.



36.  What is the difference between a TEMPORARY tablespace and a PERMANENT tablespace?



A temporary tablespace is used for temporary objects such as sort
structures while permanent tablespaces are used to store those objects
meant to be used as the true objects of the database.



37.  Name a tablespace automatically created when you create a database.



The SYSTEM tablespace.



38.  When creating a user, what permissions must you grant to allow them to connect to the database?



Grant the CONNECT to the user.



39.  How do you add a data file to a tablespace?



ALTER TABLESPACE <tablespace_name>; ADD DATAFILE <datafile_name>; SIZE <size>;

40.  How do you resize a data file?



ALTER DATABASE DATAFILE <datafile_name>; RESIZE <new_size>;;

41.  What view would you use to look at the size of a data file?



DBA_DATA_FILES

42.  What view would you use to determine free space in a tablespace?



DBA_FREE_SPACE

43.  How would you determine who has added a row to a table?



Turn on fine grain auditing for the table.



44.  How can you rebuild an index?



ALTER INDEX <index_name>; REBUILD;

45.  Explain what partitioning is and what its benefit is.



Partitioning is a method of taking large tables and indexes and splitting them into smaller, more manageable pieces.



46.  You have just compiled a PL/SQL package but got errors, how would you view the errors?



SHOW ERRORS

47.  How can you gather statistics on a table?



The ANALYZE command.



48.  How can you enable a trace for a session?



Use the DBMS_SESSION.SET_SQL_TRACE or



Use ALTER SESSION SET SQL_TRACE = TRUE;



49.  What is the difference between the SQL*Loader and IMPORT utilities?



These two Oracle utilities are used for loading data into the database.
The difference is that the import utility relies on the data being
produced by another Oracle utility EXPORT while the SQL*Loader utility
allows data to be loaded that has been produced by other utilities from
different data sources just so long as it conforms to ASCII formatted
or delimited files.



50.  Name two files used for network connection to a database.



TNSNAMES.ORA and SQLNET.ORA



Technical – UNIX

Every DBA should know something about the operating system that the
database will be running on. The questions here are related to UNIX but
you should equally be able to answer questions related to common
Windows environments.



1.  How do you list the files in an UNIX directory while also showing hidden files?



ls -ltra

2.  How do you execute a UNIX command in the background?



Use the "&"



3.  What UNIX command will control the default file permissions when files are created?



Umask



4.  Explain the read, write, and execute permissions on a UNIX directory.



Read allows you to see and list the directory contents.



Write allows you to create, edit and delete files and subdirectories in the directory.



Execute gives you the previous read/write permissions plus allows you
to change into the directory and execute programs or shells from the
directory.



5.  the difference between a soft link and a hard link?



A symbolic (soft) linked file and the targeted file can be located on
the same or different file system while for a hard link they must be
located on the same file system.



6.  Give the command to display space usage on the UNIX file system.



df -lk

7.  Explain iostat, vmstat and netstat.



Iostat reports on terminal, disk and tape I/O activity.



Vmstat reports on virtual memory statistics for processes, disk, tape and CPU activity.



Netstat reports on the contents of network data structures.



8.  How would you change all occurrences of a value using VI?



Use :%s/<old>;/<new>;/g



9.  Give two UNIX kernel parameters that effect an Oracle install



SHMMAX & SHMMNI



10.  Briefly, how do you install Oracle software on UNIX.



Basically, set up disks, kernel parameters, and run orainst.

2006年01月29日

英文原文:http://bram.jteam.nl/?p=2

你想再用几个月尝试一下Ajax, HttpXmlRequest, DWR?你的Web App正使用的是Spring的框架?恩,你来对地方了。

本文给出了DWR+Spring的介绍。不过,你必须具备Javascript和使用Spring的基本知识。

DWR
DWR使通过Ajax调用你的业务层模块变得简单。它让你使用Ajax,却不用理会那些繁琐的诸如兼容浏览器之类的麻烦。

虽然DWR自己也提供了一些对于Spring的支持,但是却无法做到无缝整合。例如,它需要你在Web.xml中配置一个独立的Servlet,却无法让它访问Web App Context中的Beans。

让我们开始吧
首先,我们来配置一个Spring MVC Controller,称之DWRServlet,用它取代上面所说的那个独立的Servlet。还好,Spring已经提供了一个现成的,叫ServletWrappingController。将下列代码加到你的dispacher app context,DWR就会成为一个标准的Spring Controller。

<bean id="dwrController" class= "org.springframework.web.servlet.mvc.ServletWrappingController">

  <property name="servletClass">

    <value>uk.ltd.getahead.dwr.DWRServlet</value>

  </property>

  <property name="initParameters">

    <props>

      <prop key="debug">true</prop>

    </props>

  </property>

</bean>

注意,开发过程中将初始化参数Debug置True,它将提供有用的Beans列表。更重要的是,注意我们是怎样将初始化参数值传递给DWR的。

添加一条Url Mapping以确保所有的请求都首先交由这个DWRServlet处理。注意,这个Mapping应该处于最后,以确保能处理到所有的请求。

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

  <property name="mappings">

    <props>

      <prop key="/**/*.html">defaultController</prop>

      <prop key="/**/*">dwrController</prop>

    </props>

  </property>

</bean>


另外,还要确定所有的后缀请求(包括没有后缀)都交给Spring的dispatcher处理了。所以,除了Mapping JSP和HTML以外,还要像下例一样Mapping所有的后缀。下例假设已经定义了名为“main”的Spring DispacherServlet。

<servlet-mapping>

  <servlet-name>main</servlet-name>

  <url-pattern>/dwr/*</url-pattern>

</servlet-mapping>


配置DWR
虽然可以通过初始参数改变,但DWRServlet假设配置文件为/WEB-INF/dwr.xml。下面是一个示例配置文件,文件中使用了假设在App Context中已经存在的一个id为productManager的Bean。

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE dwr PUBLIC

  "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"

  "http://www.getahead.ltd.uk/dwr/dwr10.dtd">

 

<dwr>

  <allow>

    <convert

      converter="bean"

      match="your.package.*"/>

 

    <create

      creator="spring"

      javascript="productManager">

      <param name="beanName" value="productManager"/>

    </create>

  </allow>

</dwr>

注意,现在默认情况下包含了一个Spring creator。这个Creator将处于App Context中的productManager Bean暴露(指在JS中可以调用)了出来。可以用此法创建一个新的Bean,或者找到一个已经存在的Bean。

在<allow>中,首先需要定义一个Convertor以将标准的JavaBeans转换为Javascript的对象。虽然DWR提供了此类支持,但是默认情况下没有开启。DWR默认只开启了对于基本类型以及一些Collection的Convertor。注意,你所开启的包,它必须包含你想要convert的类。

最后,你为每一个想要通过DWR暴露出来的Bean写一个<create>。本例中,我们暴露了一个id为productManager的bean。

访问你的Bean
该做的都差不多了,让我们试试看访问这个Product Manager。假设这个Bean有一个getAllProducts()的方法,并以List方式组织所有已知的Product(JavaBean)将它作为返回。首先需要在我们的页面中包括两个Javascript文件。

<script type='text/javascript' src='/dwrPath/interface/productManager.js’></script>

<script type=’text/javascript’ src=’/dwrPath/engine.js’></script>

上面的dwrPath就是之前提到的已经被映射了的dwrcontroller。现在,唯一要做的就是调用Product Manager的方法了。假设我们需要一个按钮来触发获取信息的过程,并用span来取得所有的返回值。

<input type="button" onclick="productManager.getAllProducts(productCallback);" value="Get all products"/>

<script type="text/javascript">

  var productCallback = function(products) {

    var text = 'Products: <br><ul>';

    for(i = 0; i < products.length; i++) {

      text += '<li>';

      text += products[i].name;

 

      text += ' (';

      price= products[i].price;

      text += price.currency;

      text += ' ';

      text += price.value;

      text += ')';

      text += '</li>';

    }

    document.getElementById('productlist').innerHTML = text;

  }

</script>

<span id="productlist">

注意嵌套的属性如price.currency和price.value。仅当Product和Price位于Bean Convertor所指定的路径(本例为your.package)下的时候,才能被正确解析。

调试
DWR 提供了内置的调试功能。由于在初始参数中将debug置为true,我们可以使用这些内置的调试功能。使用浏览器访问/dwrPath,你会发现一个列表,列出了所有通过DWR暴露的Beans。通过他们的链接,还可以得到每个Bean暴露的方法列表。它还提供了调用这些方法调试例子,以方便使用和调试。

工具
DWR还提供了些工具类方法和处理错误的手段。更多信息参见这里

祝好运
我希望这个简单的教程让你了解了如何集成Spring和DWR,并让你更容易的创建一个基于Ajax技术的Web App。  我现在还在看一些如何使用Spring+DWR进行实时的表单校验等。所以,经常逛逛本Blog以保持你跟上进度了哦!

英文原文:http://getahead.ltd.uk/dwr/browser/util/$

使用$函数(在Javascript中属于合法的名字)的想法是受到了Prototype(一个Javascript框架)的启发。大致说来,$=document.getElementById。由于在Ajax编程中大量使用了后者,用前者取代它则让你受益匪浅。

至此, “$”从当前的HTML文档中找到指定id的元素,所有非字符串的参数,将会返回原值,另外,本$函数对Prototype中的函数中的函数作了部分改进,可能会适应更多浏览器。

DWRUtil.toDescriptiveString演示了用法,可以参考。

理论上说,该函数因为使用了Array.push而不兼容IE5.0,但是,如果与engine.js共同使用,就可以避开此问题。如果你不使用engine.js又想兼容IE5.0,那就得想个法子把Array.push替换掉。

上一页
下一页

2006年01月23日

小时在家看电视,记得是那种传统的国产英雄主义片,片中男主角一身正气,毅然违反了上级错误的决定,最终带来了一个非常完美的结局。回了营地,上上级不仅没有惩罚男主角,反而进行了表彰与嘉奖。当然,做出错误决定的上级受到相应处分。

每个人都是有个性的,每个人都是有个人的想法的,每个人都希望将自己的想法付诸实现。但是,团队意味着这种个性化的东西需要被削弱,降低到最低。诚然,在上面的这个故事中,如果男主角未能勇挑重责,可能会导致一场败仗,甚至全军覆没,但是,如果最终的结果证明男主角的决定是错误的呢?在做决定的时候,没有人会知道最终的结局,不是吗?每个人在做决定的时候,都会有充分的理论依据来支持,因此,也都会认为自己的决定是正确的,如果每个人的正确想法都要被执行的话,这个团队还有何凝聚力可言?

执行即使是你认为错误的命令,这样的队伍才是强大的。

应该得到表彰的是那个正确的决定(事),而不是做出决定的男主角(人)。