Search code examples
jsfsearchjpaderby

Search function not doing anything


sorry if this is a poor question but this one feature have been driving me mad for days so i thought id post it here to see if you guys can help me

basically all i want to do from a jsf page have the user search a user and for me to return all the details

<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>

that is the jsf page, working fine

it calls my userdetailsController class

@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;
    }
......

a contactsService class

@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();
    }
}

an AbstractFacade class

   /* 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();
    }

and the Userdetails class with the query i am trying to search

 @NamedQuery(name = "Userdetails.findByUsername", query = "SELECT u FROM Userdetails u WHERE u.username = :username")})

currently only the getters and settings are working in Getsearch

how can i make this work as i have spent days on this feature and are still no closer, sorry this is my first time at this

thanks guys

EDIT

would adding

public List<Userdetails> getAllPersons() {
    if (search == null) {
        return cs.getAllPersons();
    }
    return cs.searchByString(search);
}

in the UserdetailsController be enough ?


Solution

  • You're not invoking any action here:

    <h:commandButton value="Search" action="index"/>
    

    So it's indeed logical that it isn't "doing anything".

    You need to invoke a managed bean action which in turn executes the desired code to obtain the desired data from the DB and assign to a property:

    <h:commandButton value="Search" action="#{userdetailsController.submit}" />
    

    with inside 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";
    }
    

    Your whole ContactsService seems useless by the way.

    As per your attempt in the getter method in the update of your question, please don't do that. You should never call the DB in a getter method for the reasons mentioned in Why JSF calls getters multiple times