我正在尝试创建常规联系人POJO接收电话号码和姓名。然后在JSF上,我正在读取这些属性并尝试调用支持方法将它们添加到联系人列表中。但由于某种原因,联系人不断回来。我正在尝试使用manage bean属性,但没有运气。任何帮助将不胜感激。这是我的联系方式
package com.contact.TO;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
@ManagedBean
@RequestScoped
public class ContactTO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String phoneNumber;
public ContactTO() {
name=null;
phoneNumber=null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
这是我的支持类
package com.contact.backing;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.view.ViewScoped;
import com.contact.TO.ContactTO;
@ManagedBean(name="contactBacking")
@RequestScoped
public class ContactBacking implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@ManagedProperty(value="#{contactTO}")
private ContactTO contact;
private List<ContactTO> contacts = new ArrayList<>();
public ContactBacking() {
super();
// TODO Auto-generated constructor stub
}
public void addContact(){
contacts.add(contact);
contact=null;
}
public void saveContact(String name, String phone, String oldName, String oldPhone){
for(ContactTO c : contacts){
if(c.getName().equalsIgnoreCase(oldName) && c.getPhoneNumber().equalsIgnoreCase(oldPhone)){
c.setName(name);
c.setPhoneNumber(phone);
}else{
addContact();
}
}
}
public List<ContactTO> getContacts() {
return contacts;
}
public void setContacts(List<ContactTO> contacts) {
this.contacts = contacts;
}
public ContactTO getContact() {
return contact;
}
public void setContact(ContactTO contact) {
this.contact = contact;
}
}
这是我的jsf:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head><title></title>
<link href="./css/styles.css" rel="stylesheet" type="text/css"/>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{contactTO.name}" />
<h:inputText value="#{contactTO.phoneNumber}" />
<h:commandButton value="Save"
action="#{contactBacking.addContact}" >
<f:ajax render="contactList" />
</h:commandButton>
<h:panelGroup id="contactList">
<ui:repeat var="c" value="#{contactBacking.contacts}" varStatus="status">
<h:inputText value="#{c}"></h:inputText>
</ui:repeat>
</h:panelGroup>
</h:form>
</h:body></html>
正如您所看到的,我只有两个输入接受属性名称,而phoneNumber和action按钮调用支持方法contactBacking.addContact。但我得到的所有内容对于联系人来说都是空的。管理bean或范围是否错误?
答案 0 :(得分:2)
答案 1 :(得分:0)
导入错误,使用javax.faces.bean而不是javax.faces.view用于托管bean Scopes,包javax.faces.view是CDI规范的一部分。