2

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 ?

user2061913
  • 910
  • 2
  • 14
  • 34

1 Answers1

3

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

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi there thanks very much for the answer, it now run and everything but when i search the username `test` i get the following error message `java.lang.IllegalArgumentException: You have attempted to set a parameter value using a name of string that does not exist in the query string SELECT u FROM Userdetails u WHERE u.username = :username.` how can this be corrected – user2061913 Nov 19 '13 at 19:50
  • 2
    This is unrelated to the question currently asked. Press "Ask Question" button on right top in order to be able to get an answer on that. – BalusC Nov 19 '13 at 19:53