JSF - 更改动作啊:commandButton(2.0)

mar*_*zzz 3 jsf jsf-2

我是JSF 2.0的新手.在最后一个版本中,我理解如果我想要更改关于"发送给客户端的内容"的规则,我只需要配置faces-config.xml.

现在,在2.0版本上,如何管理Action?例如,如果我在index.xhtml上有这个

<h:commandButton id="submit" value="submit" action="response" />
Run Code Online (Sandbox Code Playgroud)

我需要调用一个名为response.html(不是xhtml)的页面,或者将该页面放入/folder/response.html或其他东西?怎么办?我知道JSF 2.0对这些事情非常灵活(href链接的概念被打败).所以我认为我可以用其他方法来管理这个,对吧?

Bal*_*usC 6

action可以点两件事情:

  1. 方法表达式action="#{bean.methodname}",其方法如下所示:

    @ManagedBean
    @RequestScoped
    public class Bean {
        public String methodname() {
            // Do some business task here.
            return "response";
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    执行该方法后,该操作将有效地最终包含该方法的返回值,如下所示:action="response".

    您还可以"动态"控制通常的Java方式:

    public String methodname() {
        if (someCondition) {
            return "somepage";
        } else {
            return "anotherpage";
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    根据条件结果,动作最终会像action="somepage"action="anotherpage"

  2. 另一个XHTML页面与当前XHTML页面位于同一文件夹中.你只需要指定文件名:action="response".

无论哪种方式,它会去这是由组成的XHTML页面outcome + ".xhtml"这里outcome是行动值(例如response.xhtml,somepage.xhtmlanotherpage.xhtml),这被认为是在同一个文件夹中包含的XHTML文件h:commandButton.

您无需为此配置任何内容faces-config.xml.以前,在JSF 1.x年龄期间,您需要为此定义<navigation-case>.

也可以看看: