JSF不执行bean
我正在尝试学习JSF,但是我丢失了一些东西,因为这个按钮没有在eclipse控制台中显示消息 JAVA BEAN:JSF不执行bean,jsf,Jsf,我正在尝试学习JSF,但是我丢失了一些东西,因为这个按钮没有在eclipse控制台中显示消息 JAVA BEAN: package beans; public class protocoloBean { public void incluirProtocolo() { System.out.println("MSG"); } } xhtml: <html xmlns="http://www.w3.org/1999/xhtml" xmlns:
package beans;
public class protocoloBean {
public void incluirProtocolo() {
System.out.println("MSG");
}
}
xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:button value="Protocolar"
action="#{protocoloBean.incluirProtocolo()}"></h:button>
</h:body>
</html>
以及人脸配置:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<managed-bean>
<managed-bean-name>protocoloBean</managed-bean-name>
<managed-bean-class>beans.protocoloBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<application/>
</faces-config>
原生动物
原生动物
一场
我做错了什么?或者我输了:(代码中有两个错误:
您的JSF代码错误。当您需要/想要使用
时,您正试图使用
启动一个操作,而
仅用于导航目的。请参阅此处了解它们之间的区别:
您应该将代码更新为:
更新代码后,日志消息将按预期打印
既然您正在学习JSF 2.2,我建议您开始使用JSF 2的特性,比如勉强使用faces-config.xml文件来定义托管bean。您可以在以下方面改进代码:
@ManagedBean
@SessionScoped
public class ProtocoloBean {
public void incluirProtocolo() {
System.out.println("MSG");
}
}
以及您的faces-config.xml文件*:
*是的,它是空的:)
由于您是JSF新手,我建议您开始将bean声明为
@RequestScoped
或@ViewScoped
,而不是@SessionScoped
。您可以在这里阅读更多信息:主要问题是OP使用了错误的JSF组件:
。它应该是
。另外,关于你的句子:*你应该使用“actionListener”而不是“action”,请参阅以了解它们之间的区别。谢谢你的链接。。这真的让我对action和actionListener有了更好的理解