| « | 一月 2012 | » | ||||
|---|---|---|---|---|---|---|
| 一 | 二 | 三 | 四 | 五 | 六 | 日 |
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 | |||||
设随机变量X的概率分布为:
P(X=k)=1/2^k(k=1,2,3,…)
求:
1).P{X为偶数}
2).P{X>=5}
解:
1) P{X为偶数}
=P{x=2}+ P{x=4}+ P{x=6}+ P{x=8}+…
=1/2^2+1/2^4+1/2^6+1/2^8+…
=1/4+1/4^2+1/4^3+1/2^4+…
=(1/4)/(1-1/4)
=1/3
2) P{X>=5}
=P{x=5}+ P{x=6}+ P{x=7}+ P{x=8}+…
=1/2^5+1/2^6+1/2^7+1/2^8+…
=(1/2^5)/(1-1/2)
=1/16
或
P{X>=5}=1- P{X<5}
=1- P{x=1}- P{x=2}- P{x=3}- P{x=4}
=1-1/2-1/4-1/8-1/16
=1/16
在http://packages.debian.org/zh-tw/lenny/i386/fcitx/download
下载fcitx_
拷贝到某一目录,如/usr下
安装
执行dpkg -i fcitx_
debiangary:/usr# dpkg -i fcitx_
Selecting previously deselected package fcitx.
(
Unpacking fcitx (from fcitx_
Setting up fcitx (
debiangary:/usr# ls
X11R6 fcitx_
bin games lib sbin soft
debiangary:/usr#
在/etc/X11/Xsession.d/目录下创建一个名为25xchinput的文件,其内容如下:
export LAGN=zh_CN.utf8
export LC_CTYPE=zh_CN.utf8
export LC_ALL=
export XMODIFIERS=@im=fcitx
fcitx &
开机后将自动启动fcitx输入法。使用Ctrl+Space切换输入法
查看全文
eclipse-SDK-3.2-linux-gtk.tar.gz和MyEclipse_5_5_1GA_E3_2_2_Installer.bin整合
下载MyEclipse_5_5_1GA_E3_2_2_Installer.bin
拷贝到/ypu下
在/ypu下执行
chmod 777 MyEclipse_5_5_1GA_E3_2_2_Installer.bin
执行./ MyEclipse_5_5_1GA_E3_2_2_Installer.bin
开始安装
找到已经安装好的eclipse3.2的位置。
在source.list中加入deb http://oss.oracle.com/debian/ unstable main non-free
执行
aptitude update
aptitude oracle-xe-client oracle-xe-universal 包有点大260多兆
开始下载oracle-xe-client oracle-xe-universal,大概需要三十多分钟。
下载完后就开始安装,但是系统会提示交换分区不够,所以要增大交换分区。
分别执行
dd if=/dev/zero of=/mnt/swapfile bs=1024 count=1048576
mkswap /mnt/swapfile
swapon /mnt/swapfile
之后。
现在开始安装oracle 10g express
执行
aptitude install oracle-xe-client oracle-xe-universal
安装完成后将提示进行参数配置
执行/etc/init.d/oracle-xe configure
将设置HTTP 端口,默认是8080,此处设置为8081,为了不和tomcat冲突
设置Oracle服务端口使用默认的1521
最后是设置sys和system的密码
系统提示是否开机自动运行oracle,选择yes
最后重启debian
通过http://127.0.0.1:8081/apex/或http://localhost:8081/apex进行管理
在http://tomcat.apache.org/download-60.cgi
下载apache-tomcat-6.0.18.tar.gz
拷贝到/usr/soft/tomcat下
并在目录/usr/soft/tomcat下
执行tar –xzvf apache-tomcat-6.0.18.tar.gz
将解压生成apache-tomcat-6.0.18文件夹,为了简单,将其更名为tomcat6
mv apache-tomcat-6.0.18 tomcat6
如果已经设置好JRE_HOME环境变量,则可在目录/usr/soft/tomcat/tomcat6/bin下执行
./startup.sh驱动tomcat
./shutdown.sh关闭tomcat
设置环境变量TOMCAT_HOME、CATALINA_HOME,修改/etc/profile文件如下:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/games"
fi
if [ "$PS1" ]; then
if [ "$BASH" ]; then
PS1='u@h:w$ '
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
PATH=$PATH:/usr/soft/java/jdk/bin:/usr/soft/java/jdk/jre/bin
JAVA_HOME=/usr/soft/java/jdk
JRE_HOME=/usr/soft/java/jdk/jre
CLASSPATH=.:/usr/soft/java/jdk/lib/tools.jar:/usr/soft/java/jdk/lib/dt.jar
TOMCAT_HOME=/usr/soft/tomcat/tomcat6
CATALINA_HOME=/usr/soft/tomcat/tomcat6
export PATH
export JAVA_HOME
export JRE_HOME
export CLASSPATH
export TOMCAT_HOME
export CATALINA_HOME
umask 022
在www.eclipse.org
下载eclipse-jee-ganymede-linux-gtk.tar.gz
安装:
在拷贝eclipse-jee-ganymede-linux-gtk.tar.gz到/usr/soft/eclipse目录下,cd到该目录,执行:
tar -zvxf eclipse-jee-ganymede-linux-gtk.tar.gz
在/usr/soft/eclipse下生成eclipse包,进入eclipse下,即/usr/soft/eclipse/eclipse下,执行./eclipse,将运行eclipse出错,提示:“no java virtual machine was found after searching the following locations:”
解决办法:
原因是没找到jre,解决办法是,进入/usr/soft/eclipse/eclipse目录,建立软连接,连接到jre,
ln -s /usr/soft/java/jdk/jre jre
debiangary:/usr/soft/eclipse/eclipse# ln -s /usr/soft/java/jdk/jre jre
在http://java.sun.com/javase/downloads/ea/6u10/6u10rcDownload.jsp#6u10JREs
下载jdk-6u10-rc-bin-b28-linux-i586-21_jul_2008.bin
拷贝到/usr/soft/java下
并在目录/usr/soft/java下
执行./ jdk-6u10-rc-bin-b28-linux-i586-21_jul_2008.bin将开始安装
安装完成后,在目录/usr/soft/java下下生成jdk1.6.0_10文件夹
为了方便,将其重命名为jdk
mv jdk1.6.0_10 jdk
设置环境变量PATH、JAVA_HOME、JRE_HOME、CLASSPATH,修改/etc/profile文件如下:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/games"
fi
if [ "$PS1" ]; then
if [ "$BASH" ]; then
PS1='u@h:w$ '
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
PATH=$PATH:/usr/soft/java/jdk/bin:/usr/soft/java/jdk/jre/bin
JAVA_HOME=/usr/soft/java/jdk
JRE_HOME=/usr/soft/java/jdk/jre
CLASSPATH=.:/usr/soft/java/jdk/lib/tools.jar:/usr/soft/java/jdk/lib/dt.jar
TOMCAT_HOME=/usr/soft/tomcat/tomcat6
CATALINA_HOME=/usr/soft/tomcat/tomcat6
export PATH
export JAVA_HOME
export JRE_HOME
export CLASSPATH
export TOMCAT_HOME
export CATALINA_HOME
umask 022
笔者先完成Debian的安装后(在Windows XP下的VMWare中安装),首先考虑到是怎么让Windows和Debian之间的资源实现共享。
没用Samba,直接想到了安装SSH
即可用在windows下远程控制Debian,又可以将windows下的文件等直接拷贝到debian下。
安装Debian的SSH:
执行apt-get install openssh-server
将cd1的iso文件引入到VMWare中。
虚拟机à设置
点击CD-ROM(IDE 1:0),设置如下:
另:
运行安装命令:apt-get install openssh-server 提示有错误,请教了高人,修改/etc/apt/source.list 把源修改为国内的debian.cn99.com就可以正常运行了
apt-get update apt-get install openssh-server
具体的etc/apt/source.list 的配置文件请见附件。(将此文件,不带txt后缀的覆盖掉原来的source.list)
启动/停止服务:/etc/init.d/ssh start|stop|restart
在http://cdimage.debian.org/debian-cd/4.0_r4a/i386/iso-cd/
下载debian-40r4a-i386-DVD-1.iso
放到Windows的目录下:
E:XunleiDebian_DVD_CDdebian-40r4a-i386-DVD-1.iso
安装VMware_ha_vmware.exe后,运行
创建虚拟机
查看全文
下载eclipse-jee-ganymede-linux-gtk.tar.gz
安装:
在拷贝eclipse-jee-ganymede-linux-gtk.tar.gz到/usr/soft/eclipse目录下,cd到该目录,执行:
tar -zvxf eclipse-jee-ganymede-linux-gtk.tar.gz
在/usr/soft/eclipse下生成eclipse包,进入eclipse下,即/usr/soft/eclipse/eclipse下,执行./eclipse,将运行eclipse出错,提示:“no java virtual machine was found after searching the following locations:”
解决办法:
在debian中安装eclipse:
解压到某位置,此处为/usr/soft/eclipse/,运行eclipse时提示“no java virtual machine was found after searching the following locations:”。
原因是没找到jre,解决办法是,进入/usr/soft/eclipse/eclipse目录,建立软连接,连接到jre,
ln -s /usr/soft/java/jdk/jre jre
debiangary:/usr/soft/eclipse/eclipse# ln -s /usr/soft/java/jdk/jre jre
Windows下Subversion和TortoiseSVN构建SVN版本控制
2008-8-13 杨普
1.下载Subversion的window安装版本svn-1.4.6-setup.exe
http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=8100
2.下载TortoiseSVN的TortoiseSVN-1.5.2.13595-win32-svn-1.5.1.msi版本
http://tortoisesvn.net/downloads
3.安装subversion
双击svn-1.4.6-setup.exe,一路next完成安装。
设置环境变量,将subversion对bin设置到path中。如:D:Program FilesSubversionbin
4.安装TortoiseSVN
双击TortoiseSVN-1.5.2.13595-win32-svn-1.5.1.msi,一路next完成安装。
5.创建svn容器Repository
在硬盘上任意位置创建一文件夹,如D:svnRepository,在文件夹上右键
TortoiseSVN->Create repository here
创建成功后将在D:svnRepository下生成一系列文件。
6.配置容器的访问权限
在容器的目录下D:svnRepository的conf中,用记事本打开svnserve.conf文件,找到
# anon-access = read
# auth-access = write
# password-db = passwd
并去掉注释#
注意,去掉注释后,不要留下空格,保持每行顶格,切记!!!
效果如下:
### This file controls the configuration of the svnserve daemon, if you
### use it to allow access to this repository. (If you only allow
### access through http: and/or file: URLs, then this file is
### irrelevant.)
### Visit http://subversion.tigris.org/ for more information.
[general]
### These options control access to the repository for unauthenticated
### and authenticated users. Valid values are "write", "read",
### and "none". The sample settings below are the defaults.
anon-access = read
auth-access = write
### The password-db option controls the location of the password
### database file. Unless you specify a path starting with a /,
### the file's location is relative to the directory containing
### this configuration file.
### If SASL is enabled (see below), this file will NOT be used.
### Uncomment the line below to use the default password file.
password-db = passwd
### The authz-db option controls the location of the authorization
### rules for path-based access control. Unless you specify a path
### starting with a /, the file's location is relative to the the
### directory containing this file. If you don't specify an
### authz-db, no path-based access control is done.
### Uncomment the line below to use the default authorization file.
# authz-db = authz
### This option specifies the authentication realm of the repository.
### If two repositories have the same authentication realm, they should
### have the same password database, and vice versa. The default realm
### is repository's uuid.
# realm = My First Repository
[sasl]
### This option specifies whether you want to use the Cyrus SASL
### library for authentication. Default is false.
### This section will be ignored if svnserve is not built with Cyrus
### SASL support; to check, run 'svnserve --version' and look for a line
### reading 'Cyrus SASL authentication is available.'
# use-sasl = true
### These options specify the desired strength of the security layer
### that you want SASL to provide. 0 means no encryption, 1 means
### integrity-checking only, values larger than 1 are correlated
### to the effective key length for encryption (e.g. 128 means 128-bit
### encryption). The values below are the defaults.
# min-encryption = 0
# max-encryption = 256
配置访问用户列表,用户名加密码
记事本打开该目录下的文件passwd
去掉
# harry = harryssecret
# sally = sallyssecret
的注释#,同意注意每行要顶格,开头也不能有空格。
说明harry = harryssecret表示:用户名=密码
当然也可以自己增加,如gary=gary
最好效果如下:
### This file is an example password file for svnserve.
### Its format is similar to that of svnserve.conf. As shown in the
### example below it contains one section labelled [users].
### The name and password for each user follow, one account per line.
[users]
harry=harryssecret
sally=sallyssecret
gary=gary
7.启动Subversion
因为之前设置了环境变量path= D:Program FilesSubversionbin;因此打开cmd,直接运行命令:svnserve -d -r D:svnRepository
这样Subversion服务器就假设好了。
8.向容器添加需版本控制的文件(也可以远程,只需url为ip就行)
在硬盘上任意位置,右键需要上传的文件的上级文件夹,TortoiseSVN->Import…
此处为了测试,创建文件夹D:importFiles,里面放一doc文件test.doc,即要将test.doc上传到svn中去。右键importFiles,TortoiseSVN->Import…,输入容器的url:
svn://localhost/svnRepository
输入用户名和密码:
就将test.doc文件上传到svn容器了
9.从容器获得test.doc文件(也可以远程,只需url为ip就行)
在任意位置,某文件上右键D:checkOutFiles,点击SVN Checkout…
输入url即可
10.完成
这样就完成的部署和存取,修改相应的文件,提交即可,注意始终保持Subversion的运行。
注意,在第8步中会报错“期望文件系统格式“2”;发现格式“3””
原因是TortoisSVN的版本过高,或与Subversion不匹配。
应该换成低版本的TortoisSVN 用TortoiseSVN-1.4.8.12137-win32-svn-1.4.6.msi
即应该为svn-1.4.6-setup.exe和TortoiseSVN-1.4.8.12137-win32-svn-1.4.6.msi的组合才行,步骤和之前的一样,只是在第6步中的svnserve.conf文件中没有后面的内容
[sasl]
### This option specifies whether you want to use the Cyrus SASL
### library for authentication. Default is false.
### This section will be ignored if svnserve is not built with Cyrus
### SASL support; to check, run 'svnserve --version' and look for a line
### reading 'Cyrus SASL authentication is available.'
# use-sasl = true
### These options specify the desired strength of the security layer
### that you want SASL to provide. 0 means no encryption, 1 means
### integrity-checking only, values larger than 1 are correlated
### to the effective key length for encryption (e.g. 128 means 128-bit
### encryption). The values below are the defaults.
# min-encryption = 0
# max-encryption = 256
在原有项目中添加Quartz1.6时总报错如下(如图一所示):
java.lang.NoSuchMethodError: org.apache.commons.collections.SetUtils.orderedSet(Ljava/util/Set;)Ljava/util/Set;
原系统是整合Struts2.0.11.1+Spring2.5.2+Hibernate3.2.6GA,在MyEclipse5.5GA下开发的,集成了DWR2.0.3。
查看全文
使用Apache Directory 的LDPA连接JOSSO验证
目 录
二、Apache Directory Studio(Client)连接Apache Directory Server(Server) 5
三、用Apache Directory Studio增加用户和角色信息的Entry. 8
四、配置Josso的josso-gateway-config.xml 15 查看全文
大家好,感谢大家对我的支持,短短的时间内,本站访问量已经突破上万的访问记录,可见大家对java开源项目的热情。
以后我将更多的发布贴近开发的东西,供大家交流,谢谢!
查看全文Stripes是一个以让程序员的web开发简单而高效为准则来设计的基于动作的开源Java web框架。传统的Java web开发着眼于借去耦(Decoupling)来实现其灵活性,但导致多个的配置文件,额外的对象,和其他资源的分散。这些困难造成相当多的程序员的更高的学习时间和低下的效率。其结果是有些Java程序员被一些非Java的框架所吸引去了:Ruby on Rails或者Django。一些Java web框架,如Stripes,正在开始从这些非Java框架中汲取其成功经验:简单而高效的开发。本文将介绍Stripes与其它如Struts之类基于动作的框架的区别和其提供的一些存在于Ruby on Rails之中的简单性。
图1是典型的用Stripes写的应用程序中的正常事件流程和组件。
如你所见,其流程基本上就是一个MVC框架。Stripes和其他的基于动作的框架的一个主要的区别是没有一个外部的配置文件。我们随后将看到,Stripes用annotation和约定而非配置来提高产出和减少杂乱。
编写你的第一个Stripe动作(Action)
让我们现在就开始通过创建Hello World例程来了解Stripes框架和理解其运作。HelloWorldAction类将提示用户输入姓氏和名字然后在另一个View里面显示,首先我们来编写controller类。
public class HelloWorldAction implements ActionBean { @ValidateNestedProperties({ public void setPerson(String person) {this.person = person;} |
Controller类是一个实现了Stripes特有接口ActionBean的POJO(Plain Old Java Object,译注:读破粥)。所有的Stripes动作类都要实现这一接口以让StripesDispatcher servlet在运行服务时为其注入一个ActionBeanContext对象。ActionBeanContext对象可以让你存取的对象如request、response、和servlet context等servlet API。大多数时候在Stripes应用中是不用读取这些底层API对象的。ActionBeanContext类还提供当前动作的状态并可以添加信息消息和错误消息到当前动作中。ActionBeanContext的变量和其读写方法可以放在一个基类里面,因为所有的Stripes动作都要实现之。
Controller类的其他部分对于任何Java程序员来说都是很面熟的。有一个Person对象和其读写方法是用来读写用户的姓名给view的。虽然这仅仅是一个简单的嵌套对象,Stripes可以通过Java集合、泛型支持、和下标化的属性来实现更复杂完善的数据捆绑。因为Stripes可以处理复杂数据捆绑,你的领域对象(Domain Object)可以在其他需要它们的层重用。例如:通过Stripes你可以很容易的收集一个领域对象的信息,然后用其他的POJO框架,如Hibernate或者EJB3来对其进行持久化。
Person对象变量上有一个Stripes验证annotation用来保证用户在激活hello方法的时候已经输入了姓名。如果用户没有输入这两个必需的变量,原始页会被返回,并显示一个相关的错误消息。该验证只有在hello事件被申请的时候才会被激活,因为annotation的属性中指定了(on = {"hello"})。Stripes还会使用实用默认法则,根据验证方法和变量名称产生一个错误信息。例如,如果Person类的firstName变量在提交的时候没有提供,用户将看到:
Person First Name is a required field.
这条消息是通过将Person.firstName进行刻读化处理后得到的。如果有必要,这些错误消息可以被重载来提供更多的客户自定义功能。
另外还有一个Integer类型的变量age,是Person对象的一个属性。Stripes首先试图对request中命为person.age的parameter转换为Integer类型,并将其捆绑到Person对象上。在Person对象的age变量被付值以后,Stripes将验证该Integer值是否小于13。如果用户输入了一个字符串而非整数,用户得到这个消息:
The value (Mark) entered in field Person Age must be a valid number.
若是用户输入了一个小于13的整数,用户将看到这个消息:
The minimum allowed value for Age is 13.
同样地,我们没有必要为这些错误消息提供任何外部的配置文件。Annotation提供的验证与你的变量在同一个位置上,使得程序员定位验证、理解验证的内容、和对验证进行维护变动更容易。
这个Stripes动作还有两个可被激活的方法(称为事件)。事件是ActionBean类中有如下特征的方法:
public Resolution eventName
请注意index方法被标注为@DefaultHandler annotation。因为在本动作中有多个事件,其中一个必须被指定为默认事件。如果调用本动作的URL没有指定哪个事件,Stripes则查找标注有@DefaultHandler annotation的事件,并执行。
显示层(View)
现在让我们给Hello World例程加上显示层的逻辑。Stripes默认支持JSP为显示层的标准技术,不过你也可以用其他的显示层技术,比如FreeMaker。除了Stripes的tag库以外,没有什么新的东西要学。Hello.jsp是初始的显示,可以让用户输入和提交姓名。
| <%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld" %> ...... <stripes:errors/> <stripes:form beanclass="com. myco. web. stripes. action. example. HelloWorldAction"> Say hello to: <br> First name: <stripes:text name="person.firstName"/> <br> Age:<stripes:text name="person.age"/><br> <stripes:submit name="hello" value="Say Hello"/> </stripes:form> ...... |
<stripes:form action="/example/HelloWorld.action"> 有一个stripes:input tag指定了一个名为person.firstName属性,其作用是将其储存的输入值付给controller的Person对象的firstName变量中。最后,stripes:submit tag指定一个name属性来告诉Stripes的HelloWorldAction类使用哪一个事件。 我们现在已经完成了提交姓名的值给HelloWorldAction,剩下的就是在另一个view中将其反馈给用户了。 <%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld" %> ...... <stripes:errors/> <h2>Hello ${actionBean.person.firstName} your age is ${actionBean.person.age} </h2> <p/> <stripes:link beanclass="com.myco.web.stripes.action. example.HelloWorldAction"> Say Hello Again </stripes:link> ...... 本JSP将自己通过一个对动作的引用读取person的姓名信息并显示。为达到这一目的,Stripes自动在request的属性中添加一个名为actionBean动作对象,以供JSTL存取。最后,我们用了一个strips:link tag来建立一个返回HelloWorldAction地链接从而可以让我们输入不同的姓名。我们以可以通过如下办法显式地创建一个指向index事件的stripes:link: <stripes:link beanclass="com.myco.web.stripes.action. example.HelloWorldAction" event="index">Say Hello Again</stripes:link> 因为我们已经用annotation把index方法标记为@DefaultHandler,Stripes无须event属性也知道要执行哪一个方法。 用约定、不用配置文件 我们现在有了Java组件,我们该做配置了,把动作映射的一个URL上,并将其连接到我们的两个view上面去。等一下!我们在用Stripes,我们不需要外部配置文件! 虽然这听来好像好得不像是真的,但这的确是Stripes的一项最具生产效率的功能。Stripes使用约定而非配置文件来映射动作到URL上。我们也无须使用一个外部配置文件来把view映射到一个个标记名字上。这意味着程序员不用再为了一个标记名字——比方说SUCCESS——的实际来源,而在配置文件中跳来跳去了。没有必要在Java和view组件的外部进行配线,因而导致更好地维护性和更高的生产率。 Stripes是如何提供隐式的URL映射而无需在外部配置每一个动作或者而外的annotation呢?这个可以从Stripes在web.xml中的配置以及它是如何通过实用默认法建立URL映射来解释。首先,我们来看看Servlet过滤器:StripesFilter。其在web.xml中的默认配置如下: <filter> <display-name>Stripes Filter</display-name> <filter-name>StripesFilter</filter-name> <filter-class> net.sourceforge.stripes.controller.StripesFilter </filter-class> <init-param> <param-name>ActionResolver.UrlFilters</param-name> <param-value>/WEB-INF/classes</param-value> </init-param> </filter>
当Servlet容器启动的时候,StripesFilter对其init-param元素执行初始化。其中最重要的init-param元素就是ActionResolver.UrlFilters参数。这个参数告诉Stripes到哪里查找跟Stripes有关的类。这个例子里面,Stripes将查找/WEB-INF/classes目录下的所有实现ActionBean接口的类。每一个被找到的类和其绑定的URL都将被加入一个Map中。
让我们来看看Stripes是如何处理我们的HelloWorldAction动作为例子吧。因为HelloWorldAction类位于/WEB-INF/classes目录下,所以会被认为是一个Stripes servlet。在我们的例子当中,其完整类名是com.myco.web.stripes.action.example.HelloWorldAction。随后,其完整类名将按照如下法则被翻译成一个URL绑定。
现在Stripes找到了ActionBean类并为其创建了一个URL绑定,然后存放在一个java.util.Map<String, Class<? extends ActionBean>>之中。其中key参数是URL绑定,value参数是实现ActionBean的类名。下面是我们的例子中的Map:
URL绑定:/example/HelloWorld.action
ActionBean类:com.myco.web.stripes.action.example.HelloWorldAction
我们要看的第二个组件是Stripes如何把URL绑定翻译成你正在做的这个ActionBean类。这是Stripes调度servlet的职责,在web.xml中的配置如下:
| <servlet> <servlet-name>StripesDispatcher</servlet-name> <servlet-class> net.sourceforge.stripes.controller.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StripesDispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> |
StripesDispatcher的一个职责就是将URL解析为Stripes的ActionBean类。当用户激活URL http://host/uri/example/HelloWorld.action的时候,Stripes调度servlet将在URL映射表中查询并找到com.myco.web.stripes.action.example.HelloWorldAction类,并实例化产生该类的一个实例。最后,index方法被激活,因为在annotation中它被定义为默认局柄而在该URL中并没有指定一个事件。
如果我们想要直接执行HelloWorldAction中的hello方法怎么办?应该象下面这个URL这样把事件的名字放在request的参数中:
http://host/uri/example/HelloWorld.action?hello=&firstName=Mark&age=13
请注意我们没有给hello这个request参数名指定任何值。在这个情况下,StripesDispatcher会把hello这个这个request参数名和HelloWorldAction类中个一个带有public Resolution hello()签名的函数识别并映射。该方法名城在初始化的时候为了性能而缓存在另一个Map中。
我们已经看到Stripes的基础以及如果创建简单的动作和一些该框架是如何运作的细节。通过在web.xml中的初始化,我们能够避免用多个单独的XML配置文件来把我们的显示层组建写在一起。这很重要,原因如下:首先,如果你需要任何改动,你可以看到一个URL就立即知道你该查看哪一个类。其次,我们不需要任何单独的工具来在你的配置文件过大而且不可管理的时候帮助你。通过消除掉配置文件,我们不再需要给框架一大堆的metadata。最后,我们不再需要为一个独立的用来描述我们的组件是如果相互关联的文件来一刻不停维护了。
Ajax
要不要更高级的功能?那我们就来看看Stripes是怎么处理Ajax的。我们将把先前的Hello World例程改成使用Ajax调用Stripes动作。本例子的源代码可以在本文资源中找到。首先,我们对Hello.jsp作改动使其引用Prototype JavaScript函数库。我们还要为Ajax调用增加一个JavaScript函数,并更改提交按钮为其添加一个onclick事件:
<%@ taglib prefix="stripes" <script type="text/javascript"> Age:<stripes:text name="person.age"/><br> <stripes:button <div id="hello"></div> </stripes:form> |
| public Resolution sayHelloAjax(){ return new ForwardResolution("SayHelloAjax.jsp"); } |
这个方法没有多少工作,因为Stripes已经承担了姓名内容的绑定。因此,本方法唯一的责任就是转发到一个叫SayHelloAjax.jsp的页面片断上去。该叶面片段的内容如下:
| <h2>Hello ${actionBean.person.firstName} your age is ${actionBean.person.age}!</h2> |
Spring整合
Stripes还内置了对Spring支持。你可以自动地将Spring bean诸如到你的动作中。按照Stripes的风格,除了Spring上下文配置文件以外不需要任何外部配置文件。如果我们Spring的配置文件如下:
| <bean id="personService" parent="abstractTxDefinition"> <property name="target"> <bean class="com.myco.service.impl.PersonServiceImpl"/> </property> </bean> |
要把person服务注入到一个Stripes动作中,得增加一个跟Spring bean的名字一致的属性和setter。Stripes提供了@SpringBean annotation来查询正确的Spring bean以注入到动作之中。下面是我们要在动作类中包含的例子:
private PersonService personService; @SpringBean |
本文无法囊括Stripes的所有高级功能。但是,Stripes有非常完整的文档。Stripes还包含了一个与Tiles类似但无需外部配置文件的layout管理器。另外,拦截器还可以用于生命周期事件的各处、文件上载等等等等。
结论
Stripes是一个既强大又简单的Java web框架。Stripes利用了Java 5的annotation和泛型功能,从而使得Java程序员避免维护外部配置文件并增加工作效率。Stripes可以简化困难的web开发工作,并使得简单的工作更加简单!