Reputation: 920
hi guys sorry to post again but this is really confusing me
i have a query that i can get to run, the user enters a string and then the application will search for this in the database connected to the application, this bit i have working, however now i have successfully run the queury i am unsure of how to retrieve the data from it and be able to use it in the view in a h:datatable
currently to do the search, this is what is in the view, the faclet page the user enters
<h:inputText id="search" value="#{userdetailsController.search}" />
<p:commandButton value="Search" action="#{userdetailsController.submit}" ajax="true" />
this then goes to
@Named("userdetailsController")
@SessionScoped
public class UserdetailsController implements Serializable {
@EJB
private String search;
private List<Userdetails> item; // No need for DataModel here.
public String submit() {
item = ejbFacade.searchByString(search);
return ""; //change this !, testing only
}
public String getSearch() {
return search;
}
public void setSearch(String search) {
this.search = search;
}
then
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
private List userList;
/* trying out a search function */
public List<T> searchByString(String string) {
System.out.println("in SearchByString");
return getEntityManager().createNamedQuery("Userdetails.findByUsername").setParameter("username", "%" + string + "%").getResultList();
}
which does the search in
@Entity
@Table(name = "USERDETAILS")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Userdetails.findAll", query = "SELECT u FROM Userdetails u"),
@NamedQuery(name = "Userdetails.findById", query = "SELECT u FROM Userdetails u WHERE u.id = :id"),
@NamedQuery(name = "Userdetails.findByUsername", query = "SELECT u FROM Userdetails u WHERE u.username = :username")})
How can i once this process has been completed retrieve the data to print out in the console for example, what do i need to call to do this ?
Upvotes: 1
Views: 357
Reputation: 1108632
i am unsure of how to retrieve the data from it
Your code already does that in the submit()
method.
and be able to use it in the view in a h:datatable
Just add the following to your view, maybe below the command button in the same form, in order to present it:
<h:dataTable value="#{userdetailsController.item}" var="userdetails">
<h:column>#{userdetails.username}</h:column>
</h:dataTable>
And make sure that this is covered in the ajax update of the command button:
<p:commandButton ... update="@form" />
Unrelated to the concrete problem, I'd only rename the confusing property name item
to items
as it represents a collection of items, not a single item. Also, your submit()
method doesn't need to return an empty string. It can also just be declared void
. Also, ajax="true"
is the default already for <p:commandButton>
, you can just omit it.
Upvotes: 3