2011-03-20 66 views
1

我在处理BeanEditForm组件时遇到了一些问题。 你看,一切都很好(它显示它应该),只要我不使用参数化的构造函数为我的bean类(和我需要他们)。这是我Bean类看起来像:Tapestry 5 BeanEditForm组件故障

public class Celebrity { 
    private String firstName; 
    private String lastName; 
    private long ID; 
    private Date dateOfBirth; 
    private Occupation occupation; 
    private String biography; 
    private boolean birthDateVerified; 

    public Celebrity() { 
    } 

    public Celebrity(String firstName, String lastName, Date dateOfBirth, Occupation occupation, String biography, boolean birthDateVerified) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.dateOfBirth = dateOfBirth; 
     this.occupation = occupation; 
     this.biography = biography; 
     this.birthDateVerified = birthDateVerified; 
    } 

    public Celebrity(String firstName, String lastName, Date dateOfBirth, Occupation occupation) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.dateOfBirth = dateOfBirth; 
     this.occupation = occupation; 
    } 


    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 


    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public long getID() { 
     return ID; 
    } 

    public void setID(long ID) { 
     this.ID = ID; 
    } 

    public Date getDateOfBirth() { 
     return dateOfBirth; 
    } 

    public void setDateOfBirth(Date dateOfBirth) { 
     this.dateOfBirth = dateOfBirth; 
    } 

    public Occupation getOccupation() { 
     return occupation; 
    } 

    public void setOccupation(Occupation occupation) { 
     this.occupation = occupation; 
    } 

    /** 
    * @return the biography 
    */ 
    public String getBiography() { 
     return biography; 
    } 


    public void setBiography(String biography) { 
     this.biography = biography; 
    } 

    public boolean getBirthDateVerified() { 
     return birthDateVerified; 
    } 

    public void setBirthDateVerified(boolean birthDateVerified) { 
     this.birthDateVerified = birthDateVerified; 
    } 
} 

这是我的挂毯模板:AddNewCelebrity.tml

<html t:type="layout" title="Celebrity Details" 
     xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd" 
     xmlns:p="tapestry:parameter"> 

    <head> 
     <Title>Adding new celebrety</Title> 
    </head> 

    <body> 
     <t:beaneditform t:id="celebrity"/> 
    </body> 

</html> 

及其Java类:

public class AddNewCelebrity { 

    @Persist 
    private Celebrity celebrity; 

    public Celebrity getCelebrity() { 
     return celebrity; 
    } 

    public void setCelebrity(Celebrity celeb) { 
     this.celebrity = celeb; 
    } 
} 

这是错误,我当我不评论我的参数化构造函数时从挂毯获得:

SetupRender中的渲染队列错误[AddNewCelebrity:celebrity.editor]:异常实例化com.celebreties.celebs.model.Celebrity(对于组件'AddNewCelebrity:celebrity.editor'):错误调用构造函数com.celebreties.celebs.model。名人(字符串,字符串,日期,职业,字符串,布尔)(在Celebrity.java:29)(用于服务'BeanModelSource'):没有服务实现接口java.lang.String。

我用挂毯5.2.4和Tomcat 6.0.32

请给些指导我该怎么办。

回答

5

显然,BeanEditForm不知道传递给构造函数的参数。它试图为每个参数寻找一个匹配的服务,但是不能完成。我无法真正解释为什么它不会简单地使用无参数构造函数而不是尝试猜测其他构造函数之一的参数。

不过,你可以很容易地将它作为一个参数之前解决此通过简单地自行实例化对象:

public Celebrity getCelebrity() { 
    if (celebrity == null) { 
     celebrity = new Celebrity(...); 
    } 
    return celebrity; 
} 
+0

感谢Martin,这完全解决了我的问题。我专注于寻找某种模型类,它可以作为BeanEditForm中的模型组件插入。这并不是浪费时间,但这是更优雅的解决方案。 – 2011-03-20 14:35:21

2

这是最近加入作为官方FAQ:

http://tapestry.apache.org/beaneditform-faq.html

当你渲染一个BeanEditForm,或者当呈现的表单被提交时,Tapestry必须实例化一个要编辑的对象。当BeanEditForm的对象参数绑定为null时,会发生这种情况:Tapestry实例化属性类型的一个实例,以便BeanEditForm具有一个对象来读取默认值或将提交的值推入。

默认情况下,它使用标准注入机制,这意味着Tapestry将标识具有最多参数的公共构造函数,并尝试为每个构造函数参数查找对象和其他对象。

有两种方法微调该这样你就不会得到错误:

放在正确的构造函数@Inject注释使用(通常,无参数的构造)。或者,为“准备”事件提供事件处理程序方法,并将实例化的实例放入属性中。

public class MyPage { 
    @Property 
    public MyBean myBean; 

    // The template contains <t:beaneditform t:id="mybeaneditor"/> ... 

    void onPrepareFromMyBeanEditor() { 
    myBean = new MyBean(); 
    } 
} 
1

就在几分钟前我遇到了类似的问题。我使用netbeans和我从数据库导入的实体类。无论如何,如果你导入的实体类IDE生成3个构造函数,并且你真正需要的只有一个 - 空的,你可以简单地擦除其他两个构造函数,并且如果添加@Property注释,它将工作正常。

像这样:

@Property 
@Persist 
private Celebrity celebrity; 

和实体类删除这两个构造函数!

问候, 米洛斯D.

-1

如果您需要更多的构造函数,只要把上面的默认构造函数标注@Inject。你将能够使用带参数的构造函数,而Tapestry将使用默认的构造函数。