我正在尝试将文本字段绑定到对象。我做了一些研究,找到了这个答案。
$('#my-table').on('click', '.delete-btn', function(){
var deleteBtn = $(this),
id = deleteBtn.data('id'),
title = deleteBtn.closest('tr').find('.title').text();
swal({
title: "Are u sure?",
text: title + " are u want delete!",
icon: "warning",
buttons: {
cancel: {
text: "No, iptal!",
value: null,
visible: true,
className: "",
closeModal: false,
},
confirm: {
text: "Yes, sil!",
value: true,
visible: true,
className: "",
closeModal: false
}
}
})
});
然后,您可以像这样绑定街道地址:
public class Person {
String name;
String surname;
Address address;
// assume getters and setters
}
public class Address {
String street;
// assume getter and setters
}
我将街道实例化为什么值(在代码的最后一行中)?
以上是我找到的示例。我的代码如下-我有一个联系人类:
Binder<Person> binder = new Binder<>();
TextField streetAddressField = new TextField();
// bind using lambda expressions
binder.bind(streetAddressField,
person -> person.getAddress().getStreet(),
(person, street) -> person.getAddress().setStreet(street));
然后我有一个phoneType类:
@Entity
public class Contact {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String phoneNumber;
@ManyToOne (cascade = {CascadeType.ALL})
@JoinColumn(name="phoneType_typeId")
private PhoneType phoneType;
public Contact(){
}
public Contact(String firstName, String lastName, String phoneNumber, PhoneType type) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.phoneType = type;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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 String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public PhoneType getPhoneType() {
return phoneType;
}
public void setPhoneType(PhoneType phoneType) {
this.phoneType = phoneType;
}
@Override
public String toString() {
return String.format("Contact[firstName='%s', lastName='%s', phoneNumber='%s', phoneType = '%s']",
firstName, lastName, phoneNumber, phoneType);
}
}
然后在联系人编辑器中,我试图将phoneType绑定到文本字段:
@Entity
@Table(name="phoneType")
public class PhoneType {
@Id
@GeneratedValue
@Column(name = "typeId")
private Long id;
private String type;
public PhoneType(String type){
this.type = type;
}
public PhoneType(){}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return type;
}
}
联系人编辑器中用**括起来的行(例如,binder.bind(phoneType,contact.getPhoneType()。getType(),contact.getPhoneType()。setType(type)))给我一个错误-“否存在类型变量FIELDVALUE的实例,以便字符串符合ValueProvider。
答案 0 :(得分:3)
行
binder.bind(phoneType, contact.getPhoneType().getType(), contact.getPhoneType().setType(type));
无法编译,因为方法参数与任何bind
方法都不匹配,并且第三个参数中包含非法的Java表达式。根据您的问题,您只是忘记了使用lambda。试试:
binder.bind(phoneType, c -> c.getPhoneType().getType(), (c, t) -> c.getPhoneType().setType(t));
public <FIELDVALUE> Binder.Binding<BEAN,FIELDVALUE> bind(HasValue<FIELDVALUE> field,
ValueProvider<BEAN,FIELDVALUE> getter,
Setter<BEAN,FIELDVALUE> setter)
它期望ValueProvider
和Setter
作为第二和第三参数。这些接口只有一种方法可以实现,因此您可以使用lambda将它们传递给bind
。
答案 1 :(得分:0)
我不知道这是否是您要的内容,但我认为丢失的是您尚未将绑定程序绑定到任何bean。
您已经创建了活页夹,并且您已经告诉文本字段绑定了哪个属性,但是现在您需要告诉活页夹哪个是他的bean。
类似的东西:
Person yourPerson = new Person(); //or get person from database somehow
yourPerson.setAddress(new Address());
yourPerson.getAddress().setStreet("Road cool code, 404");
binder.setBean(yourPerson);
这应该可以解决问题...如果没有,请更好地解释您的需求。 ;)