0

im trying to learn JSF, but something i lost, cuz this button is not showing the message in eclipse console

the JAVA BEAN:

package beans;

public class protocoloBean {


    public void incluirProtocolo() {
        System.out.println("MSG");
    }

}

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

and the faces-config:

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

what im doing wrong ? or i lost :(

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2582318
  • 1,607
  • 5
  • 29
  • 47

2 Answers2

1

Your JSF code is wrong. You're trying to fire an action using <h:button> when you need/want to use <h:commandButton>. <h:button> is intended for navigation purposes only. See here for a difference between them: Difference between h:button and h:commandButton

You should update your code to:

<h:body>
    <ui:remove>
        <h:button value="Protocolar"
            action="#{protocoloBean.incluirProtocolo()}"></h:button>
    </ui:remove>
    <!--
        Note that h:commandButton MUST ALWAYS be inside a h:form
        Otherwise, the action won't fire
     -->
    <h:form>
        <h:commandButton value="Protocolar"
            action="#{protocoloBean.incluirProtocolo}" />
    </h:form>
</h:body>

After updating your code, the log message will be printed as expected.


Since you're learning JSF 2.2, I would suggest start using JSF 2 features like barely using the faces-config.xml file for managed bean definitions. You could improve your code to this:

@ManagedBean
@SessionScoped
public class ProtocoloBean {

    public void incluirProtocolo() {
        System.out.println("MSG");
    }
}

And your faces-config.xml file*:

<?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">
</faces-config>

* Yes, it is empty :).

Since you're new to JSF, I would recommend start declaring your beans as @RequestScoped or @ViewScoped instead of @SessionScoped. You cn read more info about this here: How to choose the right bean scope?

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

two mistakes in your code:

  1. the first character of your class name should be Captialized. You should use "ProtocoloBean" insteads of "protocoloBean"

  2. Action attribute is used for redirect page, the method type must be String instead of void. If you just want to execute some code, then you should use "actionListener" rather than "action", in this case your method return type can be void, but make sure (ActionEvent action) is defined as your method input argument

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
tanghao
  • 4,073
  • 2
  • 13
  • 17
  • The main problem is that OP uses the wrong JSF component: ``. It should be ``. Also, about your sentence: *you should use "actionListener" rather than "action", refer to [here](http://stackoverflow.com/q/3909267/1065197) to understand the difference between them. – Luiggi Mendoza Feb 02 '14 at 02:23
  • Thanks for your link.. That really gives me a better understanding between action and actionListener – tanghao Feb 02 '14 at 03:26