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

查看:69
本文介绍了#{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 ;

无法为结果为'success'的动作'#{bean.gotoP1()}'的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的要求.也许您只是在学习和玩耍.可以,但是要了解正确的方法和一些历史,请仔细阅读以下链接:

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:

  • Invoke direct methods or methods with arguments / variables / parameters in EL
  • Differences between action and actionListener
  • JSF implicit vs. explicit navigation
  • How to navigate in JSF? How to make URL reflect current page (and not previous one)
  • Difference between JSP EL, JSF EL and Unified EL

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

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

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

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