我在弄清楚如何使用BeanEditForm组件时遇到了一些麻烦.你知道,只要我没有为我的bean类使用参数化构造函数(并且我需要它们),一切都很好(它显示它应该是什么).这是我的Bean类的样子:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的挂毯模板: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>
Run Code Online (Sandbox Code Playgroud)
它的Java类:
public class AddNewCelebrity {
@Persist
private Celebrity celebrity;
public Celebrity getCelebrity() {
return celebrity;
}
public void setCelebrity(Celebrity celeb) {
this.celebrity = celeb;
}
}
Run Code Online (Sandbox Code Playgroud)
当我不对我的参数化构造函数进行注释时,这是我从tapestry获得的错误:
在SetupRender中渲染队列错误[AddNewCelebrity:celebrity.editor]:异常实例化com.celebreties.celebs.model.Celebrity的实例(对于组件'AddNewCelebrity:celebrity.editor'):错误调用构造函数com.celebreties.celebs.model.Celebrity (String,String,Date,Occupation,String,boolean)(在Celebrity.java:29)(对于服务'BeanModelSource'):没有服务实现接口java.lang.String.
我正在使用带有Tomcat 6.0.32的tapestry 5.2.4
请给出一些指导我可以做些什么.
显然,BeanEditForm不知道要传递给构造函数的参数.它试图为每个参数寻找匹配服务,但No service implements the interface java.lang.String不能这样做.我无法解释为什么它不会简单地使用no-args构造函数而不是尝试猜测其他构造函数之一的参数.
不过,您可以通过在将对象作为参数传递之前自己实例化对象来轻松解决此问题:
public Celebrity getCelebrity() {
if (celebrity == null) {
celebrity = new Celebrity(...);
}
return celebrity;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2800 次 |
| 最近记录: |