Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

jsf - What is difference between #{bean.function} and #{bean.function()}?

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 ;

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

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

My codes are below;

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

@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

<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>

Thanks....

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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.

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:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...