📄 contactcontroller.java
字号:
package com.fit.controller;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UICommand;
import javax.faces.component.UIForm;
import javax.faces.context.FacesContext;
import com.fit.model.Contact;
import com.fit.model.ContactRepository;
public class ContactController {
private ContactRepository contactRepository;
/** The current contact that is being edited. */
private Contact contact = new Contact();
/** Contact to remove. */
private Contact selectedContact;
/** The current form. */
private UIForm form;
/** Add new link. */
private UICommand addNewCommand;
/** Persist command. */
private UICommand persistCommand;
/** For injection of collaborator. */
public void setContactRepository(ContactRepository contactRepository) {
this.contactRepository = contactRepository;
}
public void addNew() {
form.setRendered(true);
addNewCommand.setRendered(false);
persistCommand.setValue("Add");
}
public void persist() {
form.setRendered(false);
addNewCommand.setRendered(true);
if (contactRepository.persist(contact) == null) {
addStatusMessage("Added " + contact);
} else {
addStatusMessage("Updated " + contact);
}
}
public void remove() {
contactRepository.remove(selectedContact);
addStatusMessage("Removed " + selectedContact);
}
public void read() {
contact = selectedContact;
form.setRendered(true);
addNewCommand.setRendered(false);
addStatusMessage("Read " + contact);
persistCommand.setValue("Update");
}
private void addStatusMessage(String message) {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public Contact getSelectedContact() {
return selectedContact;
}
public void setSelectedContact(Contact selectedContact) {
this.selectedContact = selectedContact;
}
public UIForm getForm() {
return form;
}
public void setForm(UIForm form) {
this.form = form;
}
public UICommand getAddNewCommand() {
return addNewCommand;
}
public void setAddNewCommand(UICommand addNewCommand) {
this.addNewCommand = addNewCommand;
}
public UICommand getPersistCommand() {
return persistCommand;
}
public void setPersistCommand(UICommand persistCommand) {
this.persistCommand = persistCommand;
}
public ContactRepository getContactRepository() {
return contactRepository;
}
public List<Contact> getContacts() {
return contactRepository.getContacts();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -