4 Replies Latest reply on Aug 4, 2006 1:31 PM by sjmenden

    commandLink not initially executing action when clicked

    sjmenden

      I have a page which allows me to search for registered users to a site.

      The part of the page I have a problem with is as follows, page: showUsers.jsp which makes calls to an EJB3 called userFinder

      <h:commandLink value="S" action="#{userFinder.findFirstPage}" styleClass="alpha_list">
       <f:param name="start" value="S"/>
      </h:commandLink>
      


      Here is the problem. When I initially hit the showUsers.jsp page and click on the link defined above, the page refreshes but nothing happens on the page or in the console output.
      HOWEVER, if I first search for a user (code not shown) THEN I click the link above, then it works and shows me all users starting with the letter S.

      It seems like something contexual, but I don't understand why the #{userFinder.findFirstPage} action isn't working on the commandLink before I search for something.

      @Name("userFinder")
      @Stateful
      @Scope(ScopeType.SESSION)
      public class UserFinderBean implements UserFinder {
      
       private User example = new User();
       public User getExample() {
       return example;
       }
      
      
       private String[] searchColumns;
       public String[] getSearchColumns() {
       return searchColumns;
       }
       public void setSearchColumns(String[] searchColumns) {
       this.searchColumns = searchColumns;
       }
      
      
       private SelectItem[] searchColumnItems = new SelectItem[] {
       new SelectItem("username", "Username"),
       new SelectItem("firstName", "First"),
       new SelectItem("lastName", "Last"),
       new SelectItem("company", "Company"),
       new SelectItem("email", "Email")
       };
       public SelectItem[] getSearchColumnItems() {
       return searchColumnItems;
       }
      
       private int pageNumber = 0;
       private int pageSize = 15;
       public void setPageSize(int size) {
       pageSize = size;
       }
       public int getPageSize() {
       return pageSize;
       }
      
       public boolean isPreviousPage() {
       return userList!=null && pageNumber>0;
       }
       public boolean isNextPage() {
       return userList!=null && userList.size()==pageSize;
       }
      
       @Logger
       private Log log;
      
       @DataModel
       private List<User> userList;
      
       @DataModelSelection
       private User selectedUser;
      
       @PersistenceContext
       private EntityManager entityManager;
      
       private void executeQuery() {
       Map<String, Object> parameters = new HashMap<String, Object>();
       StringBuffer queryString = new StringBuffer();
      
       log.info("searchColumns: " + Arrays.asList(searchColumns));
      
      
       //*******************************************************//
       // Handle case when a user clicks on a letter
       //*******************************************************//
       if(start != null && !start.equals("")) {
       queryString.append(" or user.username" + " like :value or user.username like :valuetwo ");
       parameters.put("value", start + "%");
       parameters.put("valuetwo", start.toLowerCase() + "%");
       }
      
      
      // if(example.getUsername() != null ) {
      // if(example.getUsername().contains("%")) {
      // queryString.append(" and user.username like :username");
      // } else {
      // queryString.append(" and user.username = :username");
      // }
      // parameters.put( "username", example.getUsername() );
      // }
      
       if(example.getUsername() != null ) {
       int x = 0;
       for(String searchColumn : searchColumns) {
       if(example.getUsername().contains("%")) {
       queryString.append(" or user." + searchColumn + " like :" + searchColumn);
       } else {
       queryString.append(" or user." + searchColumn + " = :" + searchColumn);
       }
       parameters.put( searchColumn, example.getUsername() );
       x++;
       }
       }
      
       if(queryString.length() == 0) {
       queryString.append("select user from User user");
       } else {
       queryString.delete(0, 3).insert(0, "select user from User user where ");
       }
      
       if(order!=null) {
       queryString.append(" order by user.").append(order);
       if (descending) queryString.append(" desc");
       }
      
      
      // Properties of a JavaBean can be bound to named query parameters:
      //
      // Query q = s.createQuery("from foo Foo as foo where foo.name=:name and foo.size=:size");
      // q.setProperties(fooBean); // fooBean has getName() and getSize()
      // List foos = q.list();
      
       log.info("example.username: " + example.getUsername() + " queryString: \n\n" + queryString + "\n\n");
      
       Query query = entityManager.createQuery(queryString.toString());
       for (Entry <String, Object> param : parameters.entrySet()) {
       query.setParameter( param.getKey(), param.getValue() );
       }
       userList = (List<User>) query.setMaxResults(pageSize)
       .setFirstResult(pageSize*pageNumber)
       .getResultList();
       }
      
       public String findFirstPage() {
       log.info("Find First Page");
       pageNumber=0;
       executeQuery();
       return null;
       }
      
       public String findNextPage() {
       pageNumber++;
       executeQuery();
       return null;
       }
      
       public String findPreviousPage() {
       pageNumber--;
       executeQuery();
       return null;
       }
      
       public void refresh() {
       if (userList!=null) executeQuery();
       }
      
       public String clear() {
       userList=null;
       example = new User();
       return null;
       }
      
       public User getSelection() {
       return entityManager.merge( selectedUser );
       }
      
       @Destroy @Remove
       public void destroy() {}
      
       private String order;
       private boolean descending = false;
      
       @RequestParameter
       private String orderBy;
      
       @RequestParameter
       private String start;
      
       public String reorder() {
       if (orderBy.equals(order)) {
       descending = !descending;
       }
       else {
       descending = false;
       }
       order = orderBy;
       executeQuery();
       return null;
       }
      
      }
      


        • 1. Re: commandLink not initially executing action when clicked
          pmuir

          It would be easier to help if simplyfy your code so that it contains only the relevant methods. Also can you show the whole JSP page. Do you have a h:form? Does it contain any input fields? Do you get any errors from h:messages?

          • 2. Re: commandLink not initially executing action when clicked
            sjmenden

            Below is the showUsers.jsp.

            What I want to use is allow the user to click on the R or the S and for that to bind to the userFinder.findFirstPage which will look for the start variable which is set to R or S and find all users that start with and R or S and return the list.

            As I said before, when going to the page for the first time, the link will just refresh the page and do nothing and no output, however, if I first search for a user, then I click on the R or the S links, then it all works.

            showUsers.jsp:

            <h:form>
            <table>
             <tr >
             <td><h2>Users</h2></td>
             </tr>
             <div>
             <table>
             <tr>
             <td align="left">
             ...
             ...
             ...
             <h:commandLink value="R" action="#{userFinder.findFirstPage}">
             <f:param name="start" value="R"/>
             </h:commandLink>
             <h:commandLink value="S" action="#{userFinder.findFirstPage}">
             <f:param name="start" value="S"/>
             </h:commandLink>
             ...
             ...
             ...
             </td>
             </tr>
             </table>
             </div>
            <table>
             <tr>
             <td colspan="5">
             <div>
             <div>
             <div>
             <div>
             <span>User:</span>
             <h:inputText value="#{userFinder.example.username}" size="15" required="true"/>
             <h:commandButton type="submit" value="Search" action="#{userFinder.findFirstPage}"/>
             </div>
             <br />
             <h:selectManyCheckbox
             value="#{userFinder.searchColumns}"
             layout="lineDirection"
             required="true">
             <f:selectItems value="#{userFinder.searchColumnItems}" />
             </h:selectManyCheckbox>
             <br />
             <span>(for wildcard search use %)</span>
             </div>
             </div>
             </div>
             </td>
             </tr>
            </table>
            
             <h:dataTable value="#{userList}" var="user" rendered="#{userList.rowCount > 0}">
             <h:column>
             <f:facet name="header">
             <h:commandLink value="Username" action="#{userFinder.reorder}">
             <f:param name="orderBy" value="username"/>
             </h:commandLink>
             </f:facet>
             <h:outputText value="#{user.username}"/>
             </h:column>
            
             <h:column>
             <f:facet name="header">
             <h:commandLink value="Company" action="#{userFinder.reorder}">
             <f:param name="orderBy" value="company"/>
             </h:commandLink>
             </f:facet>
             <h:outputText value="#{user.company}"/>
             </h:column>
            
             <h:column>
             <f:facet name="header">
             <h:outputText value="Name"/>
             </f:facet>
             <h:outputText value="#{user.firstName} #{user.lastName}"/>
             </h:column>
            
             <h:column>
             <f:facet name="header">
             <h:commandLink value="Email Address" action="#{userFinder.reorder}">
             <f:param name="orderBy" value="email"/>
             </h:commandLink>
             </f:facet>
             <h:outputText value="#{user.email}"/>
             </h:column>
            
             <h:column>
             <f:facet name="header">
             <h:commandLink value="Active" action="#{userFinder.reorder}">
             <f:param name="orderBy" value="active"/>
             </h:commandLink>
             </f:facet>
             <h:outputText value="#{user.active}"/>
             </h:column>
             </h:dataTable>
            
             <h:commandButton action="#{userFinder.findPreviousPage}" value="Previous Page" disabled="#{!userFinder.previousPage}" />
             <h:commandButton action="#{userFinder.findNextPage}" value="Next Page" disabled="#{!userFinder.nextPage}" />
            </h:form>
            



            Here is my UserFinderBean with only the relevant information to this question. If you want to see the full code, see my previous post.
            @Name("userFinder")
            @Stateful
            @Scope(ScopeType.SESSION)
            public class UserFinderBean implements UserFinder {
            
             @DataModel
             private List<User> userList;
            
             @DataModelSelection
             private User selectedUser;
            
             @PersistenceContext
             private EntityManager entityManager;
            
             private void executeQuery() {
             Map<String, Object> parameters = new HashMap<String, Object>();
             StringBuffer queryString = new StringBuffer();
            
             if(start != null && !start.equals("")) {
             queryString.append(" or user.username" + " like :value or user.username like :valuetwo ");
             parameters.put("value", start + "%");
             parameters.put("valuetwo", start.toLowerCase() + "%");
             }
            
             Query query = entityManager.createQuery(queryString.toString());
             for (Entry <String, Object> param : parameters.entrySet()) {
             query.setParameter( param.getKey(), param.getValue() );
             }
             userList = (List<User>) query.setMaxResults(pageSize)
             .setFirstResult(pageSize*pageNumber)
             .getResultList();
             }
            
             public String findFirstPage() {
             executeQuery();
             return null;
             }
            
             @Destroy @Remove
             public void destroy() {}
            
            
             @RequestParameter
             private String start;
            
            }
            


            • 3. Re: commandLink not initially executing action when clicked
              pmuir

              My guess would be that there was a conversion, validation or model update error from your input fields (probably searchColumns is null) but as your jsf seems to have no h:messages it's hard to tell.

              • 4. Re: commandLink not initially executing action when clicked
                sjmenden

                Thank you petemuir, all I needed to do was output the messages, then I saw the validation error, because I had two fields as required, I took the required off and everything worked.