我有一个带有inputtext
的页面,我在其中键入要搜索的作者的名称,当我单击a4j:commandButton
searchButton
时,我想启用h:commandButton
GoToBUtton
,如果搜索没有返回作者并且禁用该按钮,如果搜索返回作者。首先,GoToButton
应该被禁用,这是默认行为。
问题是,goToButton
总是被禁用,它永远不会改变。怎么了?
<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>
insertauthcontrollert1 .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();
}
}
首先,非常不鼓励混合使用JSTL标记和Facelets标记(参见:JSTL在JSF2 Facelets中…有意义吗?)
那么您将使用f:ajax
标记。在动作执行后,您将基本上使用ajax调用来呈现goToButton
<h:commandButton id="searchButton" value="Search" action="#{insertAuthControllerPt1.searchAuthor()}">
<f:ajax render="goToButton">
</h:commandButton>
不要错过在JSF中使用AJAX:
- Java教程:发送Ajax请求Max Katz:学习JSF2: Ajax在JSF -使用
- Lincoln Baxter:渲染表单外的组件
f:ajax
标签将h:commandButton
更改为a4j:commandButton
并添加render
属性:
<a4j:commandButton id="searchButton" value="Search"
action="#{insertAuthControllerPt1.searchAuthor()}"
render="goToButton" />