gpt4 book ai didi

Java EE 将 Controller 类与 DAO 类连接

转载 作者:行者123 更新时间:2023-11-30 01:56:19 25 4
gpt4 key购买 nike

我尝试将 Controller 连接到 DAO 类,但收到 UnsupportedOperationException。这是我的 Controller :

@Named("contactsController")
@SessionScoped

public class ContactsController implements Serializable {
private static final long serialVersionUID = 1L;

protected List<Contact> contacts;

protected ContactsDAO contactsDAO = new ContactsDAOImp();

@Inject
public ContactsController(ContactsDAO contactsDAO) {
this.contactsDAO = contactsDAO;
}

public List<Contact> getContacts() {
return contacts;
}

@PostConstruct
public void init() {
this.contacts = contactsDAO.getAllContacts();
}
}

这是我的 DAO:

@Named
@ApplicationScoped
public class ContactsDAOImp implements Serializable, ContactsDAO {
// DAO code here
}

错误:WELD-000049:无法在 com.controller.ContactsController@7e7514ca 上调用 public void com.controller.ContactsController.init()

似乎我未能将 DAO Referenec 注入(inject) Controller ,但我不确定我做错了什么

最佳答案

试试这个

@Named("contactsController")
@SessionScoped
public class ContactsController implements Serializable {

private static final long serialVersionUID = 1L;
private List<Contact> contacts;

@Inject
private ContactsDAOImp contactsDAOImp;

public ContactsController() { }

public List<Contact> getContacts() {
return contacts;
}

@PostConstruct
public void init() {
this.contacts = contactsDAO.getAllContacts();
}
}

ContactDAO(存储库?)

@Named
@ApplicationScoped
public class ContactsDAOImp implements Serializable, ContactsDAO {
// DAO code here
}

编辑:您无法将存储库注入(inject)到 Controller 中,您应该在 Controller 和存储库之间放置一个层(服务)以在将数据发送到 View 之前操作数据

关于Java EE 将 Controller 类与 DAO 类连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54401786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com