牛仔裤的夏天

JAVA是蓝色的- online

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  30 随笔 :: 5 文章 :: 15 评论 :: 0 Trackbacks

#

在Jdon.com里面看到的,觉得很值得借鉴
原文:http://www.jdon.com/jive/thread.jsp?forum=16&thread=302

看下面比较:

public List getUsers() 
  ResultSet rs 
= userDbQuery();
  List retval 
= new ArrayList();
  
while (rs.next()) {
    retval.add(rs.getString(
1));
  }

  
return retval;
}
上面是个我们采取返回Collection后最常用的方法,将ResultSet中的用户名加入List再返回,显然这很耗费内存。

使用Iterator返回:
public Iterator getUsers() {
  
final ResultSet rs = userDbQuery();
    
return new Iterator() {
      
private Object next;
      
public void hasNext() {
        
if (next == null{
          
if (! rs.next()) {
            
return false
          }
 
          next 
= rs.getString(1);
        }

        
return true;
      }


      
public Object next() {
        
if (! hasNext()) {
          
throw new NoSuchElementException();
        }

        String retval 
= next;
        next 
= null;
        
return retval;
      }


      
public void remove() {
        
throw new UnsupportedOperationException ("no remove allowed");
      }

   }

}

这个Javabean只是做了一个指针传递作用,将调用本Javabean的指针传递到ResultSet,这样既提高了效率,节约了内存,又降低了偶合性,这是堪称中间件典型的示范。


posted @ 2006-03-15 16:56 luckyrobbie 阅读(5447) | 评论 (6)编辑 收藏

今天贝贝在家休息一天,没有去幼儿园
下午我打电话到父母家,我爸爸说了几句后把电话给了贝贝
和贝贝聊了一会后
我:让奶奶接电话
贝贝:妈妈刚才也让奶奶接电话的
我:快点
贝贝:不行
我:那让爷爷接电话
贝贝:也不可以
我:为什么
贝贝:只允许贝贝接电话
...
...
我:快点呀,让爷爷奶奶接电话
贝贝:byebye~~~
最后竟然把电话挂了。

posted @ 2006-03-13 22:51 luckyrobbie 阅读(246) | 评论 (0)编辑 收藏

     摘要: 单位里的查询系统是基于struts的,在结果集显示的处理上都是用 <logic:iterate id=".." indexId="id" name=".." scope="request"> 然后在<bean:write name=".." property=".."> 这样子对付的,后来觉得比较麻烦,就写了一个显示结果集的标签 行记录用HashMap保存 ...  阅读全文
posted @ 2006-03-10 13:12 luckyrobbie 阅读(1153) | 评论 (1)编辑 收藏

http://www.w3schools.com/xpath/default.asp

http://www.zvon.org/xxl/XPathTutorial/General/examples.html
posted @ 2006-03-09 13:20 luckyrobbie 阅读(361) | 评论 (3)编辑 收藏

When to use DOM

If your XML documents contain document data (e.g., Framemaker documents stored in XML format), then DOM is a completely natural fit for your solution. If you are creating some sort of document information management system, then you will probably have to deal with a lot of document data. An example of this is the Datachannel RIO product, which can index and organize information that comes from all kinds of document sources (like Word and Excel files). In this case, DOM is well suited to allow programs access to information stored in these documents.

However, if you are dealing mostly with structured data (the equivalent of serialized Java objects in XML) DOM is not the best choice. That is when SAX might be a better fit.

When to use SAX

If the information stored in your XML documents is machine readable (and generated) data then SAX is the right API for giving your programs access to this information. Machine readable and generated data include things like:

  • Java object properties stored in XML format
  • queries that are formulated using some kind of text based query language (SQL, XQL, OQL)
  • result sets that are generated based on queries (this might include data in relational database tables encoded into XML).

So machine generated data is information that you normally have to create data structures and classes for in Java. A simple example is the address book which contains information about persons, as shown in Figure 1. This address book XML file is not like a word processor document, rather it is a document that contains pure data, which has been encoded into text using XML.

When your data is of this kind, you have to create your own data structures and classes (object models) anyway in order to manage, manipulate and persist this data. SAX allows you to quickly create a handler class which can create instances of your object models based on the data stored in your XML documents. An example is a SAX document handler that reads an XML document that contains my address book and creates an AddressBook class that can be used to access this information. The first SAX tutorial shows you how to do this. The address book XML document contains person elements, which contain name and email elements. My AddressBook object model contains the following classes:

  • AddressBook class, which is a container for Person objects
  • Person class, which is a container for name and email String objects.

So my "SAX address book document handler" is responsible for turning person elements into Person objects, and then storing them all in an AddressBook object. This document handler turns the name and email elements into String objects.

Conclusion

The SAX document handler you write does element to object mapping. If your information is structured in a way that makes it easy to create this mapping you should use the SAX API. On the other hand, if your data is much better represented as a tree then you should use DOM.

posted @ 2006-02-22 21:52 luckyrobbie 阅读(194) | 评论 (0)编辑 收藏

参考原文地址 http://homepage.mac.com/edahand/projects/java/example1.html

1  在E盘建立一个目录sandh, 然后把那个空的struts-blank.war解压到这下面作为struts模板,把hibernate3.1里面的jar文件拷贝到/sandh/web-inf/lib下面,ehcache.xml拷贝到/sand/web-inf/src/java下面。

2  在tomcat下配置datasource命名为jdbc/aix,然后建立一个新的context名字为sand,在sand下建一个新的资源连接jdbc/aix,作为hibernate.cfg.xml中hibernate.connection.datasouce引用值。

3  在sybase中新建一个表item

create table dbo.item (
id  numeric(
180)  identity,
name    varchar(
32) not null,
description text    
null,
constraint PK_ITEM PRIMARY KEY  NONCLUSTERED ( id )
)


4  在/sandh/web-inf/src/java下面建立以下文件
log4j.properties 
hibernate.cfg.xml
Item.hbm.xml
HibernateUtil.java
Item.java
AddItemAction.java
ItemService.java

在/sandh/web-inf/src下面新建build.xml文件,然后ant compile。
在/sandh/pages下新建AddItem.jsp文件

5   修改/sandh/web-inf/struts-config.xml文件,内容如下:

<struts-config>
    
<form-beans>
        
<form-bean name="addItemForm" 
                   type
="org.apache.struts.validator.DynaValidatorForm">
                   
<form-property name="name" type="java.lang.String"/>
                   
<form-property name="description" type="java.lang.String"/>
        
</form-bean>
    
</form-beans>

    
<global-forwards>
        
<forward name="welcome" path="/items.do"/>
    
</global-forwards>
    
    
<action-mappings>
        
<action
        path
="/items"
        type
="org.apache.struts.actions.ForwardAction"
        parameter
="/pages/AddItem.jsp"/>
     
        
<action
           path
="/addItem"
           type
="com.fan.hibernate.AddItemAction"
           name
="addItemForm"
           scope
="request"
           validate
="true"
           input
="/pages/AddItem.jsp">
           
<forward name="success" path="/pages/AddItem.jsp" />
         
</action>
    
</action-mappings>
    
    
<message-resources parameter="MessageResources" null="false"/>
    
    
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
       
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
    
</plug-in>    
</struts-config>



修改/sandh/web-inf/validation.xml文件,为addItemForm增加动态验证,内容如下:

    <formset>
        
<form name="addItemForm">
            
<field property="name" depends="required">
                
<arg key="name" resource="false"/>
            
</field>
            
<field property="description" depends="required">
                    
<arg key="description" resource="false"/>
            
</field>
        
</form>
    
</formset>


6   在tomcat下面测试一下吧。http://127.0.0.1:8080/sand

7   测试的时候发现2个问题:
首先,如果在/sandh/web-inf/classes下面没有ehcache.xml这个文件,会报这个错误,
No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath:.....这个文件是hibernate缓存配置文件,以后在学习。
另外在log文件中一直发现这个警告:Could not bind factory to JNDI javax.naming.NamingException: Context is read only..... 网上查找后发现只要在hibernate.cfg.xml文件的<session-factory name="java:/hibernate/HibernateFactory"> 中把name=....去掉就好了,否则会自动把name注册进jndi中失败而发出警告。

8   所有文件打包在这里
http://www.blogjava.net/Files/luckyrobbie/sandh.rar 里面lib下面的文件自己添进去吧。



posted @ 2006-01-26 10:49 luckyrobbie 阅读(803) | 评论 (0)编辑 收藏

昨晚带贝贝去大统华玩,买了几本书和吃的东西,比较开心的样子。
“贝贝,晚上和谁睡觉?”
“爸爸”
“一定吗”
“一定”
“如果骗人的话你是什么”
“小猪”
“那我们拉勾”
“不”
怎么也不肯和我拉勾,看来还是骗人的
posted @ 2006-01-24 08:44 luckyrobbie 阅读(629) | 评论 (0)编辑 收藏

昨天是星期天,早上我睡懒觉起床后贝贝和妈妈已经出去玩了,发现电脑桌上有几颗鱼皮花生米,那是我买给贝贝吃的椰子味的花生米。心想:是不是留给我吃的啊^^

中午在父母家吃饭,我问老婆,是不是贝贝特意留了给我的?老婆说,别美了,肯定是他放在桌子上忘记了。于是我问贝贝:是否留给爸爸吃的呀?贝贝答:是的。我继续问:你当时是怎么想的啊?贝贝答:我当时想爸爸也喜欢吃的,于是留给你的。

呵呵,不管是真是假,心里美滋滋的。

晚上,贝贝拿着玩具手机打电话给老师,编了几句后,我问他老师说什么了。他说:陆老师明天带我到##饭店去吃饭的。
“带爸爸去吗?”
“不带”
“带妈妈去吗?”
“不带”
“那你一个人怎么过去呢?” 我问。
于是贝贝又打了一个电话,说“陆老师明天来接我去吃饭的”

posted @ 2006-01-16 14:49 luckyrobbie 阅读(336) | 评论 (0)编辑 收藏

今天看了一个别人的帖子才发现刚刚做的项目里面存在一些画蛇添足的实现方法。

在action处理完控制逻辑后,把dto处理结果扔给显示层表示后,在显示层里面如果要显示一些在ActionForm中的property,本来是通过在action中用request.setAttribute(..)传递的,其实没这个必要。

Action处理完毕后,本来传递给action的form在request域里面是依旧存在的,所以在输出的显示页面上只要用<bean:write name="actionformname" property="field" scope="request"/> 就ok了。

打个哈欠,以前写的代码也懒得去改了,反正也没多大的不适,嘿嘿

468c05860200005t.jpg
posted @ 2005-12-15 16:16 luckyrobbie 阅读(360) | 评论 (0)编辑 收藏

public  ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
{

// ***********

  DynaValidatorForm f
= (DynaValidatorForm) form;

  String a
= (String) f.get( " A " );   

  String b
= (String) f.get( " B " );   

// ***********

}

对于上面的actionform的传递,本来以为一定要通过提交<html:form.../>才能接收的,后来发现其实通过参数的传递也可以实现。

例如上面的actionform, 如果有2个field, 分别是A和B, 如果不通过提交form的方法,直接用带参数的连接表示, 那么通过这个做法也可以正确的用f.get(..)方法得到数值. 

http://localhost:8080/app/myaction.do?A=abc&B=def

由此可见,如果分别3个页面page a, page b and page c. page a中有一个form, 提交后产生page b, 对page b中的链接提交后产生page c. 那么page b 和 page c 可以通过一个action来实现, 只要保证page b中关于action的连接后面加的参数名字和actionform的field名字一致就可以了。

468c05860200007v.jpg

posted @ 2005-12-13 21:52 luckyrobbie 阅读(611) | 评论 (0)编辑 收藏

仅列出标题
共3页: 上一页 1 2 3