2006年09月28日

以前用过UML,对Use Cases相对比较熟一点,最近在学习Agile的过程中,接触了一个新的概念,User Stories。 但是怎么就感觉说的和Use Cases是一回事,正好在《User Stories Applied: For Agile Software Development》一书的FAQ中发现了相关内容。

书中提及,“One of the most obvious differences between stories and use cases is their scope.”“ stories are kept smaller in scope because we place constraints on their size (such as no more than ten days of development work) so that they may be used in scheduling work. A use case almost always covers a much larger scope than a story.”“ Each story is not necessarily equivalent to a main success scenario”。按照这样的理解,Use Cases是针对一个比较完整的用例,包含正常情况和各种非正常的情况。相对的,User Stories更像是把User Cases break down,并排列整合起来。

Another important difference between use cases and stories is their longevity. Use cases are often permanent artifacts that continue to exist as long as the product is under active development or maintenance. Stories, on the other hand, are not intended to outlive the iteration in which they are added to the software. While it is possible to archive story cards, many teams simply rip them up.”对此,非常不理解,为何Stories就在迭代周期外不应存在呢?继续读下去。。。

Another difference is that use cases and stories are written for different purposes (Davies 2001). Use cases are written in a format acceptable to both customers and developers so that each may read and agree to them. Their purpose is to document an agreement between the customer and the development team. Stories, on the other hand, are written to facilitate release and iteration planning, and to serve as placeholders for conversations about the users’ detailed needs.”感觉这该是一个非常重要的差异,因为其他一些区别是基于这个差异的。

Finally, use cases are generally written as the result of an analysis activity, while user stories are written as notes that can be used to initiate analysis conversations.

读完了这些,基本上对作者想要阐述的区别有了一个大致的了解。由于两者存在的目的不同,Use Cases是为了让Business方和Software方能有一种双方能够理解的沟通方式并达成一致,使以后的软件开发有个依据,以遵从Business方的需求。而相对应的,User Story是为了在一个迭代周期内制定计划的。所以,他们的内容也许很相似,但是本质却很不一样。Use Case更有些为Software一方服务,可说是一个类似architecture的工具,而User Story更站在用户角度,主要为了Schedule,可以说是一个类似Project Management的工具。鉴于以上的目的不一致,表现形式的不一致也就可以理解了。所以,虽然Use Case是一个比较概括的东西,但是却必须是非常Serious的,相对而言,User Story就比较随意些。

顺便翻了下《UML Distilled: A Brief Guide to the Standard Object Modeling Language, Third Edition》,正好也简短的说到了两者的差距,觉得说得比较到位。

Features are a good way of chunking up a system for planning an iterative project, whereby each iteration delivers a number of features. Use cases provide a narrative of how the actors use the system. Hence, although both techniques describe requirements, their purposes are different.

并且,提供了一种比较好的建议,很赞同。

Although you can go directly to describing features, many people find it helpful to develop use cases first and then generate a list of features. A feature may be a whole use case, a scenario in a use case, a step in a use case, or some variant behavior, such as adding yet another depreciation method for your asset valuations, that doesn’t show up in a use case narrative. Usually, features end up being more fine grained than use cases.

2006年09月04日

今天想在自己winxp电脑上安装apche+php环境,结果就一个简单的phpinfo(),折腾了半天,记录下碰到的问题:

1 按照安装步骤,一切就绪以后,愣是无法重启apache Server。看了Apache的Test Configuration, 发现“LoadModule php5_module modules/php5apache2.dll ”有问题,load 没有成功。

解决方法:http://snaps.php.net/ 下载5.2.x dev版本的php,解压后将php5apache2_2.dll放到modules目录下面,并将config里面的php5apache2.dll改为php5apache2_2.dll。

 

2 简单的<? phpinfo() ?> 显示空白

解决方法:改为 <?php phpinfo() ?>

 

最后总算是看到了点界面。

2006年06月13日

使用Spring框架一个很大的好处在于可以方便地对代码进行测试,涉及到数据库加Hibernate,Spring也为此提供了便利,参见Spring官方文档

官方文档中的23.2.3对测试中的事务 (Transaction) 处理作了解释,默认情况下,使用了Spring框架提供的AbstractTransactionalDataSourceSpringContextTests以及它的扩展类,将会使每一个test case测试完毕以后,数据自动回滚。这是一个很贴心的服务,理论上来说,我们的确不希望测试数据被加入数据库中,所以,测试完以后的数据库,最好能和测试以前一样。

但是,不知怎么的,我的一个测试用例每次都在数据库里留下蛛丝马迹,让人非常费解。最后定位到了问题所在,(占个位子,未完待续)。

2006年04月20日

Trackback,这里的解释算是比较通俗易懂了

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年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月18日

英文原文:http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html

JDK5.0 引入了一种新的元语言工具,叫“annotation”。 Annotation 提供的信息不是程序的一部分,例如,代码片断的作者,或者告诉编译器忽略特定的错误等。Annotation不会对程序的运行产生任何影响。

Annotation的形式为:@annotation。它可以被应用到类,属性,方法等的声明处。Annotation位于他们之前,并且(通常)独立一行。它还可以带有参数,例如:
    @Author("MyName")
    class myClass {}
或者
    @SuppressWarnings("unchecked")
    void MyMethod() {}

自定义annotation属于高级用法,不属于本文讨论范畴。但是有三个内置的annotation你必须知道(如果你是Java程序员的话),他们是@Deprecated,@Override和@SuppressWarnings。以下列举他们对于方法的应用。

import java.util.List;

class Food {}
class Hay extends Food {}
class Animal {
Food getPreferredFood() {
return null;
}
/**
* @deprecated document why the method was deprecated
*/
@Deprecated
static void deprecatedMethod() { }
}
class Horse extends Animal {
Horse() {
return;
}
@Override
Hay getPreferredFood() {
return new Hay();
}
@SuppressWarnings("deprecation")
void useDeprecatedMethod() {
Animal.deprecateMethod(); //deprecation warning - suppressed
}
}

@Deprecated

@Deprecated annotation 表示被标志的方法不应该被继续使用了,如果程序使用了deprecated的类,变量或方法,编译器将会给出警告。如果一个元素被deprecate了,那么应该如上例所示在上面的注释文档中添加deprecated的标记并给出原因。注意,文档中标记的首字母是小写d,annotation中的首字母是大写D。一般来说,你应该避免使用deprecated的方法,可以参阅相关文档了解替代方法。


@Override

@Override annotation是为了告诉编译器,该元素会override父类的这个元素。在上例中,override annotation表示Horse中的getPreferredFood方法将会override 父类Animal中的getPreferredFood方法。如果这步失败,编译器会报错。

尽管Override的时候并不是必须使用这个annotation,使用它还是可以使程序逻辑更明了化,特别是当子类方法的返回值继承自父类方法的返回值。如在上例中,子类的getPreferredFood方法返回Hey的实例,父类的getPreferredFood方法返回Food的实例,其中Hey继承Food。更多信息见Overriding and Hiding Methods


@SuppressWarnings

@SuppressWarnings告诉编译器,此处不要报错。在上例中,useDeprecatedMethod方法调用了Animal类中的deprecated方法,通常编译器会给出警告,但是此处,被suprress了。

每一个编译器警告属于一个类别,Java语言规范中列出了两类:“deprecation” 和“unchecked”。如果要suppress这两类警告,使用如下方式:
    @SuppressWarnings ( {"unchecked", "deprecation"} )
参见编译器文档了解警告的类别。

对于annotation更高级的用法是写一段程序不仅可以读java代码,并且可以处理annotations。为了达到这个目标,5.0JDK中包含了一个处理annotation的工具,称为apt。下一个版本JDK(代号Mustang)的apt将成为Java编译器的一个标准部分。更多信息,可以参见Getting Started with the Annotation Processing Tool。关于正在实现的Mustang感兴趣,可以参见Language Model APIJSR 269: Pluggable Annotation Processing API