0

I have a page with an inputtextwhere I type the name of an author to search for, when I click the a4j:commandButton searchButton, I'd like to enable the h:commandButton GoToBUtton, if the search returns no authors and to disabled that button, if the search returns an author. At first, the GoToButton should be disabled, that's the default behaviour.

The problem is, goToButton is always disabled, it never changes. What's wrong?

<h:form>
 <div align="left">
<rich:panel id="panel" style="width:310px">
           <h:panelGrid columns="3">
              Nome do autor: <h:inputText id="nameInput" 
              value="#{insertAuthControllerPt1.nameToSearch}"/>

                <a4j:commandButton id="searchButton" value="Procurar" 
           action="#{insertAuthControllerPt1.searchAuthor()}"
            render="authorTable, goToButton">              
           </a4j:commandButton>

           </h:panelGrid>  
       </rich:panel> 


   <h:panelGrid id="authorTable">
       <rich:dataTable value="#{insertAuthControllerPt1.authorListOfMap}"  var="result">
            <c:forEach items="#{insertAuthControllerPt1.variableNames}" var="vname">
                 <rich:column>
                     <f:facet name="header">#{vname}</f:facet>
                      #{result[vname]}
                 </rich:column> 
            </c:forEach>    
       </rich:dataTable>
       <br /> 
   </h:panelGrid>


<h:commandButton id="goToButton" value="Go" action="InsertAuthorPt2" 
           disabled="#{insertAuthControllerPt1.disabled}">
             <f:setPropertyActionListener target="#{insertAuthorController.flag}"
                         value="true" />      
           </h:commandButton>
 </div>
</h:form>

Search Method at InsertAuthControllerPt1.java

public class InsertAuthorControllerPt1 {
    private boolean disabled;
    private String nameToSearch;
    private List<String> variableNames;
    private List<Map<String, String>> authorListOfMap;
    private String flag;
    private Map<String, Object> sessionMap;
    private List<Author> authors; 

    public InsertAuthorControllerPt1() {
        this.authorListOfMap = new ArrayList<Map<String, String>>();
        this.variableNames = new ArrayList<String>();
        this.sessionMap = new HashMap<String, Object>();
        this.authors = new ArrayList<Author>();
        this.disabled = true;
    }

    public void searchAuthor() {

            this.variableNames.clear();
            this.authorListOfMap.clear();

           if( this.nameToSearch.equals("") ) {
                this.disabled = true;
           } else {

             try {
                this.findAuthorByNameOperation(this.sessionMap, this.flag, this.nameToSearch);
            }catch(NullPointerException n) {

                this.disabled = false;

                }catch (Exception e) {
                e.printStackTrace();
            } 

                if( (authors != null) && (!authors.isEmpty()) ) {

                    this.disabled = true;

                    for( Author author : authors ) {
                        Map<String, String> map = new HashMap<String, String>();

                        map.put("URI", author.getUri().toString());
                        map.put("Nome", author.getName());
                        map.put("Email", author.getEmail());
                        this.authorListOfMap.add(map);  
                    }
              this.addVariableNames();
            }
}
Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32
Luciane
  • 259
  • 4
  • 23

2 Answers2

1

First it is highly discouraged to mix JSTL tags and Facelets tags (see: JSTL in JSF2 Facelets… makes sense?)

Then you will use the f:ajax tag. You will basically use an ajax call to render the goToButton after the action has been executed

<h:commandButton id="searchButton" value="Search" action="#{insertAuthControllerPt1.searchAuthor()}">
    <f:ajax render="goToButton">
</h:commandButton>

NOT to miss about using AJAX with JSF:

Community
  • 1
  • 1
Rami
  • 328
  • 1
  • 13
  • When I use your solution, I get a NullPointerException. I made some changes in my code according to what @Vasil suggestted me and I don't get a NullPointerException but the `goToButton` remains always disabled, even if I change the property disabled to true. I wrote the changes I made and changed the question above.. – Luciane Mar 11 '14 at 14:55
  • NullPointerException on what ? Possibly your problem is caused by using `c:forEach` (See: http://stackoverflow.com/a/3343681/696299) the constructor is getting called multiple times which causes the `disabled` attribute to be always set to true. Otherwise, does your `authorTable` render well ? – Rami Mar 11 '14 at 15:33
0

change h:commandButton to a4j:commandButton and add render attribute:

<a4j:commandButton id="searchButton" value="Search" 
    action="#{insertAuthControllerPt1.searchAuthor()}"
    render="goToButton" />
Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
  • I used your solution and added the table that shows the search results at `render`. But the `goToButton` still remains always disabled. I wrote those changes in my code and changed the question above.. – Luciane Mar 11 '14 at 14:54
  • After update question I propose to check your Java code and if it correct try wrap goToButton with `a4:region`. – Vasil Lukach Mar 11 '14 at 18:35