抱歉,如果这是一个糟糕的问题,但这一个功能让我疯了几天,所以我想在这里发帖,看看你们是否可以帮助我
基本上所有我想从jsf页面做的都有用户搜索用户并让我返回所有细节
<h:form id="searchForm">
<h:outputLabel value="Search: " style="font-weight:bold" />
<h:inputText id="search" value="#{userdetailsController.search}" />
<h:commandButton value="Search" action="index"/>
</h:form>
这是jsf页面,工作正常
它调用我的userdetailsController类
@Named("userdetailsController")
@SessionScoped
public class UserdetailsController implements Serializable {
private Userdetails current;
private DataModel items = null;
@EJB
private Richard.beans.UserdetailsFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;
private String search;
public String getSearch() {
System.out.println("inGetSearch");
return search;
}
public void setSearch(String search) {
this.search = search;
}
......
一个contactsService类
@Stateless
public class ContactsService {
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
@EJB
private UserdetailsFacade cf;
public List<Userdetails> searchByString(String string) {
return cf.searchByString(string);
}
public List<Userdetails> getAllPersons() {
return cf.findAll();
}
}
AbstractFacade类
/* trying out a search function */
public List<T> searchByString(String string) {
System.out.println("in SearchByString");
return getEntityManager().createNamedQuery("Userdetails.findByUsername").setParameter("string", "%" + string + "%").getResultList();
}
和带有我正在尝试搜索的查询的Userdetails类
@NamedQuery(name = "Userdetails.findByUsername", query = "SELECT u FROM Userdetails u WHERE u.username = :username")})
目前只有getters和设置在Getsearch
中有效我怎样才能完成这项工作,因为我已经花了好几天时间来完成这项功能并且仍然没有接近,抱歉这是我第一次参加此活动
谢谢你们
修改
会添加
public List<Userdetails> getAllPersons() {
if (search == null) {
return cs.getAllPersons();
}
return cs.searchByString(search);
}
在UserdetailsController中就足够了吗?
答案 0 :(得分:3)
您没有在此处调用任何操作:
<h:commandButton value="Search" action="index"/>
因此,它“不做任何事情”确实是合乎逻辑的。
您需要调用托管bean操作,该操作又执行所需的代码以从DB获取所需数据并分配给属性:
<h:commandButton value="Search" action="#{userdetailsController.submit}" />
内有UserdetailsController
:
private String search;
private List<UserDetail> items; // No need for DataModel here.
@EJB
private UserdetailsFacade ejbFacade;
public String submit() {
items = ejbFacade.searchByString(search);
return "index";
}
顺便说一句,你的整个ContactsService
似乎毫无用处。
根据您在更新问题时尝试使用getter方法,请不要这样做。出于Why JSF calls getters multiple times
中提到的原因,您不应该使用getter方法调用DB