Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSF-更改h:commandButton的操作(2.0)_Jsf_Jsf 2 - Fatal编程技术网

JSF-更改h:commandButton的操作(2.0)

JSF-更改h:commandButton的操作(2.0),jsf,jsf-2,Jsf,Jsf 2,我是JSF2.0的新手。在上一个版本中,我理解如果我想要更改关于“向客户端发送什么”的规则,我只需要配置faces-config.xml 现在,在2.0版上,如何管理操作? 例如,如果我在index.xhtml上有这个 <h:commandButton id="submit" value="submit" action="response" /> 我需要调用一个名为response.html(不是xhtml)的页面,或者调用放在/folder/response.html中的页面

我是JSF2.0的新手。在上一个版本中,我理解如果我想要更改关于“向客户端发送什么”的规则,我只需要配置faces-config.xml

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

<h:commandButton id="submit" value="submit" action="response" />


我需要调用一个名为response.html(不是xhtml)的页面,或者调用放在/folder/response.html中的页面,或者其他什么东西?你怎么能做到?我知道JSF2.0在这些方面非常灵活(href链接的概念已经过时)。所以我想我可以用其他方法来处理这个问题,对吗?

这个
操作可以指出两件事:

  • 方法表达式
    action=“#{bean.methodname}”
    其中方法如下所示:

    @ManagedBean
    @RequestScoped
    public class Bean {
        public String methodname() {
            // Do some business task here.
            return "response";
        }
    }
    
    执行该方法后,该操作将有效地包含该方法的返回值,如下所示:
    action=“response”

    您还可以通过通常的Java方式“动态”控制结果:

    public String methodname() {
        if (someCondition) {
            return "somepage";
        } else {
            return "anotherpage";
        }
    }
    
    根据条件结果,操作将以
    action=“somepage”
    action=“anotherpage”

  • 文件夹中的另一个XHTML页面与当前XHTML页面相同。您只需指定文件名:
    action=“response”

  • 无论哪种方式,它都会转到XHTML页面,该页面由
    output+“.XHTML”
    组成,其中
    output
    是动作值(例如
    response.XHTML
    somepage.XHTML
    anotherpage.XHTML
    )它应该与包含
    h:commandButton
    的XHTML文件位于同一文件夹中

    您不需要为此在
    faces config.xml
    中配置任何内容。以前,在JSF1.x时代,您需要为此定义

    另见:
    • -Mojarra首席开发者的博客

    谢谢你的提示。是的,在“1”的情况下,我调用一个bean上的方法。但它只返回一个变量。如果bean调用return“some value”或“other value”,如何说“return the page somepage.xhtml”?例如,如果bean返回“hello”,它必须加载helloworld.xhtml,如果返回“mate”“它必须加载byemate.xhtml。只需在action方法中使用
    if-else
    语句,即可返回不同的
    字符串
    结果。请参阅更新答案中的第二个代码块。因此,可能我不理解JSF的过程。这将从bean服务器端“返回”字符串,它不会调用somepage.xhtml或anotherpage.xhtml,对吗?否则,Web服务器将首先评估bean,然后评估整个“操作”状态:)会的。唯一的区别是返回值现在由bean方法控制。只要将
    somepage.xhtml
    另一个页面.xhtml
    放在同一个文件夹中,JSF就会根据结果值导航到其中任何一个页面。啊,好的。我总是忘记JSF的生命周期!我是从php方法学开始的,这在当时是非常疯狂的:)谢谢你,伙计