2013-02-26 60 views
4

我在JSF中遇到了一个问题。有什么方法可以根据URL调用backing bean的方法吗?在使用Struts时,我可以通过structs-config和action class来实现。当我从Struts迁移到JSF时,我正面临着这个问题。基于URL调用backing bean方法

+0

这将帮助您:http://balusc.blogspot.in/2011/09/communication-in-jsf-20.html – 757071 2013-02-26 11:50:33

回答

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() 
    { 
     ... 
    } 

这就是所有。

+0

感谢您的回复。 – 2013-02-26 12:48:28

+0

PrettyFaces是一款出色的工具,我们将它与PrimeFaces一起使用,它已经解决了许多架构问题。 – 2013-10-04 18:40:33

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){ 
    ... 
} 
+0

感谢您的回复 – 2013-02-26 14:12:45