??xml version="1.0" encoding="utf-8" standalone="yes"?>精品一区二区日韩,日韩av二区,裸体在线国模精品偷拍http://www.aygfsteel.com/lmseeyou/发现在我走近java之后Q感觉自׃么都不会了!zh-cnSun, 04 May 2025 12:49:43 GMTSun, 04 May 2025 12:49:43 GMT60深度研讨Collection[转]http://www.aygfsteel.com/lmseeyou/archive/2006/02/16/30912.htmlEddie LeeEddie LeeThu, 16 Feb 2006 01:54:00 GMThttp://www.aygfsteel.com/lmseeyou/archive/2006/02/16/30912.htmlhttp://www.aygfsteel.com/lmseeyou/comments/30912.htmlhttp://www.aygfsteel.com/lmseeyou/archive/2006/02/16/30912.html#Feedback0http://www.aygfsteel.com/lmseeyou/comments/commentRss/30912.htmlhttp://www.aygfsteel.com/lmseeyou/services/trackbacks/30912.html

U性表Q链表,哈希表是常用的数据结构,在进行Java开发时QJDK已经为我们提供了一pd相应的类来实现基本的数据l构。这些类均在java.util包中。本文试N过单的描述Q向读者阐q各个类的作用以及如何正用这些类?

Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set
Map
├Hashtable
├HashMap
└WeakHashMap

Collection接口
  Collection是最基本的集合接口,一个Collection代表一lObjectQ即Collection的元素(ElementsQ。一些Collection允许相同的元素而另一些不行。一些能排序而另一些不行。Java SDK不提供直接承自Collection的类QJava SDK提供的类都是l承自Collection的“子接口”如List和Set?BR>  所有实现Collection接口的类都必L供两个标准的构造函敎ͼ无参数的构造函数用于创Z个空的CollectionQ有一个Collection参数的构造函数用于创Z个新的CollectionQ这个新的Collection与传入的Collection有相同的元素。后一个构造函数允许用户复制一个Collection?BR>  如何遍历Collection中的每一个元素?不论Collection的实际类型如何,它都支持一个iterator()的方法,该方法返回一个P代子Q用该q代子即可逐一讉KCollection中每一个元素。典型的用法如下Q?BR>    Iterator it = collection.iterator(); // 获得一个P代子
    while(it.hasNext()) {
      Object obj = it.next(); // 得到下一个元?BR>    }
  由Collection接口z的两个接口是List和Set?/P>

List接口
  List是有序的CollectionQ用此接口能够_的控制每个元素插入的位置。用戯够用烦引(元素在List中的位置Q类g数组下标Q来讉KList中的元素Q这cM于Java的数l?BR>和下面要提到的Set不同QList允许有相同的元素?BR>  除了hCollection接口必备的iterator()Ҏ外,Listq提供一个listIterator()ҎQ返回一个ListIterator接口Q和标准的Iterator接口相比QListIterator多了一些add()之类的方法,允许dQ删除,讑֮元素Q还能向前或向后遍历?BR>  实现List接口的常用类有LinkedListQArrayListQVector和Stack?/P>

LinkedListc?BR>  LinkedList实现了List接口Q允许null元素。此外LinkedList提供额外的getQremoveQinsertҎ在LinkedList的首部或N。这些操作LinkedList可被用作堆栈QstackQ,队列QqueueQ或双向队列QdequeQ?BR>  注意LinkedList没有同步Ҏ。如果多个线E同时访问一个ListQ则必须自己实现讉K同步。一U解x法是在创建List时构造一个同步的ListQ?BR>    List list = Collections.synchronizedList(new LinkedList(...));

ArrayListc?BR>  ArrayList实现了可变大的数组。它允许所有元素,包括null。ArrayList没有同步?BR>sizeQisEmptyQgetQsetҎq行旉为常数。但是addҎ开销为分摊的常数Q添加n个元素需要O(n)的时间。其他的Ҏq行旉为线性?BR>  每个ArrayList实例都有一个容量(CapacityQ,即用于存储元素的数组的大。这个容量可随着不断d新元素而自动增加,但是增长法q没有定义。当需要插入大量元素时Q在插入前可以调用ensureCapacityҎ来增加ArrayList的容量以提高插入效率?BR>  和LinkedList一PArrayList也是非同步的QunsynchronizedQ?/P>

Vectorc?BR>  Vector非常cMArrayListQ但是Vector是同步的。由Vector创徏的IteratorQ虽然和ArrayList创徏的Iterator是同一接口Q但是,因ؓVector是同步的Q当一个Iterator被创且正在被用,另一个线E改变了Vector的状态(例如Q添加或删除了一些元素)Q这时调用Iterator的方法时抛出ConcurrentModificationExceptionQ因此必L莯异常?/P>

Stack c?BR>  Stackl承自VectorQ实C个后q先出的堆栈。Stack提供5个额外的Ҏ使得Vector得以被当作堆栈用。基本的push和popҎQ还有peekҎ得到栈顶的元素,emptyҎ试堆栈是否为空QsearchҎ一个元素在堆栈中的位置。Stack刚创建后是空栈?/P>

Set接口
  Set是一U不包含重复的元素的CollectionQ即L的两个元素e1和e2都有e1.equals(e2)=falseQSet最多有一个null元素?BR>  很明显,Set的构造函数有一个约束条Ӟ传入的Collection参数不能包含重复的元素?BR>  h意:必须心操作可变对象QMutable ObjectQ。如果一个Set中的可变元素改变了自w状态导致Object.equals(Object)=true导致一些问题?/P>

Map接口
  h意,Map没有l承Collection接口QMap提供key到value的映。一个Map中不能包含相同的keyQ每个key只能映射一个value。Map接口提供3U集合的视图QMap的内容可以被当作一lkey集合Q一lvalue集合Q或者一lkey-value映射?/P>

Hashtablec?BR>  Hashtablel承Map接口Q实C个key-value映射的哈希表。Q何非I(non-nullQ的对象都可作ؓkey或者value?BR>  d数据使用put(key, value)Q取出数据用get(key)Q这两个基本操作的时间开销为常数?BR>Hashtable通过initial capacity和load factor两个参数调整性能。通常~省的load factor 0.75较好地实C旉和空间的均衡。增大load factor可以节省I间但相应的查找旉增大,q会影响像get和putq样的操作?BR>使用Hashtable的简单示例如下,?Q?Q?攑ֈHashtable中,他们的key分别是”one”,”two”,”three”:
    Hashtable numbers = new Hashtable();
    numbers.put(“one? new Integer(1));
    numbers.put(“two? new Integer(2));
    numbers.put(“three? new Integer(3));
  要取Z个数Q比?Q用相应的keyQ?BR>    Integer n = (Integer)numbers.get(“two?;
    System.out.println(“two = ?+ n);
  ׃作ؓkey的对象将通过计算其散列函数来定与之对应的value的位|,因此M作ؓkey的对象都必须实现hashCode和equalsҎ。hashCode和equalsҎl承自根cObjectQ如果你用自定义的类当作key的话Q要相当心Q按照散列函数的定义Q如果两个对象相同,即obj1.equals(obj2)=trueQ则它们的hashCode必须相同Q但如果两个对象不同Q则它们的hashCode不一定不同,如果两个不同对象的hashCode相同Q这U现象称为冲H,冲突会导致操作哈希表的时间开销增大Q所以尽量定义好的hashCode()ҎQ能加快哈希表的操作?BR>  如果相同的对象有不同的hashCodeQ对哈希表的操作会出现意想不到的l果Q期待的getҎq回nullQ,要避免这U问题,只需要牢C条:要同时复写equalsҎ和hashCodeҎQ而不要只写其中一个?BR>  Hashtable是同步的?/P>

HashMapc?BR>  HashMap和HashtablecMQ不同之处在于HashMap是非同步的,q且允许nullQ即null value和null key。,但是HashMap视ؓCollectionӞvalues()Ҏ可返回CollectionQ,其P代子操作旉开销和HashMap的容量成比例。因此,如果q代操作的性能相当重要的话Q不要将HashMap的初始化定w讑־q高Q或者load factorq低?/P>

WeakHashMapc?BR>  WeakHashMap是一U改q的HashMapQ它对key实行“弱引用”,如果一个key不再被外部所引用Q那么该key可以被GC回收?/P>

ȝ
  如果涉及到堆栈,队列{操作,应该考虑用ListQ对于需要快速插入,删除元素Q应该用LinkedListQ如果需要快速随问元素,应该使用ArrayList?BR>  如果E序在单U程环境中,或者访问仅仅在一个线E中q行Q考虑非同步的c,其效率较高,如果多个U程可能同时操作一个类Q应该用同步的cR?BR>  要特别注意对哈希表的操作Q作为key的对象要正确复写equals和hashCodeҎ?BR>  量q回接口而非实际的类型,如返回List而非ArrayListQ这样如果以后需要将ArrayList换成LinkedListӞ客户端代码不用改变。这是针对抽象~程?/P>


Eddie Lee 2006-02-16 09:54 发表评论
]]>
tree2试http://www.aygfsteel.com/lmseeyou/archive/2006/01/05/26792.htmlEddie LeeEddie LeeThu, 05 Jan 2006 13:34:00 GMThttp://www.aygfsteel.com/lmseeyou/archive/2006/01/05/26792.htmlhttp://www.aygfsteel.com/lmseeyou/comments/26792.htmlhttp://www.aygfsteel.com/lmseeyou/archive/2006/01/05/26792.html#Feedback1http://www.aygfsteel.com/lmseeyou/comments/commentRss/26792.htmlhttp://www.aygfsteel.com/lmseeyou/services/trackbacks/26792.htmlMyfaces|站tomahawk栏目的Extensions Filter 子栏目中有如下一D话Q?BR>If you just use standard JSF component, but don't use any MyFaces' extended component (beginning with t:),
then you don't need the Extensions Filter.
However, if you use some of the MyFaces' extended components like t:inputFileUpload, t:inputHTtml, t:inputCalendar, ...
then you most likely need to have this filter configured in your webapp.
大概意思是Q如果你在项目中没有使用到Myfaces的扩展组Ӟt:开头的Q,则你不需要配|这个Filter.
可是Q如果你用了Myfaces的扩展组Ӟ你必Mؓ你的webE序配置q个Filter.
配置如下Q?BR>
<filter>
    
<filter-name>MyFacesExtensionsFilter</filter-name>
    
<filter-class>org.apache.myfaces.component.html.util.ExtensionsFilter</filter-class>
    
<init-param>
        
<param-name>maxFileSize</param-name>
        
<param-value>20m</param-value>
        
<description>Set the size limit for uploaded files.
            Format: 10 - 10 bytes
                    10k - 10 KB
                    10m - 10 MB
                    1g - 1 GB
        
</description>
    
</init-param>
</filter>

<!-- extension mapping for adding <script/>, <link/>, and other resource tags to JSF-pages  -->
<filter-mapping>
    
<filter-name>MyFacesExtensionsFilter</filter-name>
    
<!-- servlet-name must match the name of your javax.faces.webapp.FacesServlet entry -->
    
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

<!-- extension mapping for serving page-independent resources (javascript, stylesheets, images, etc.)  -->
<filter-mapping>
    
<filter-name>MyFacesExtensionsFilter</filter-name>
    
<url-pattern>/faces/myFacesExtensionResource/*</ url-pattern>
</filter-mapping>

要用MyFaces的tomahawk控gQ要在页面引?lt;%@ taglib uri="在页面中dtree2的标{:

<t:tree2 id="sortTree" var="node" varNodeToggler="t" showRootNode="false" value="#{tree.treeData}">

</t:tree2>
建立托管Bean,用于填充tree?BR>
public class TreeBacker implements Serializable
{
    
public TreeNode getTreeData ( )
    
{
        TreeNode treeData 
= new TreeNodeBase ( "foo-folder" , "Inbox" , false ) ;

        TreeNodeBase personNode 
= new TreeNodeBase("person""Eddie L"false);
        
//personNode.getChildren().add(new TreeNodeBase("document","Eddie doc one", true));
        TreeNodeBase doc = new TreeNodeBase("document","Eddie doc one"true);
        doc.setIdentifier(
"No.1");
        personNode.getChildren().add(doc);
        treeData.getChildren().add(personNode);
        
        System.out.println ( 
"Create Tree ..ok" ) ;
        
return treeData ;
    }

}

其中TreeNodeBase personNode = new TreeNodeBase("person", "Eddie L", false);中的"person"的作用是表示渲染得时候显CZ么样式,
跟页面中<f:facet name="person"></f:facet>相对应?BR>在页面中

<f:facet name="person">
    
<h:panelGroup>
        
<f:facet name="expand">
            
<t:graphicImage value="../images/person.png" rendered="#{t.nodeExpanded}" border="0" />
        
</f:facet>
        
<f:facet name="collapse">
            
<t:graphicImage value="../images/person.png" rendered="#{!t.nodeExpanded}" border="0" />
        
</f:facet>
        
<h:outputText value="#{node.description}" styleClass="nodeFolder" />
    
</h:panelGroup>
</f:facet>

<t:graphicImage value="../images/person.png" rendered="#{t.nodeExpanded}" border="0" />昄Tree每一个节点的囄。rendered属性的意思是是否渲染的意思,
jsf中渲染的意思是把jsflg树当前的状态{换成html。在jsf的生命周期里Q渲染响应在最后一个周期,renderedgؓfalse在渲染相应这个周期就不运行?BR>在处理叶子节点上Q需要特D的注意。因为对叶子节点是需要操作的。可能需要点d子节点连接到一个页面,或者执行某些action{?BR>
<f:facet name="document">
    
<h:panelGroup>
        
<h:commandLink immediate="true" styleClass="#{t.nodeSelected ? 'documentSelected':'document'}" actionListener="#{t.setNodeSelected}" action="#{tree.okListener}" id="clickbutton">
            
<t:graphicImage value="../images/document.png" border="0" />
            
<h:outputText value="#{node.description}" />
            
<f:param name="docNum" value="#{node.identifier}" />
        
</h:commandLink>
    
</h:panelGroup>
</f:facet>
commandLink标记可以有n个参敎ͼ参数分ؓ参数名和参数倹{在E序里可以读取?BR>~写action事gQokListener
FacesContext context = FacesContext.getCurrentInstance ( ) ;//取得上下?/SPAN>

String a 
= context.getExternalContext ( ).getRequestParameterMap ( )
                .get ( 
"docNum" ).toString ( ) ;

a的值就是docNum的倹{?BR>identifier的值在创徏树的时候一起加q去的:
TreeNodeBase doc = new TreeNodeBase("document","Eddie doc one", true);
doc.setIdentifier("No.1");

q样。一个tree2的应用基本就ok了?/P>

Eddie Lee 2006-01-05 21:34 发表评论
]]>
FacesContext保存lg?/title><link>http://www.aygfsteel.com/lmseeyou/archive/2005/12/31/26169.html</link><dc:creator>Eddie Lee</dc:creator><author>Eddie Lee</author><pubDate>Sat, 31 Dec 2005 03:31:00 GMT</pubDate><guid>http://www.aygfsteel.com/lmseeyou/archive/2005/12/31/26169.html</guid><wfw:comment>http://www.aygfsteel.com/lmseeyou/comments/26169.html</wfw:comment><comments>http://www.aygfsteel.com/lmseeyou/archive/2005/12/31/26169.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lmseeyou/comments/commentRss/26169.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lmseeyou/services/trackbacks/26169.html</trackback:ping><description><![CDATA[FacesContext保存了组件书的一个引用,由它求进行渲染;<BR>贯穿整个h处理生命周期Q你可以通过FacesContext讉Klg树;<BR>而且贯穿整个生命周期Q你可以更改或替换他的当前组件树?img src ="http://www.aygfsteel.com/lmseeyou/aggbug/26169.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lmseeyou/" target="_blank">Eddie Lee</a> 2005-12-31 11:31 <a href="http://www.aygfsteel.com/lmseeyou/archive/2005/12/31/26169.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JSF commandButton的immediate属?/title><link>http://www.aygfsteel.com/lmseeyou/archive/2005/12/29/25956.html</link><dc:creator>Eddie Lee</dc:creator><author>Eddie Lee</author><pubDate>Thu, 29 Dec 2005 13:56:00 GMT</pubDate><guid>http://www.aygfsteel.com/lmseeyou/archive/2005/12/29/25956.html</guid><wfw:comment>http://www.aygfsteel.com/lmseeyou/comments/25956.html</wfw:comment><comments>http://www.aygfsteel.com/lmseeyou/archive/2005/12/29/25956.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lmseeyou/comments/commentRss/25956.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lmseeyou/services/trackbacks/25956.html</trackback:ping><description><![CDATA[把这个属性设|成True,他就会直接执行Action的方法,不执行一些getҎQ不会读取一些必要的信息?BR><BR><img src ="http://www.aygfsteel.com/lmseeyou/aggbug/25956.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lmseeyou/" target="_blank">Eddie Lee</a> 2005-12-29 21:56 <a href="http://www.aygfsteel.com/lmseeyou/archive/2005/12/29/25956.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JSF SelectItems 使用http://www.aygfsteel.com/lmseeyou/archive/2005/12/29/25811.htmlEddie LeeEddie LeeWed, 28 Dec 2005 16:09:00 GMThttp://www.aygfsteel.com/lmseeyou/archive/2005/12/29/25811.htmlhttp://www.aygfsteel.com/lmseeyou/comments/25811.htmlhttp://www.aygfsteel.com/lmseeyou/archive/2005/12/29/25811.html#Feedback2http://www.aygfsteel.com/lmseeyou/comments/commentRss/25811.htmlhttp://www.aygfsteel.com/lmseeyou/services/trackbacks/25811.html
在托Bean里初始化q个标签的时候,可以使用SelectItem[] 数组cdQ也可以使用List 、Map{类型?BR>
具体做法如下Q?BR>
private List<SelectItem> okItems = null;
    
/**
     * 
@return Returns the okItems.
     
*/

public List < SelectItem > getOkItems ( )
    
{
        
this.okItems = new LinkedList< SelectItem >();
        
this.okItems.add(new SelectItem("a","aaa"));
        
this.okItems.add(new SelectItem("b","bbb"));
        
return okItems ;
    }

然后在jsp面中:
<h:selectOneMenu id="sortpid" value="#{booksort.sortPid}">
    
<f:selectItems value="#{booksort.okItems}" />
</h:selectOneMenu>

selectItems他拥有一个valueq个属性,赋gؓ托管Bean里的初始化下拉框的那个属性,也就是okItems?img src ="http://www.aygfsteel.com/lmseeyou/aggbug/25811.html" width = "1" height = "1" />

Eddie Lee 2005-12-29 00:09 发表评论
]]>
MyEclipseq行MyFaces出现I白面的问题,我的解决Ҏ?/title><link>http://www.aygfsteel.com/lmseeyou/archive/2005/12/18/24498.html</link><dc:creator>Eddie Lee</dc:creator><author>Eddie Lee</author><pubDate>Sun, 18 Dec 2005 07:39:00 GMT</pubDate><guid>http://www.aygfsteel.com/lmseeyou/archive/2005/12/18/24498.html</guid><wfw:comment>http://www.aygfsteel.com/lmseeyou/comments/24498.html</wfw:comment><comments>http://www.aygfsteel.com/lmseeyou/archive/2005/12/18/24498.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lmseeyou/comments/commentRss/24498.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lmseeyou/services/trackbacks/24498.html</trackback:ping><description><![CDATA[      用MyEclipse创徏一个Web Project的项目,然后让这个项目支持MyFaces。运行,l果不报错而且只显C出一个空白的面Q右键查看网늚源代码,里面只显C出了网늚一些基本html代码Q没有出CQ何有关jsf的一些内宏V然而,我只让他支持JSFQ却一切正常,什么东襉K昄出来了?BR><BR>      发现q个问题以后Q去csdn和QQ的群里面问了很多人,都没有出现过q些问题。我都开始纳闷了。。。。?BR><BR>      然后我做了一个实验,我把MyFaces官方的blank代码直接复制到tomcat的webapp目录却正常显CZ。我把这个blank的代码全部复制到MyEclipse的项目里Q然后同步服务器Q同L问题又出现了,q是出现了一个大白页面。。这个问题整整让我郁闷了好几天,到底是我配置的问题呢Q还是MyEclipse插g的问题呢Q因为我把这个blank的代码直接放到webapp目录里,却一切正常,我想q说明我的配|应该是没有错误的。那问题出在MyEclipse上了?BR><BR>      然后我在Eclipse的选项里面查看MyEclipse的选项Q找CProject Capabilities里有对jsf目的一些jar的设|,在这个选项里面我发现在下方有一个copy jsf jar and package Tlds to /WEB-INF/lib, if not present 的复选框Q把q个勾打掉,然后再用MyEclipse建立web Project目Q然后支持MyFaces,自己手工的把blank的所有jar都复制到/WEB-INF/lib里,同步服务器。哇靠了Q?亲爱的MyFaceslg都出来了Q?BR><BR>      到目前ؓ止,我还没有弄明白,Z么用MyEclipse带的所有jar׃出现q些问题Q我也仔l察看了Q新建的MyFaces目里的classpath都没有jsf-api.jarQ最后还使用了最土的办法Q自己手工copy那些jar?BR><BR>哪位大兄弟也遇到q这L问题Q还有更好的解决ҎQ一定要告诉我啊。还有就是,错误的根在哪里呢Q我q没弄明白。我觉得MyEclipse他自带的那些jar按道理来说不应该有问题的。可能原因还在其它的地方?img src ="http://www.aygfsteel.com/lmseeyou/aggbug/24498.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lmseeyou/" target="_blank">Eddie Lee</a> 2005-12-18 15:39 <a href="http://www.aygfsteel.com/lmseeyou/archive/2005/12/18/24498.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]JavaServer Faces介绍http://www.aygfsteel.com/lmseeyou/archive/2005/12/16/24118.htmlEddie LeeEddie LeeFri, 16 Dec 2005 01:12:00 GMThttp://www.aygfsteel.com/lmseeyou/archive/2005/12/16/24118.htmlhttp://www.aygfsteel.com/lmseeyou/comments/24118.htmlhttp://www.aygfsteel.com/lmseeyou/archive/2005/12/16/24118.html#Feedback1http://www.aygfsteel.com/lmseeyou/comments/commentRss/24118.htmlhttp://www.aygfsteel.com/lmseeyou/services/trackbacks/24118.html
  首先Q一?JSF 应用是一?servlet/JSP 应用。它有一个配|描q符Q有 JSP 面、客户定制标{、静态资源等{。不同的是,JSF 应用是事仉动的。你通过写一个事件侦听类来决定应用程序的行ؓ。以下徏立一?JSF 应用所需要的几个步骤Q?BR>1、徏?JSP 面Q用 JSF lg包装 HTML 元素?BR>2、写一?JavaBean 用来保持用户输入与组件数据的状态?BR>3、写一个事件侦听器来决定当某事件发生时应该有什么反映,比如用户点击了一个按钮或者提交了表单。JSF 支持两个事gQActionEvent ?ValueChangeEvent 。ActionEvent 是针对用h交表单与点击按钮的,?ValueChangeEvent 是当一?JSF lg改变了时触发?BR>
  现在Q让我们来看一?JSF 动作的细节?BR>

JSF 怎样工作

  JSP 面?JSF 应用的用h口。每个页面包括一?JSF lg用来描述 WEB 控gQ如表单、输入框、按钮等{。组件可以嵌入另一个组件中Q正如输入框可以在表单中。每?JSP 面p栯CZؓlg树。JaveBeans 从用Lh中获取数据ƈ存储?BR>
  q是有意思的部分Q每当用户做M事情Q如点击按钮或者提交表单,都有事g产生。然后事件消息通过 HTTP 传到服务器。在服务器端Q是一个配|了叫做 Faces servlet 的特D?servlet ?WEB 容器。Faces servlet(javax.faces.webapp.FacesServlet)是所?JSF 应用的引擎。每?JSF 应用?WEB 容器中都有独立的 Faces servlet 。另一个重要的对象?javax.faces.context.FacesContext , 它包括了所有关于当前用戯求的必要信息?BR>
  Faces servlet 的后台处理是相当复杂的。然而你没有必要了解q些l节Q只需要记住:Faces servlet ?JSP 面创徏了组件树Q对lg树的控制又对应着事g。Faces servlet 知道怎么d建组件树Q因为它已经讉K了当前应用中所有的 JSP 面。Faces servlet q会创徏一?Event 对象Qƈ把它传递给所有注册过的侦听器。你可以通过与当前请求相对应?FacesContext 得到q个面的组件树?BR>
  客户端浏览器?WEB 控g产生的事Ӟ被包含在一?HTTP h中,攑֜一赯有如览器类型、请求地址{其它信息。因此,所有需?Faces servlet 处理的请求必L向这?servlet 。那你怎样通过调用 Faces servelt 来处理每?HTTP h呢?很容易,只需要在配置描述W里用一?servlet-mapping 元素把一个特D的 URL 式样映射?Faces servlet。通常Q你会用?/faces/* 样式Q如下所C:
<!-- Faces Servlet -->
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Faces Servlet Mapping -->
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
  h地址必须包含有在 <url-pattern> 元素中描q的样式。这个要求不Ҏ辑ֈ。另外也需要注意的?<servlet> 元素Q它包含 Faces servlet Q有一?<load-on-startup> 元素Q用来确是否应用E序W一ơ启动时 servlet 是否加蝲?BR>  Z捕获lg产生的事Ӟ你需要ؓq个lg写一个侦听器Qƈ把它注册l这个组件。通过在表C组件的客户端标{中嵌入 <action_listener> 元素能做到这一炏V例如,Z让一个名?jsfApp.MyActionListener 的事件侦听器Q来捕获一个名?submitButton 的命令按钮生的事gQ在你的 JSP 面中写如下的代码即可:
<h:command_button id="submitButton" label="Add" commandName="submit" >
  <f:action_listener type="jsfApp.MyActionListener" />
</h:command_button>
  一?action listener 必须实现 javax.faces.event.ActionListener 接口Q而一?value-changed listener 必须实现 java.faces.event.ValueChangedLister 接口。下面让我们来创Z个简单的 JSF 应用Q以展现 JSF 是怎么样事仉动的?BR>

一个简单的 JSF 应用

  我们创Z个简单的应用Q它可以实现对二个数字相加。ؓ了运行这个应用,你需要准?TOMCAT5 ?JSF v1.0 EA4(包含?Java Web Services Developer Pack (JWSDP) 1.2?。这个应用程序包括:
adder.jsp  JSP 面?BR>NumberBean 存放用户数据?JavaBean
MyActionListener 事g侦听?BR>web.xml 配置描述文g
  Z使这个应用能正常工作Q还需要几?jar 文gQ包?JSF 标准实现与其它类库。如果你安装?JWSDP 1.2Q你可以在 jsflib 目录下找到所需要的q些文g。把q些 .jar 文g拯?WEB-INF/lib 目录下。下面是整个?.jar ?.tld 文g列表Q?BR>jsf-api.jar 包含?Faces servlet 与其它相?javax.faces 包下面的c?BR>jfs-ri.jar ?JSF 的参考实?BR>jstl_el.jar
standard.jar
  此外Q一?JSF 的应用还需要如下的cdQ它们是 Apache Jakarta 目的一部分Q?BR>commons-beanutils.jar
commons-digester.jar
commons-logging.jar is


以下的几段讨论q个 JSF CZ的每个部分。最后的一段Q“编译与q行”,解释 JSF 应用怎么栯行?BR>
创徏目录l构

  首先Z?JSF 应用创徏一个目录结构。在 TOMCAT 中,它在 webapps 目录下。“图1”描qC叫做 myJSFApp 的应用程序的目录l构?BR>

写配|描q符

  与其它的 servlet/JSP 应用一Pq个应用E序也需要一个配|描q文件。如“清?”表C?BR>
Listing 1. The deployment descriptor (the web.xml file)

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <!-- Faces Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>

    <!-- Faces Servlet Mapping -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
</web-app>

  在这个配|描q文件中有二个部分?<servlet> 元素注册 Faces servlet Q?<servlet-mapping> 元素声明M包含?/faces/ 式样的请求地址Q必M递给 Faces servlet ?BR>
创徏 JSP 面

  一个叫?adder.jsp ?JSP 面提供用户接口Q如“清?”所C:

Listing 2. The adder.jsp page

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>Add 2 numbers</title>
</head>
<body>
<jsp:useBean id="NumberBean" class="jsfApp.NumberBean" scope="session" />
<f:use_faces><br />
    <h:form id="addForm" formName="addForm" ><br />
        First Number:<br />
        <h:input_number id="firstNumber" valueRef="NumberBean.firstNumber" /><br />
        Second Number:
        <h:input_number id="secondNumber" valueRef="NumberBean.secondNumber" /><br />
        Result:
        <h:output_number id="output" valueRef="NumberBean.result"/><br>
        <h:command_button id="submitButton" label="Add" commandName="submit" >
            <f:action_listener type="jsfApp.MyActionListener" />
        </h:command_button>
    </h:form>
</f:use_faces>
</body>
</html>

  我们首先定义了俩个标{,它用?JSF 的两个标{ֺQhtml ?core 。这俩个标签库的定义可以?jsf-ri.jar 文g中找刎ͼ所以你不用为它担心。它们的前缀分别?h / f ?BR><%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
   <jsp:useBean> q个动作元素定义 NumberBean JavaBean ?session scope ?BR>   <jsp:useBean id="NumberBean" class="jsfApp.NumberBean" scope="session" />
  接着?JSF 控g了。注?JSF 控g需要嵌入到 <f:use_faces> 标签中:
<f:use_faces>
...
</f:use_faces>
  在这里面Q有一个表单?BR><h:form id="addForm" formName="addForm">
...
</h:form>
  内嵌在这个表单里的是二个 input_numbers, 一?output_number, 与一?command_button ?BR>W一个数?
<h:input_number id="firstNumber" valueRef="NumberBean.firstNumber" /><br />
W二个数?
<h:input_number id="secondNumber" valueRef="NumberBean.secondNumber" /><br />
l果:
<h:output_number id="output" valueRef="NumberBean.result" /><br />
<h:command_button id="submitButton" label="Add" commandName="submit">
    <f:action_listener type="jsfApp.MyActionListener" />
</h:command_button>
  注意命o按钮的事件侦听器。“图2”描qCq个 JSP 面的组件树(树根省略)?BR>
  ȝ件是表单Q它有四个子lg?BR>
写对象模?/B>

  在这个应用中Q你需要用一?JavaBean 来存二个数字与相加的l果。“清?”是q个 JavaBean 的内容:NumberBean

Listing 3. The NumberBean JavaBean

package jsfApp;
public class NumberBean {
    int firstNumber  = 0;
    int secondNumber = 0;

    public NumberBean () {
        System.out.println("Creating model object");
    }

    public void setFirstNumber(int number) {
        firstNumber = number;
        System.out.println("Set firstNumber " + number);
    }

    public int getFirstNumber() {
        System.out.println("get firstNumber " + firstNumber);
        return firstNumber;
    }

    public void setSecondNumber(int number) {
        secondNumber = number;
        System.out.println("Set secondNumber " + number);
    }

    public int getSecondNumber() {
        System.out.println("get secondNumber " + secondNumber);
        return secondNumber;
    }

    public int getResult() {
        System.out.println("get result " + (firstNumber + secondNumber));
        return firstNumber + secondNumber;
    }
}


写事件侦听器

  命o按钮的事件侦听器是这?JSF 应用的最有趣的部分。它表述一个事件怎么样引起一个侦听器M听。侦听器单地只是输出信息到控制台。然而,它显CZ重要的信息,?JSP 面lg树的层次l构Q正是这些组件触发了事g。“清?”展CZ件侦听器Q?BR>
Listing 4. The action listener for the command button (MyActionListener.java)

package jsfApp;

import java.util.Iterator;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.event.PhaseId;
import javax.faces.tree.Tree;

public class MyActionListener implements ActionListener {

    public PhaseId getPhaseId() {
        System.out.println("getPhaseId called");
        return PhaseId.APPLY_REQUEST_VALUES;
    }
  
    public void processAction(ActionEvent event) {
        System.out.println("processAction called");

        // the component that triggered the action event
        UIComponent component = event.getComponent();
        System.out.println("The id of the component that fired the action event: "
            + component.getComponentId());

        // the action command
        String actionCommand = event.getActionCommand();
        System.out.println("Action command: " + actionCommand);
  
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Tree tree                 = facesContext.getTree();
        UIComponent root          = tree.getRoot();

        System.out.println("----------- Component Tree -------------");
        navigateComponentTree(root, 0);
        System.out.println("----------------------------------------");
    }
  
    private void navigateComponentTree(UIComponent component, int level) {
        Iterator children = component.getChildren();

        // indent
        for (int i=0; i<level; i++)
            System.out.print("  ");

        // print component id
        System.out.println(component.getComponentId());

        // navigate children
        while (children.hasNext()) {
            UIComponent child = (UIComponent) children.next();
            navigateComponentTree(child, level + 1);
        }
    }
}

~译与运?/B>

  Z~译q个应用Q我们{?myJSFApp/WEB-INF/classes q个目录。如果你用的?windows pȝQ打出如下命令:
javac -classpath ../lib/jsf-api.jar;../lib/jsf-ri.jar;../../../../common/lib/servlet.jar jsfApp/*.java
  注意你必ȝ?lib 目录下的cd?servlet.jar 库?BR>
  然后q行 Tomcat Q在地址栏输入如下地址Q?BR>http://localhost:8080/myJSFApp/faces/adder.jsp
  注意你在 JPS 面文g名前用了 /faces/ 式样。然后可以在览器中看到”如?“所C:
image
  在控制台Q你可以看到如下信息Q?BR>Model Object Created
get firstNumber 0
get secondNumber 0
get result 0
getPhaseId called
  现在在二个输入框中分别输入二个数字,然后点击 ADD 按钮。浏览器显C结果“如?”:
image
  更重要的Q再查一下控制台Q看到如下信息:
get firstNumber 0
get secondNumber 0
processAction called
The id of the component that fired the action event: submitButton
Action command: submit
----------- Component Tree -------------
null
    addForm
        firstNumber
        secondNumber
        output
        submitButton
----------------------------------------
Set firstNumber 10
Set secondNumber 20
get firstNumber 10
get secondNumber 20
get result 30


ȝ
  在这文章中Q你学到?JSF 区别于其?servlet/JSP 应用的最重要的特点:事g驱动。你也创Z一个包含一?JSP 面的简单应用。更重要的,你写了能响应事g的侦听器?BR>  实际应用中的 JSF 应用复杂得多Q通常是很?JSP 面。这h况下Q你需要从一个页面导航到另一个页面。然而这应是另一文章的主题?img src ="http://www.aygfsteel.com/lmseeyou/aggbug/24118.html" width = "1" height = "1" />

Eddie Lee 2005-12-16 09:12 发表评论
]]>
linux的树型目录结?/title><link>http://www.aygfsteel.com/lmseeyou/archive/2005/12/05/22644.html</link><dc:creator>Eddie Lee</dc:creator><author>Eddie Lee</author><pubDate>Mon, 05 Dec 2005 15:26:00 GMT</pubDate><guid>http://www.aygfsteel.com/lmseeyou/archive/2005/12/05/22644.html</guid><wfw:comment>http://www.aygfsteel.com/lmseeyou/comments/22644.html</wfw:comment><comments>http://www.aygfsteel.com/lmseeyou/archive/2005/12/05/22644.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lmseeyou/comments/commentRss/22644.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lmseeyou/services/trackbacks/22644.html</trackback:ping><description><![CDATA[linux采用的是树型l构。最上层是根目录Q其他的所有目录都是从根目录出发而生成的。微软的DOS和windows也是采用树型l构Q但是在DOS和windows中这L树型l构的根是磁盘分区的盘符Q有几个分区有几个树型l构Q他们之间的关系是ƈ列的。但是在linux中,无论操作pȝ理几个盘分区Q这L目录树只有一个。从l构上讲Q各个磁盘分Z的树型目录不一定是q列的?<img src ="http://www.aygfsteel.com/lmseeyou/aggbug/22644.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lmseeyou/" target="_blank">Eddie Lee</a> 2005-12-05 23:26 <a href="http://www.aygfsteel.com/lmseeyou/archive/2005/12/05/22644.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux中的一些主要目录的作用http://www.aygfsteel.com/lmseeyou/archive/2005/12/05/22641.htmlEddie LeeEddie LeeMon, 05 Dec 2005 15:02:00 GMThttp://www.aygfsteel.com/lmseeyou/archive/2005/12/05/22641.htmlhttp://www.aygfsteel.com/lmseeyou/comments/22641.htmlhttp://www.aygfsteel.com/lmseeyou/archive/2005/12/05/22641.html#Feedback0http://www.aygfsteel.com/lmseeyou/comments/commentRss/22641.htmlhttp://www.aygfsteel.com/lmseeyou/services/trackbacks/22641.html/dev 讑֤Ҏ文g
/etc pȝ理和配|文?
/etc/rc.d 启动的配|文件和脚本
/home 用户ȝ录的基点Q比如用户user的主目录是/home/userQ可以用~user表示
/lib 标准E序设计库,又叫动态链接共享库Q作用类似windows里的.dll文g
/sbin pȝ理命oQ这里存攄是系l管理员使用的管理程?
/tmp 公用的时文件存储点
/root pȝ理员的ȝ录(呵呵Q特权阶U)
/mnt pȝ提供q个目录是让用户临时挂蝲其他的文件系l?
/lost+found q个目录qx是空的,pȝ非正常关留下“无家可归”的文gQwindows下叫什?chkQ就在这?
/proc 虚拟的目录,是系l内存的映射。可直接讉Kq个目录来获取系l信息?
/var 某些大文件的溢出区,比方说各U服务的日志文g
/usr 最庞大的目录,要用到的应用E序和文件几乎都在这个目录。其中包含:
/usr/X11R6 存放X window的目?
/usr/bin 众多的应用程?
/usr/sbin 用户的一些管理程?
/usr/doc linux文
/usr/include linux下开发和~译应用E序所需要的头文?
/usr/lib 常用的动态链接库和Y件包的配|文?
/usr/man 帮助文
/usr/src 源代码,linux内核的源代码放?usr/src/linux?
/usr/local/bin 本地增加的命?
/usr/local/lib 本地增加的库

Eddie Lee 2005-12-05 23:02 发表评论
]]>
jsf规范定义的两U标准用L面组Z?/title><link>http://www.aygfsteel.com/lmseeyou/archive/2005/12/04/22469.html</link><dc:creator>Eddie Lee</dc:creator><author>Eddie Lee</author><pubDate>Sun, 04 Dec 2005 13:23:00 GMT</pubDate><guid>http://www.aygfsteel.com/lmseeyou/archive/2005/12/04/22469.html</guid><wfw:comment>http://www.aygfsteel.com/lmseeyou/comments/22469.html</wfw:comment><comments>http://www.aygfsteel.com/lmseeyou/archive/2005/12/04/22469.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lmseeyou/comments/commentRss/22469.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lmseeyou/services/trackbacks/22469.html</trackback:ping><description><![CDATA[JSF规范定义了两个标准用L面组Z件?BR><EM><FONT color=#ff1493>javax.faces.event.ActionEvent</FONT></EM>是当标准UICommandlgQ通常昄为按钮、菜单项或超链接Q被用户触发Ӟ由其q播事g?BR>?EM><FONT color=#ff1493>javax.faces.event.ValueChangeEvent</FONT></EM>是当标准UIInputlg及其子类的值发生变化且通过验证Ӟ由其q播的事件?BR><BR>支持的所有事件必Mjavax.faces.event.FacesEvent基类zQJavaBean规范每个事gcȝ名字?Event"l尾<img src ="http://www.aygfsteel.com/lmseeyou/aggbug/22469.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lmseeyou/" target="_blank">Eddie Lee</a> 2005-12-04 21:23 <a href="http://www.aygfsteel.com/lmseeyou/archive/2005/12/04/22469.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank">ĺ</a>| <a href="http://" target="_blank">º</a>| <a href="http://" target="_blank">ʼ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ƽ</a>| <a href="http://" target="_blank">μԴ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">֣</a>| <a href="http://" target="_blank">ˮ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ʩ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ٳ</a>| <a href="http://" target="_blank">ӳ</a>| <a href="http://" target="_blank">ͬ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ʤ</a>| <a href="http://" target="_blank">ˮ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ƽ</a>| <a href="http://" target="_blank">Ԫ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">г</a>| <a href="http://" target="_blank">־</a>| <a href="http://" target="_blank">۲</a>| <a href="http://" target="_blank">۶</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">⳵</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">¡</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ϫ</a>| <a href="http://" target="_blank">ֶ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>