我正在寻找另一种JSF
导航方式,而不是在faces-config.xml
.
目前我正在使用faces-config.xml
导航。我想把它清理干净。
请建议所有其他方式,以便我可以使用任何适合我需要的方式。
对于简单的页面到页面导航(不提交任何内容),您应该使用<h:outputLink>
而不是<h:commandLink>
.
所以,而不是
<h:form>
<h:commandLink value="Page 1" action="page1" />
<h:commandLink value="Page 2" action="page2" />
<h:commandLink value="Page 3" action="page3" />
</h:form>
还有那些导航箱
<navigation-rule>
<navigation-case>
<from-outcome>page1</from-outcome>
<to-view-id>page1.jsf</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>page2</from-outcome>
<to-view-id>page2.jsf</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>page3</from-outcome>
<to-view-id>page3.jsf</to-view-id>
</navigation-case>
</navigation-rule>
你应该使用
<h:outputLink value="page1.jsf">Page 1</h:outputLink>
<h:outputLink value="page2.jsf">Page 2</h:outputLink>
<h:outputLink value="page3.jsf">Page 3</h:outputLink>
对于真实的表单提交,您应该重写操作方法以返回void
或null
代替结果。所以,而不是
<h:form>
<h:inputText value="#{bean.query}" />
<h:commandButton value="Search" action="#{bean.search}" />
</h:form>
和
public String search() {
results = searchService.find(query);
return "results";
}
在一页上和
<h:dataTable value="#{bean.results}" var="result">
...
</h:dataTable>
在其他页面和这个导航案例
<navigation-rule>
<from-view-id>search.jsf</from-view-id>
<navigation-case>
<from-outcome>results</from-outcome>
<to-view-id>results.jsf</to-view-id>
</navigation-case>
</navigation-rule>
你应该使用
<h:form rendered="#{empty bean.results}">
<h:inputText value="#{bean.query}" />
<h:commandButton value="Search" action="#{bean.search}" />
</h:form>
<h:dataTable value="#{bean.results}" var="result" rendered="#{not empty bean.results}">
...
</h:dataTable>
和
public void search() {
results = searchService.find(query);
}
如有必要,您可以包含页面片段<jsp:include>
。
<h:outputLink value="login.xhtml" >
Login page
</h:outputLink>
<a href="login.xhtml">
Login page
</a>
有关更多信息,请参阅此 URL:-
您可以将导航操作的返回值设置为您要转到的页面的名称(例如return "page2";
切换到page2.jsf)。但据我所知,这个特性在 JSF 2.0 中首先实现了。