我在 JSF 中遇到了一个问题。有什么方法可以根据 URL 调用 backing bean 的方法吗?使用 Struts 时,我可以通过structs-config
action 类来实现。当我从 Struts 迁移到 JSF 时,我遇到了这个问题。
问问题
3156 次
2 回答
4
您可以使用PrettyFaces,它极大地改进了 JSF 导航。使您能够使用易于理解、可添加书签的 REST url。
如果您使用 Servlet 3.0,那么您只需将 PrettyFaces jar 添加到您的 Web 应用程序,使用页面映射注释您的托管 bean,并为特定映射选择操作:
import com.ocpsoft.pretty.faces.annotation.URLAction;
import com.ocpsoft.pretty.faces.annotation.URLMapping;
import com.ocpsoft.pretty.faces.annotation.URLMappings;
@ManagedBean(name = "pageViewBean")
@URLMappings(mappings = {
@URLMapping(id = "myAction",
pattern = "/page/myAction", // URL mapped to jsf file
viewId = "/page.xhtml"), // jsf file
@URLMapping(id = "myAction2",
pattern = "/page/myAction2", // URL mapped to jsf file
viewId = "/page.xhtml")}) // jsf file
public class PageViewBean
{
@URLAction(mappingId = "myAction") // action for URL /page/myAction
public void myAction()
{
...
}
@URLAction(mappingId = "myAction2") // action for URL /page/myAction2
public void myAction2()
{
...
}
就这样。
于 2013-02-26T12:44:30.163 回答
2
您可以使用<f:event type="preRenderView" />
,每次渲染页面时都会调用它,将其放在<h:head>
标签上方
例如:
<f:event listener="#{myBean.myAction}" type="preRenderView" />
<h:head>
...
</h:head>
<h:body>
...
在你的 bean 中:
public void myAction(ComponentSystemEvent event){
...
}
于 2013-02-26T12:20:36.900 回答