2009年8月10日星期一

1248375768_HvlgQr


1248375768_HvlgQr
Originally uploaded by tony_wangx
It's the first time to post weblog with flickr.

Flickr

This is a test post from flickr, a fancy photo sharing thing.

2009年5月13日星期三

Java 数字浮点型与chat之间的转化(int or float convert into char)

见这行代码:
char c=(char)(0.7+'a');
System.out.println(c);
打印的结果将是a,所以如果数字类型强行转化为char类型时,会砍掉小数点后面的小数。再转化,而不是四舍五入。

2009年5月4日星期一

用7种语言实现的数据结构与算法

有很多人在找数据结构方面的资料吧。这个可以看看,很不错。
用7种语言实现的数据结构与算法

在Ext GridPanel中设置某一列不参与分组的办法

假设你已经有一个包含分组的Grid,在其设置column的地方设置属性groupable:false。
见代码:
           {
                id: 'cost',
                header: "Cost",
                width: 20,
                sortable: false,
                groupable: false,
                dataIndex: 'cost',
                summaryType:'totalCost',
                summaryRenderer: Ext.util.Format.usMoney
            } 

    2009年5月2日星期六

    mvn无法安装jpa(jta)的解决办法 Jta1.0.1B no maven

    先去http://java.sun.com/javaee/technologies/jta/index.jsp手动下载classes文件。
    之后放到本地的一个本件家中。
    运行mvn install:install-file -Dfile=./jta-1_0_1B-classes.zip -DgroupId=javax.transaction -DartifactId=jta -Dversion=1.0.1B -Dpackaging=jar
    之后再在项目中运行mvn install
    ok、

    Spring Annotation详解(AOP篇2)

    接上一篇Spring Annotation详解(AOP篇1)
    本文的相关文章有:
    Spring Annotation详解(IOC篇1)
    Spring Annotation详解(IOC篇2)
    Spring Annotation详解(IOC篇3)
    Spring Annotation详解(AOP篇1)

    上一篇介绍了基本的pointcut与advice的使用。这篇着重介绍一下除了execution之外的其他类型的pointcut。

    within:within指的是在某个范围内所有的方法执行。
    例如:
    @pointcut("within(com.xyz.project.web..*)")就是指在com.xyz.project.web包及其子包中所有的方法被执行的时候,都会触发这个pointcut。within(com.xyz.project.web.*)这样写就只是针对这个包,而不包括这个包的子包。

    this:this指的是实现了某个确定的接口的代理对象中的方法被执行。
    target:target指的是实现了某个确定的接口的代理对象中的方法被执行。
    这两种,以后再议。

    当然还有其他的类型,但是目前用到得不多。不太常见。

    2009年4月30日星期四

    Spring Annotation详解(AOP篇1)

    相关文章:Spring Annotation详解(AOP篇2)
    Spring Annotation详解(IOC篇)
    本文主要简单了解一下使用Java annotation的情况下,先介绍一下比较主流和简单的用法,Spring对AspectJ的支持。
    在ApplicationContext中加入关于AOP的命名空间,具体内容见Spring 使用AspectJ来配置AOP
    首先明确一点,用作通知(Advice)的类也应该受到ApplicationContext的管理,之后再使用AspectJ的标签来管理通知动作,见代码:
    //import ...
    @Component
    @Aspect
    public class Alert {
        @Before("execution(* get*(..))")//方法运行前通知
        public void show() {
            System.out.println("before");
        }
        @After("execution(* get*(..))")//最终通知
        public void show2() {
            System.out.println("after");    
        }
        @Pointcut("execution(* get*(..))")
        public void afterReturn(){}
        //方法返回值通知,注意这个通知需要与@Pointcut配合使用
        @AfterReturning(pointcut="afterReturn()",returning="value")
        public void returning(Object value){
            System.out.println("returning");
        }
        @AfterThrowing(pointcut="afterReturn()",throwing="ex")
        //异常通知,这个通知复用了afterReturn的pointcut。当然你也可以使用新的pointcut
        public void doRecoveryActions(Exception ex){
            //...
        }
        @Around("afterReturn()")
        //环绕通知。真正的方法实际上是joinPoint.proceed();
        public Object doSomeForAround(ProceedingJoinPoint joinPoint) throws Throwable
        {
           System.out.println("before around");
           Object retVal=joinPoint.proceed();
           System.out.println("after around");
          return retVal;
        }
    }

    这段代码的意思就是在所有以get开头的方法被调用之前和之后都会执行show和returning,show2方法,其运行顺序是show(),returning(),show2()。注意*与get*之间有一个空格。对一个方法,你可以同时使用Before Advice,Around Advice,AfterReturning Advice,After Advice,Throwing Advice。那么这些通知的顺序是什么呢?一个方法运行时,最先触发的是Around Advice,之后是Before Advice 是 AfterReturning(如果有的话),之后是After Advice,最后还要再通知Around。
    就拿上面代码这个例子说,打印的顺序是:
    before around
    before
    returning
    after
    after around

    Ruby:Timezone synchronization function(跨时区同步时间方法)

    方法是:首先取得客户端的时区,之后得到服务器的本地时区,之后取得两个时区的差值,计算出相应的时间。
    输入:服务器时间
    输出:相应的客户端对应的时间
    上代码:
      #时间同步方法
      #clint_timezone:client timezone
      def formate_date_to_client_time_zone(clint_timezone,time)
        diff=(clint_timezone-get_server_time_zone)
        unless(clint_timezone.nil?)
          time=time+diff*3600
        end
        return time
      end
      #获得本地时区
      #return the server timezone
      def get_server_time_zone
        return Time.now.gmtoff/3600
      end

    2009年4月29日星期三

    Ruby:string to date Or date to string

    Ruby:string to date Or date to string
    #string to date:
    str="2009-02-02"
    date=Date.strptime(str,"%Y-%m-%d")
    #date to string:
    date=Date.new(2009,2,2)
    str=date.strftime("%Y-%m-%d")

    2009年4月28日星期二

    超炫的flex特效,UI做到这份上,应该说无敌了。

    Efflex 是一组由 Stephen Downs (aka “Tink”)开发的 Flex特效。
    留个纪念,慢慢看
    这里是demo

    正则表达式验证邮箱地址

    定义以下模式为有效的:
    1. john@hotmail.com
    2. john.doe@somewhere.com
    3. John Doe<john.doe@somewhere.com>
    javascript的正则表达式为
    var reEmail=/^(?:\w+\.?)*\w+@(?:\w+\.?)*\w+$/;

    2009年4月27日星期一

    Flex:浅谈Flex事件机制

          一直对RIA技术非常感兴趣,研究了一下Flex的事件机制,发现Flex的事件机制和Javascript很像,只不过更加严格。在Flex的事件机制中有一个比较重要的概念dispatchEvent,基本原理就是事件所在的容器类初始化的时候注册其事件监听函数。如果需要自己定义Event类型,需要将事件分发到事件队列中,也就是dispatchEvent。也就是你要手动找到自定义的事件的前一个事件,在这个结束的时候调用dispatchEvent(event),这个event就是你自定一个事件,这个事件(一个类)一定要继承flash.events.Event
    给个例子:
    自定义的事件类:
    public class AddressFormEvent extends Event

    {
    public static const SUBMIT:String = "submit";
    private var _data:AddressVO;
    public function AddressFormEvent (eventName:String)
    {
    super (eventName);
    }
    public function set data (value:AddressVO):void
    {
    _data = value;
    }
    public function get data ():AddressVO
    {
    return _data;
    }
    }
    在上一个事件结束的时候发布这个事件,下面这段代码就是找到上一个事件:
    public class AddressFormClass extends Form

    {
    public var submitButton:Button;
    public var nameInput:TextInput;
    public var street:TextInput;
    public var city:TextInput;
    public var state:TextInput;
    public var country:CountryComboBox;
    public var otherInput:TextInput;
    public var otherText:Text;
    public function AddressFormClass():void
    {
    addEventListener(FlexEvent.CREATION_COMPLETE,creationCompleteHandler);
    }
    private function creationCompleteHandler(event:FlexEvent):void
    {
    submitButton.addEventListener(MouseEvent.CLICK,submitHandler);
    }
    private function submitHandler(event:MouseEvent):void //这个就是上一个事件的监听函数
    {
    // Gather the data for this form
    var addressVO:AddressVO = new AddressVO();
    addressVO.name = nameInput.text;
    addressVO.street = street.text;
    addressVO.city = city.text;
    addressVO.state = state.text;
    addressVO.country = country.selectedItem as String;
    // 下面就是创建这个事件自定义事件,注意类型就是我们刚刚建的那个事件类。
    var submitEvent:AddressFormEvent = new AddressFormEvent(AddressFormEvent.SUBMIT);
    submitEvent.data = addressVO;
    // Dispatch an event to signal that the form has been submitted
    dispatchEvent(submitEvent); //这就是发布这个时间
    }
    }

    2009年4月26日星期日

    Spring 使用AspectJ来配置AOP

    在Application_context中引入名称空间,例子:<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:lang="http://www.springframework.org/schema/lang"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
         http://www.springframework.org/schema/lang
         http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-2.5.xsd">
      <context:component-scan base-package="bean"/>
      <aop:aspectj-autoproxy/>
    </beans>

    2009年4月24日星期五

    在ExtJs应用中快速为页面元素添加tooltip

    ExtJs是通过Ext.ToolTip和Ext.QuickTips两个组件来实现浮动提示功能的。

    QuickTips代码示例:只需要加入Ext.QuickTips.init(); 就可以在html页面中使用。html页面 可以通过:
    <input type="button" value="OK" ext:qtitle="Test" ext:qtip="Test Content!">

    2009年4月23日星期四

    Javascript方式打开一个浏览器

    var newWindow=window.open(url,'txtPopup','height=768,location=0,menubar=0,personalbar=0,scrollbars=1,status=0,toolbar=0,width=1024,resizable=0');
    newWindow.focus();

    javascript 刷新页面的方式:
    1. history.go(0)
    2. location.reload()
    3. location=location
    4. location.assign(location)
    5. document.execCommand('Refresh')
    6. window.navigate(location)
    7. location.replace(location)
    8. document.URL=location.href

    JRuby On Spring在spring框架中使用Jruby

    昨天尝试在Spring中使用脚本语言,比如jruby,发现非常不方便,最主要的一点就是ruby class必须继承某一个java的接口,而其他bean调用时也是调用这个接口类型的对象。jruby的动态性基本丧失了。唉!

    JRuby的性能优化(update)

    JRuby wiki上列出了性能优化的四条建议:
    1、调优编译器,JRuby早就弃暗投明跟随XRuby走上了编译这条牛B的道路,将Ruby Script编译成字节码,因此这个环节是断断不能忽略的。
    两种编译方式:
    AOT模式:直接生成class文件,脱了Ruby这层皮,咱就是人见人“爱”的java了。
    JIT模式:充分利用成熟的jit技术,咱不全脱,朦胧美才是真的美。默认从0.9.9版本开始就是开启的,关闭的话(要我说还不如全脱)
    jruby -J-Djruby.jit.enabled=false
    2、关闭ObjectSpace
    ObjectSpace是Ruby用来操作所有运行时对象的模块,这个功能相当牛x。这个的实现在c ruby里是比较容易的,但是对于JRuby代价就比较昂贵了,其实就大部分情况下你基本用不到这个东东,那么最好就是关闭它,JRuby提供了
    jruby -J-Djruby.objectspace.enabled=false选项来关闭它。
    3、开启线程池
    我们知道,在c ruby中的线程是绿色的轻量级线程,因此运行时就动不动开个百来十个“线程”跑一跑充下款爷;然而在JRuby中,线程的实现那可是实打实的本地线程(也就是Ruby线程与java线程一比一),你这么动不动上百个线程那不慢才怪了。因此JRuby提供了线程池选项,运行时尽可能地满足你的要求开线程,但是当短命的Ruby线程重复创建的时候,这些线程将被复用,这在大多数情况下能提高性能表现,特别是在每次调用都启动一个线程的情况下。不过具体效果还是要测试的实际数据说话。
    jruby -J-Djruby.thread.pooling=true
    4、使用Java "server"模式虚拟机,地球淫都知道
    jruby -J-server myscript.rb
    5、尽量使用最新的jdk,在我的测试中,jdk6跑jruby是效率最高的。

    2009年4月22日星期三

    用ruby写的一个网络爬虫程序

    前几天写的一个ruby爬虫,专抓指定网站的图片
    require 'net/http'
    require "monitor"
    def query_url(url)
      return Net::HTTP.get(URI.parse(url));
    end
    def save_url(url,dir,filename)
      filename = url[url.rindex('/')+1, url.length-1] if filename == nil || filename.empty?
      require 'open-uri'
      Dir.mkdir("#{dir}") if dir != nil && !dir.empty? && !FileTest.exist?(dir)
      open(url) do |fin|
          File.new("#{dir}#{filename}","wb").close
          open("#{dir}#{filename}","wb") do |fout|
            while buf = fin.read(1024) do
              fout.write buf
              STDOUT.flush
            end
          end
      end
    end
    def download_page(content)
      content.scan(/<[Ii][Mm][Gg].* src="\S+[^ni]."/) {|match|
        match.scan(/http:\/\/\S+"/){|img|
          img=img.gsub(/"/,'')
          puts "img:"+img
          begin
            save_url(img,"E:\\TET\\",nil)
          rescue =>e
            puts e
          ensure
            next
          end
        }
      }
    end
    begin
      start_url = 'http://se.1ssdd.com/'
      print "开始搜索#{start_url}\n"
      content = query_url(start_url)
      next_host = "http://se.1ssdd.com/"
      threads=[]
      i=1
      content.scan(/<a href="\/(.*?\.html)"/) {|match|
        if  !match.nil? && match.size>0
          threads<<Thread.new(match) do |urls|
            urls.each{|url|
              next_url =next_host+url
              puts next_url+"::"+i.to_s
              page=query_url(next_url)
              download_page(page)
            }
          end
        end
      }
      threads.each{|thr| thr.join}
      download_page(content)
      p "over"
    end