Search code examples
javajsfrichfacesseam

rich suggestions - why input is null? (seam framework)


I'm trying to build a rich suggestions and i do not understand WHY the input value is null... I mean, why inputText value is not taken when i enter something.

The .xhtml code:

<h:inputText value="#{suggestion.input}" id="text">
</h:inputText>
<rich:suggestionbox id="suggestionBoxId" for="text" tokens=",[]"
                    suggestionAction="#{suggestion.getSimilarSpacePaths()}" var="result"
                    fetchValue="#{result.path}"
                    first="0"
                    minChars="2"
                    nothingLabel="No similar space paths found"
                    columnClasses="center"
        >
    <h:column>
        <h:outputText value="#{result.path}" style="font-style:italic"/>
    </h:column>
</rich:suggestionbox>

and action class:

@Name("suggestion")
@Scope(ScopeType.CONVERSATION)
public class Suggestion {
@In
protected EntityManager entityManager;

private String input;

public String getInput() {
    return input;
}

public void setInput(final String input) {
    this.input = input;
}

public List<Space> getSimilarSpacePaths() {
    List<Space> suggestionsList = new ArrayList<Space>();
    if (!StringUtils.isEmpty(input) && !input.equals("/")) {
        final Query query = entityManager.createNamedQuery("SpaceByPathLike");
        query.setParameter("path", input + '%');
        suggestionsList = (List<Space>) query.getResultList();
    }
    return suggestionsList;
}

}

So, input beeing null, suggestionList is always empty... Why input's value is not posted?


Solution

  • Well, The key is to use a method with parameter for the rich suggestion ! That parameter is actually what the user types in that inputText.... So, instead of the above class it should be:

    @Name("suggestion")
    public class Suggestion {
    
    
    @In
     protected EntityManager entityManager;
    
     public List<String> getSimilarSpacePaths(final Object input) {
      //prepare the list with the strings
            //...
         return suggestionsList;
         }
        }
    

    and the .xhtml:

    <h:inputText id="text" size="80" value="#{accountHome.instance.creationPath}">
    </h:inputText>
    <rich:suggestionbox id="suggestionBoxId" for="text" tokens=",[]"
         suggestionAction="#{suggestion.getSimilarSpacePaths}" var="result"
         fetchValue="#{result}"
         first="0"
         minChars="2"
         nothingLabel="No similar space paths found"
         columnClasses="center"
         width="350"
      >
     <h:column>
      <h:outputText value="#{result}" style="font-style:italic"/>
     </h:column>
     <a:support ajaxSingle="true" event="onselect">
      <f:setPropertyActionListener value="#{result}" target="#{accountHome.instance.creationPath}"/>
     </a:support>
    </rich:suggestionbox>
    

    Maybe will be usefull for others :)