【问题标题】:Calling backing bean method based on URL基于 URL 调用 backing bean 方法
【发布时间】:2013-02-26 11:48:51
【问题描述】:

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

【问题讨论】:

标签: jsf-2


【解决方案1】:

您可以使用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()
    {
        ...
    }

就是这样。

【讨论】:

  • PrettyFaces 是一个很棒的工具,我们将它与 PrimeFaces 一起使用,它已经解决了许多架构问题。
【解决方案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){
    ...
}

【讨论】:

    猜你喜欢
    • 2015-12-10
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多