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 entit开发者_Go百科yManager;
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?
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 :)
I'm not sure if this is valid, but Java uses getters and setters to encapsulate it's fields with JSP. Since you're trying to use Java in an xhtml file, I presume that's what you're doing.
Notice how you #{suggestion.input}
? In JSP, that would result in a suggestion.getInput()
, which is the getter you created.
In much the same way, you would call suggestion.SimilarSpacePaths
.
JSP defaults to null or an empty string (I'm not sure which) when it can't evaluate a value properly. There are debugging plugins out there for JSP though, so you can tell this without having to compile and check on the webpage.
Did that help?
精彩评论