论坛首页 Java企业应用论坛

Tapestry 5.0.5 bug, TAPESTRY-1648

浏览 3055 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-08-26  
有一个domian object, 其中有个property的type是BigDecimal。在增加bigdecimal的translator后。

package com.javaeye.dengyin2000.gtts.tapestry;

import java.math.BigDecimal;

import org.apache.tapestry.Translator;
import org.apache.tapestry.ValidationException;
import org.apache.tapestry.ioc.Messages;
import org.apache.tapestry.ioc.internal.util.InternalUtils;

public class BigDecimalTranslator implements Translator<BigDecimal> {

	public BigDecimal parseClient(String clientValue, Messages messages)
			throws ValidationException {
        if (InternalUtils.isBlank(clientValue))
            return null;

        try
        {
            return new BigDecimal(clientValue.trim());
        }
        catch (NumberFormatException ex)
        {
            throw new ValidationException(messages.format("number-format-exception", clientValue));
        }
	}

	public String toClient(BigDecimal value) {
		return value == null ? "" : value.toString();
	}

}


然后contribute 到AppModule

    public static void contributeTranslatorDefaultSource(
            MappedConfiguration<Class, Translator> configuration)
    {
    	configuration.add(BigDecimal.class, new BigDecimalTranslator());
    }
    
    public static void contributeTranslatorSource(
            MappedConfiguration<String, Translator> configuration)
    {
    	configuration.add("bigdecimal",  new BigDecimalTranslator());
    }





当提交form时,会发生一个NPE。 错误发生在。org.apache.tapestry.ioc.services.TapestryIOCModule
        // String to BigDecimal is important, as String->Double->BigDecimal would lose
        // precision.

        add(configuration, String.class, BigDecimal.class, new Coercion<String, BigDecimal>()
        {
            public BigDecimal coerce(String input)
            {
                return new BigDecimal(input);
            }
        });


这里有个很明显的错误。  就是当input为null时。 就发生了NPE.错误如下:

引用
Caused by: org.apache.tapestry.ioc.internal.util.TapestryException: Failure writing parameter value of component basicinformation/AddOrEditDriver:carlong: Coercion of null to type java.math.BigDecimal (via null --> String, String --> java.math.BigDecimal) failed: java.lang.NullPointerException [at classpath:com/javaeye/dengyin2000/gtts/pages/basicinformation/AddOrEditDriver.html, line 50, column 115]
    at org.apache.tapestry.internal.structure.InternalComponentResourcesImpl.writeParameter (InternalComponentResourcesImpl.java:239)
    at org.apache.tapestry.corelib.base.AbstractTextField._$update_parameter_value(AbstractTextField.java)
    at org.apache.tapestry.corelib.base.AbstractTextField.processSubmission (AbstractTextField.java:181)
    at org.apache.tapestry.corelib.base.AbstractField.processSubmission(AbstractField.java:200)
    at org.apache.tapestry.corelib.base.AbstractField.access$100(AbstractField.java:45)
    at org.apache.tapestry.corelib.base.AbstractField$ProcessSubmissionAction.execute(AbstractField.java:114)
    at org.apache.tapestry.corelib.base.AbstractField$ProcessSubmissionAction.execute(AbstractField.java:108)
    at org.apache.tapestry.corelib.components.Form.onAction(Form.java:364)
    ... 45 more
Caused by: java.lang.RuntimeException: Coercion of null to type java.math.BigDecimal (via null --> String, String --> java.math.BigDecimal) failed: java.lang.NullPointerException
    at org.apache.tapestry.ioc.internal.services.TypeCoercerImpl.coerce(TypeCoercerImpl.java:154)
    at $TypeCoercer_1149d2e0ddf.coerce($TypeCoercer_1149d2e0ddf.java)
    at org.apache.tapestry.internal.structure.InternalComponentResourcesImpl.writeParameter(InternalComponentResourcesImpl.java:233)
    ... 52 more
Caused by: java.lang.NullPointerException
    at java.math.BigDecimal .<init>(BigDecimal.java:594)
    at org.apache.tapestry.ioc.services.TapestryIOCModule$7.coerce(TapestryIOCModule.java:219)
    at org.apache.tapestry.ioc.services.TapestryIOCModule$7.coerce(TapestryIOCModule.java :217)
    at org.apache.tapestry.ioc.services.CoercionTuple$CoercionWrapper.coerce(CoercionTuple.java:53)
    at org.apache.tapestry.ioc.internal.services.CompoundCoercion.coerce(CompoundCoercion.java:48)
    at org.apache.tapestry.ioc.internal.services.TypeCoercerImpl.coerce(TypeCoercerImpl.java:150)
    ... 54 more


我试着想去覆盖Tapestry IOC module 的这快代码。  然而没有用。 然后google, 发现原来这是个bug。 这个bug会在5.0.6中解决。可是这是个很明显的错误呀。也不尽快解决掉。 具体可以看。https://issues.apache.org/jira/browse/TAPESTRY-1648, 然后发贴到Tapestry mailling list上问 也没有下文。  既然这样,也只有去自己修改Tapestry-ioc的source code了。 其实这也很简单, 如果是是null 或者是 空字符的话 renturn null就是了。  ok  把tapestry的ioc用svn上拉下来。  简单的修改为

        // String to BigDecimal is important, as String->Double->BigDecimal would lose
        // precision.

        add(configuration, String.class, BigDecimal.class, new Coercion<String, BigDecimal>()
        {
            public BigDecimal coerce(String input)
            {
                    if (input == null || input.trim().length() == 0)
                        return null;
                    return new BigDecimal(input);
            }
        });


打包替换掉tapestry-ioc 5.0.5。 再跑便程序。  ok  通过。
   发表时间:2007-09-21  
請教一下,如果我在 AppModule 加入下列方法會有什麼影響嗎?我對 ioc 還不是很熟悉。

我試了一下,這樣子可以正確將 null 轉換為 Integer 或 Long, 否則一遇到 null 就會有發生錯誤。

而這篇文章提到的方法似乎對我無效
http://www.mail-archive.com/users@tapestry.apache.org/msg15168.html

public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration) {
    	System.out.println("contributeTypeCoercer ===============================");
    	
    	CoercionTuple tuple = new CoercionTuple(Object.class, Long.class, new Coercion<Object, Long>()
        {
            public Long coerce(Object input) {
            	if (input == null)	return null;
            	
                try {
                	return new Long(input.toString());
                } catch (Exception e) {
                	return null;
                }
            }
        });
    	
        configuration.add(tuple);
        
        CoercionTuple tupleInt = new CoercionTuple(Object.class, Integer.class, new Coercion<Object, Integer>()
                {
                    public Integer coerce(Object input) {
                    	if (input == null)	return null;
                    	
                        try {
                        	return new Integer(input.toString());
                        } catch (Exception e) {
                        	return null;
                        }
                    }
                });
            	
                configuration.add(tupleInt);
    }
[url]
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics