注    册
密 码 忘记密码
保存密码         取消

我的工具

我的广告

日历

统计

统计中,请等候...

统计中,请等候...

日志

jQuery中的lt和gt方法之不支持该属性的解决方法

分类:java web开发

 在《Learning jQuery》-Better Interaction Design and Web Development with Simple JavaScript Techniques一书中,作者Jonathan Chaffer 和Karl Swedberg 提供的代码中部分函数在新版的jQuery中已经去掉,所以书中提供的分页示例代码无法使用,在国外网站找到解决办法。

原书中分页示例代码,用来显示第一页的10行数据:
$(document).ready(function() {
$('table.paginated').each(function() {
var currentPage = 0;
var numPerPage = 10;
var $table = $(this);
$table.find('tbody tr').show()
.lt(currentPage * numPerPage)
.hide()
.end()
.gt((currentPage 1) * numPerPage - 1)
.hide()
.end();
});
});
 
需要将原代码做如下替换,就可以正常进行分页了:
移除.lt(currentPage * numPerPage) 用.slice(0, currentPage * numPerPage)替代...

移除.gt((currentPage 1) * numPerPage - 1)   用.slice((currentPage 1) * numPerPage - 1)代替

另外可以把分页处理修改为:
$table.find('tbody tr').hide()
        .slice(currentPage * numPerPage, (currentPage 1) * numPerPage - 1)
          .show();
也可达到同样之效果。

这个代码我在使用中,发现要将numPerPage-1改为numPerPage。各位在测试中看看是否正确。

关于slice函数在jQuery中的定义与JavaScript中的定义一致:.slice()用来从匹配的jQuery对象中分离出一部分jQuery对象。下面是调用slice()方法的一些正确方式:
JavaScript代码
$("div").slice(0,1); // 第一个 div   
$("div").slice(-1); // 最后一个 div   
$("div").slice(1,-1); // 除第一个最后一个的所有 div
$("div").slice(1,3); // 第二个和第三个 div
$("div").slice(7,8); // 第八个 div
slice中两个参数的定义slice(start,end):
start Integer
Where to start the subset. The first element is at zero. Can be negative to start from the end of the selection.
end (Optional) Integer
Where to end the subset (does not include the end element itself). If unspecified, ends at the end of the selection.
 
今天编写完数据分页,总结一下,完整的分页算法:
 //对建立完好的表格进行分页处理
                $("table.sortable").each(function(){ 
                var currentPage = 0;
                var numPerPage = 10;
                var $table = $(this);
                    $table.bind("repaginate", function() {
                        $table.find('tbody tr').hide()
                        .slice(currentPage * numPerPage, (currentPage 1) * numPerPage)
                          .show();
                    });//repaginate
                
                var numRows = $('table.sortable').find("tbody tr").length;  
                var numPages = Math.ceil(numRows/numPerPage);  
                var $pager = $('<div class="page"></div>');  
                for(var page=0;page<numPages;page ){
                    $('<a href=# ><span class="page-number">' (page 1) '</span></a>').bind("click", { "newPage": page }, function(event) {  
                        currentPage = event.data["newPage"];                  
                        $table.trigger("repaginate");             
                        $(this).addClass("active").siblings().removeClass("active");  
                    }).appendTo($pager);  
                    $pager.append("&nbsp;");
                }  //for
                $pager.find("span.page-number:first").addClass("active");  
                $pager.insertAfter($table);  
                $table.trigger("repaginate"); 

JunitPerf使用简明手册

分类:软件测试

     代码效率对于许多开发者而言是一个非常关心的指标。而且,代码执行效率的高低对于应用的使用者来说是感觉最直观的。它往往也是一个应用是否成功的关键性指标之一。那么是否存在那些能够反映代码执行效率的工具呢?我们可以试一试junitperf

 

使用目的
       测试单元测试的执行效率。由于在单元测试中实际调用的就是单元代码,因此,测试单元测试的执行效率可以直接的反应实际代码的效率。Junitperf实际是junit的一个decorator,通过编写用于junitperf的单元测试,我们也可使这一测试过程自动化。当前的版本是1.9,可以从:http://www.clarkware.com/software/JUnitPerf.htm下载。
用法
1.       基本使用步骤:
-          书写junit单元测试;
-          如果单元测试执行效率比预想的低,那么书写另一个单元测试,隔离可能的代码;
-          为这个单元测试书写对应的junitperf,如果测试通过,表示隔离代码不是引起问题的原因。此时,需要重复上面的步骤。如果测试不通过,就需要重新考虑被隔离代码的实现以提高效率,重复整个过程。
2.       使用TimedTest,测试代码是否能在指定时间内完成工作。TimedTest的构造函数有三个参数:要测试的Test,期望测试完成的时间,当时间到时是否等待测试完成。
-          测试单个测试方法
//创建一个suite方法,方便使用junit执行
public static Test suite( ) {
        //创建一个TestCase,其中构造函数中传的是要测试的测试方法名
Test testCase = new StringUtilTest("testMethod");
//创建一个TestSuite
TestSuite suite = new TestSuite();
//增加一个TimedTest
    suite.addTest( new TimedTest( testCase, 2000, false));
    return suite;
}
-          测试TestSuite
//创建一个suite方法,方便使用junit执行
public static Test suite( ) {
//创建一个TestSuite
TestSuite suite = new TestSuite();
//增加一个TimedTest
    suite.addTest( new TimedTest(new TestSuite(StringUtilTest.class), 2000, false));
    return suite;
}
3.       使用LoadTest,进行压力测试。LoadTest用来模拟一定数量的用户并行使用单元测试。它有4种构造函数,从构造函数,可以看出LoadTest的主要的功能:
-          LoadTest(Test test, int users),创建users数目的用户,每个请求执行一次。
-          LoadTest(Test test, int users, int iterations),创建users数目的用户,每个用户请求iterations次。
-          LoadTest(Test test, int users, int iterations, Timer timer),创建users数目的用户,使用timer规定请求时延,每个用户请求iterations次。
-          LoadTest(Test test, int users, Timer timer),创建users数目的用户,使用timer规定请求时延,每个用户请求1次。
Timer接口请参见对应的javaDoc。它的使用与TimedTest类似。例子如下:
……
Test test= new StringUtilTest(“testMethod”);
TestSuite suite= new TestSuite();
Suite.addTest( new LoadTest( test, 100, 10));
……
4.       组合测试,发挥decorator的潜力。junitperf实际上是junit的decorator,从decorator的定义可以得出,应该可以使用这两个相互的decorate。
-          使用TimedTest decorate LoadTest,测试满足并行指标的请求是否满足效率指标。
……
Test test= new StringUtil( “testMethod”);
TestSuite suite= new TestSuite();
Suite.addTest( new TimedTest( new LoadTest( test, 100, 10), 3000, false));
……
-          使用LoadTest decorate TimeTest,测试满足效率指标的单个请求是否满足并行指标。
……
Test test= new StringUtil( “testMethod”);
TestSuite suite= new TestSuite();
Suite.addTest( new LoadTest( new TimedTest( test, 300, false), 1000, 10));
……
5.       使用ant,使测试自动化。Junit提供了自动执行unit test的ant task,我们可以使用这个task来使junitperf的测试自动化(因为是decorator)。但是,它又是与unit test的侧重点不相同,因此有必要区分他们。从junit的介绍中可以看出ant task主要是通过命名规则来查找要执行的测试类,因为为了区分他们,我们只需要把这两种测试的命名规则不一样即可。至于其他方面,则与junit完全相同。
检查表
       以下是使用在junitperf时需要注意的问题:
-          TimedTest的测试时间并非非常准确,而是一个大致的值。对于一个测试方法而言,它所测量的时间是指:setUp、测试方法和tearDown的总时间。
-          在进行LoadTest时,请谨慎的选择请求的时延,如果时延过大会造成达不真正并行的效果。
-          使用不同的命名规则区分普通的junit。
       -      junitperf并不是用来替换那些执行效率测试工具的,它仅仅应该只用来进行本地化的效率测试和更好的重构。(摘自junitperf文档)。

org.apache.commons.dbcp.BasicDataSource异常

分类:java web开发

error:java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource

java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
很显然这是一个没有找到对应的类文件的异常。
还有两个包commons-pool.jar和commons-dbcp-1.2.1.jar需要引入到工程中。

利用lucene2.3.2对文本文件进行索引

分类:Lucene搜索引擎

/**
 *
 */
package com.lucene.indexs;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

/**
 *
 */
public class Indexer {

 /**
  * @param args
  * @throws Exception
  */
 public static void main(String[] args) throws Exception {
  if(args.length!=2){
   throw new Exception("使用: java " Indexer.class.getName() " <索引存放目录> <被索引文件所在目录>");
  }
  File indexDir= new File(args[0]);
  File dataDir= new File(args[1]);
  
  long start= new Date().getTime();
  int numIndexed= index(indexDir,dataDir);
  long end= new Date().getTime();
  System.out.println("索引了 " numIndexed "个文件,用了" (end-start) " 毫秒");
 }
 /**
  *
  * @param indexDir
  * @param dataDir
  * @return
  * @throws IOException
  */
 private static int index(File indexDir, File dataDir) throws IOException {
  if(!dataDir.exists() || !dataDir.isDirectory()){
   throw new IOException(dataDir " 不存在或不是目录");
  }
  
  IndexWriter writer= new IndexWriter(indexDir,new StandardAnalyzer(),true);
  writer.setUseCompoundFile(false);
  
  indexDirectory(writer,dataDir);
  
  int numIndexed= writer.docCount();
  writer.optimize();
  writer.close();
  return numIndexed;
 }
 /**
  *
  * @param writer
  * @param dataDir
  * @throws IOException
  */
 private static void indexDirectory(IndexWriter writer, File dataDir) throws IOException {
  File[] files= dataDir.listFiles();
  int fileSize= files.length;
  for(int i=0;i<fileSize;i ){
   File file= files[i];
   if(file.isDirectory()){
    indexDirectory(writer,file);
   }else if(file.getName().endsWith(".txt")){
    indexFile(writer,file);
   }
  }
 }
 /**
  *
  * @param writer
  * @param file
  * @throws IOException
  */
 private static void indexFile(IndexWriter writer, File file) throws IOException {
  if(file.isHidden() || !file.exists() || !file.canRead()){
   return;
  }
  System.out.println("Indexing " file.getCanonicalPath());
  Document doc= new Document();
  doc.add(new Field("contents",new FileReader(file)));
  doc.add(new Field("filename",file.getCanonicalPath(),Field.Store.YES,Field.Index.UN_TOKENIZED));
  doc.add(new Field("modified",DateTools.timeToString(file.lastModified(), DateTools.Resolution.MINUTE),Field.Store.YES,Field.Index.UN_TOKENIZED));
  writer.addDocument(doc);
 }

}

命令行javac和java的使用

分类:java web开发

1、javac

D:devstudioeclipse3.2_newworkspaceLuceneStudysrc>javac -classpath ../lib/lucene-core-2.3.2.jar com/lucene/indexs/Indexer.java

 

2、java

D:devstudioeclipse3.2_newworkspaceLuceneStudysrc>java -classpath ../lib/lucene-core-2.3.2.jar; com.lucene.indexs.Indexer d:lucene_index d:lucene_data

为奥运健儿加油 感受奥林匹克精神

分类:默认栏目

 

 

2008年8月8号,北京举行了29届奥运会开幕式,北京奥运会正式开始啦。

作为在家门口举行的奥运会,我感到很自豪。

能够在北京感受到奥运的气氛,我感到自豪。

看着首都人们对奥运的热情,我很激动,同时很自豪。

我感到今日中国变得越来越强大,越来越自信和成熟了。

少年强,则中国强,作为一名当代的青年,我们要更加努力、自强,把国家建设得更加强大。

让我们为北京奥运加油!

让我们为全世界的奥运健儿加油!

最后,让我们记住顾拜旦在1936年奥运会演讲时说过的一句话:"奥运会重要的不是胜利,而是参与;生活的本质不是索取,而是奋斗。"。

quartz_jobs.xml 中时间格式的问题

分类:Quartz任务调度

 

quartz_jobs.xml 中时间格式的问题,写成 2008-06-20 7:23:00 PM 的话 JobSchedulingDataProcessor.parseDate(value) 没办法解析

在 quartz_jobs.xml 中 <start-time> 的格式是:

<start-time>2008-06-23T21:23:00</start-time>

T隔开日期和时间,默认时区

或者:
<start-time>2008-06-23T21:23:00 08:00</start-time>
08:00 表示东八区

我觉得这是 Quartz 的一个 Bug,其实 Quartz 在解析时间时准备了两个 Pattern 的,分别是:
yyyy-MM-dd'T'hh:mm:ss
yyyy-MM-dd hh:mm:ss a

但是在 JobSchedulingDataProcessor.parseDate(value) 方法中只会以第一个 Pattern 解析时间,并不会尝试使用第二个 Pattern 去解析时间,第二个 Pattern 是可以认识 2008-06-20 7:23:00 PM 的。

所以为了规避这个问题,还是应该写成 yyyy-MM-dd'T'hh:mm:ss 格式。

用Spring集成Struts时老出Servlet action is not available

分类:java web开发

本人用Spring集成Struts时老出Servlet action is not available错
找配置文件找了N久,没有找出错来。后面把
 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
   <set-property value="contextConfigLocation" property="/WEB-INF/applicationContext.xml"/>
  </plug-in>

这段代码给剪掉。在web.xml文件中加上
<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <servlet>
 <servlet-name>context</servlet-name>
 <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
  </servlet>
就OK了!!

ClassCastException:struts.ActionMessage异常解决

分类:java web开发

在tomcat5.5.17上运行struts项目时报如下错误:
java.lang.ClassCastException: org.apache.struts.action.ActionMessage
org.apache.struts.taglib.html.ErrorsTag.doStartTag(ErrorsTag.java:215)
org.apache.jsp.login_jsp._jspx_meth_html_005ferrors_005f0(login_jsp.java:173)
org.apache.jsp.login_jsp._jspx_meth_html_005fform_005f0(login_jsp.java:115)
org.apache.jsp.login_jsp._jspService(login_jsp.java:77)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:1012)
org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:980)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
解决办法:
   ActionErrors error=new ActionErrors();
   if ("login".equals(status)) {
   if (this.id == null || "".equals(this.id)) {
    error.add("id", new ActionMessage("id.null"));
   }
  }
该为
   ActionErrors error=new ActionErrors();
   if ("login".equals(status)) {
   if (this.id == null || "".equals(this.id)) {
    error.add("id", new ActionError("id.null"));
   }
  }
就可以了 ActionError比ActionMessage好用

感觉好累

分类:默认栏目

  感觉好累,可能我是闷得有些慌了。

  想呼吸一下新鲜的空气,想亲近下大自然,想放松下身心。

  也许,我应该去爬爬北京郊区的山了,比如香山。

  我想那种爬完山后,出一身汗,全身完全放松的感觉,会让疲倦、萎靡远离自己。

更多日志..