我想用Spring Web MVC创建一个JSF应用程序。作为范围,我使用视图范围。我的问题是bean的行为类似于请求范围。我在Bean的开头初始化了一个对象,但是如果调用save方法,则该对象为null。有谁知道为什么我的豆子会这样呢?
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Named
@ViewScoped
public class DeveloperBean implements Serializable {
@Inject
private DeveloperService service;
public List<DeveloperDTO> developerDTOList = new ArrayList<>();
public DeveloperDTO selectedDeveloper = new DeveloperDTO();
public String nametest;
public DeveloperBean() {
}
@PostConstruct
public void developerBeanInit() {
selectedDeveloper = new DeveloperDTO();
// loadDevelopers();
}
public void loadDevelopers() {
List<DeveloperDTO> developerList = service.getDeveloperList();
if (developerList.isEmpty()) {
developerList = new ArrayList<>();
}
developerDTOList = developerList;
selectedDeveloper = new DeveloperDTO();
}
public void onRowSelect(SelectEvent event) {
DeveloperDTO dto = (DeveloperDTO) event.getObject();
FacesMessage msg = new FacesMessage("Developer selected", dto.getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowUnselect(UnselectEvent event) {
DeveloperDTO dto = (DeveloperDTO) event.getObject();
FacesMessage msg = new FacesMessage("Developer unselected", dto.getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void loadSelectedDevelopers(String name) {
selectedDeveloper = service.getSelectedDeveloper(name);
}
public void save() {
System.out.println(selectedDeveloper.getName());
selectedDeveloper = service.save(selectedDeveloper);
}
public void delete() {
service.delete(selectedDeveloper.getName());
}
public DeveloperService getService() {
return service;
}
public void setService(DeveloperService service) {
this.service = service;
}
public List<DeveloperDTO> getDeveloperDTOList() {
return developerDTOList;
}
public void setDeveloperDTOList(List<DeveloperDTO> developerDTOList) {
this.developerDTOList = developerDTOList;
}
public DeveloperDTO getSelectedDeveloper() {
return selectedDeveloper;
}
public void setSelectedDeveloper(DeveloperDTO selectedDeveloper) {
this.selectedDeveloper = selectedDeveloper;
}
public String getNametest() {
return nametest;
}
public void setNametest(String nametest) {
this.nametest = nametest;
}
}
具体对象是selectedDeveloper对象。调试器的结果是在构造函数中是用空对象初始化的对象。如果我使用质数按钮进入save方法,则该对象为null。我希望有人知道这个问题,可以为我提供帮助