我是JSF的新手。我想知道JSF /导航规则的一点。我有四个页面,索引,p1,p2,p3。当我尝试导航到一个页面,其中action =“#{bean.gotoP1 ()}“,它给出了错误;
“无法找到匹配的导航案例与from-view-id'/ index.xhtml'进行操作'#{bean.gotoP1()}',结果'成功'”
我的问题很简单;为什么我不能用#{bean.gotoP1()}导航,我必须删除括号,#{bean.gotoP1}?
我的代码如下;
的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>
...谢谢
答案 0 :(得分:6)
我的问题很简单;为什么我不能用#{bean.gotoP1()}导航,我必须删除括号,#{bean.gotoP1}?
因为EL语法与导航案例不匹配。您在导航案例中将#{bean.gotoP1}
而不是#{bean.gotoP1()}
定义为from-action。就这么简单。
那些毫无争议的括号实际上是不必要的。自从引入EL 2.2以来,它们开始在JSF页面上传播,因为平均的EL 2.2感知IDE认为比它更聪明,并且不必要地用括号和所有这些自动完成方法表达式,令人困惑地让JSF首发者认为它们是实际需要。我甚至看到实际使用#{bean.getProperty()}
而不是#{bean.property}
的初学者输出的代码片段输出一个属性,然后在输入组件中使用后会失败并显示javax.el.PropertyNotWritableException
。
请忽略那些无争议的括号。这种语法在JSF中是必需的和“正常”的,这是不正确的。而且,导航规则非常JSF 1.x-ish。此外,使用POST请求执行导航非常JSF 1.x-ish。也许你只是在学习和玩耍。没关系,但要了解正确的方法和一些历史,请仔细阅读以下链接:
最后但同样重要的是,our JSF wiki page是一个很好的起点。