#{bean.function} 和 #{bean.function()} 有什么区别? [英] What is difference between #{bean.function} and #{bean.function()}?

查看:21
本文介绍了#{bean.function} 和 #{bean.function()} 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JSF 的新手.我想知道 JSF/导航规则的一点.我有四个页面,索引、p1、p2、p3.当我尝试导航到带有 action="#{bean.gotoP1 的页面时()}",它给出了这样的错误;

i am new in JSF.I wonder one point at JSF/navigation rules.i have four pages, index,p1,p2,p3.When i am trying to navigate to a page with action="#{bean.gotoP1()}", it is giving error like that ;

无法为操作 '#{bean.gotoP1()}' 和结果为 'success' 找到匹配的带有 from-view-id '/index.xhtml' 的导航案例"

"Unable to find matching navigation case with from-view-id '/index.xhtml' for action '#{bean.gotoP1()}' with outcome 'success'"

我的问题很简单;为什么我不能使用 #{bean.gotoP1()} 导航,我必须删除括号 #{bean.gotoP1} ?

My question is simple; why can not I navigate with #{bean.gotoP1()} , and i have to remove parenthesis , #{bean.gotoP1} ?

我的代码如下;

index.xhtml

index.xhtml

<h:body>    
    <h:form>
        <h:commandButton action="#{mybean.gotoP1()}" value="P1"/>
        <h:commandButton action="#{mybean.gotoP2()}" value="P2"/>
        <h:commandButton action="#{mybean.gotoP3()}" value="P3"/>
    </h:form>
</h:body>

mybean.java

mybean.java

@ManagedBean
@RequestScoped
public class Mybean implements Serializable{

    private static final long serialVersionUID=1L;

    public Mybean() {
    }

    public String gotoP1(){
        return "success";
    }

    public String gotoP2(){
        return "success";
    }

    public String gotoP3(){
        return "positive";
    }
}

faces-config.xml

faces-config.xml

<navigation-rule>
    <from-view-id>/index.xhtml</from-view-id>

    <navigation-case>
        <from-action>#{mybean.gotoP1}</from-action>
        <from-outcome>success</from-outcome>
        <to-view-id>/p1.xhtml</to-view-id>
    </navigation-case>

    <navigation-case>
        <from-action>#{mybean.gotoP2}</from-action>
        <from-outcome>success</from-outcome>
        <to-view-id>/p2.xhtml</to-view-id>
    </navigation-case>

    <navigation-case>
        <from-action>#{mybean.gotoP3}</from-action>
        <from-outcome>positive</from-outcome>
        <to-view-id>/p3.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

谢谢....

推荐答案

我的问题很简单;为什么我不能使用 #{bean.gotoP1()} 导航,我必须删除括号 #{bean.gotoP1} ?

因为 EL 语法与导航案例不匹配.您将 #{bean.gotoP1} 而不是 #{bean.gotoP1()} 定义为导航案例中的 from-action.就这么简单.

Because the EL syntax doesn't match with the navigation case. You defined #{bean.gotoP1} instead of #{bean.gotoP1()} as from-action in navigation case. Simple as that.

那些无参数的括号实际上是不必要的.自从引入 EL 2.2 以来,它们开始在 JSF 页面上传播,因为平均 EL 2.2 感知 IDE 认为比它更聪明,并且不必要地自动完成带括号的方法表达式等等,使 JSF 初学者认为它们是实际需要.我什至看到代码片段来自实际使用 #{bean.getProperty()} 而不是 #{bean.property} 来输出属性的初学者当在输入组件中使用时,稍后会以 javax.el.PropertyNotWritableException 失败.

Those argumentless parentheses are actually unnecessary. They started to spread over JSF pages since introduction of EL 2.2, because the average EL 2.2 aware IDE thinks to be smarter than it is and unnecessarily auto-completes the method expressions with parentheses and all, confusingly making the JSF starter to think that they are actually required. I've even seen code snippets coming along from starters who actually used #{bean.getProperty()} instead of #{bean.property} to output a property, which would then later fail with a javax.el.PropertyNotWritableException when used in an input component.

去掉那些没有参数的括号.在 JSF 中这种语法是必需的并且是正常的",这是不正确的.此外,导航规则非常符合 JSF 1.x 风格.此外,使用 POST 请求执行导航非常符合 JSF 1.x-ish.也许你只是在学习和玩耍.没关系,但要了解正确的方法和一些历史,请仔细阅读以下链接:

Just leave out those argumentless parentheses. It's not true that this syntax is required and "normal" in JSF. Moreover, navigation rules are very JSF 1.x-ish. Also, performing navigation using POST requests is very JSF 1.x-ish. Maybe you're just learning and playing around. That's OK, but to learn about the right ways and a bit of history, carefully read below links:

最后但并非最不重要的是,我们的 JSF wiki 页面是一个很好的起点.

Last but not least, our JSF wiki page is a great place to start.

这篇关于#{bean.function} 和 #{bean.function()} 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆