0

JSF page doesn't show anything; I add the JSF framework to the lib folder but no HTML form or result when I run (submit) the page on server.

The IDE I'm using is: Eclipse.

Here my java code :

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean
@ApplicationScoped
public class Counter {

    public Counter() {}

    private int value = 0;

    public String CounterValue() {
        setValue(getValue() + 1);
        return "counter";
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

}

Here my XHTML code :

<!DOCTYPE html>
<html 
    lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
>

    <h:head>
        <title>Hello world - form</title>
    </h:head>

    <h:body>

        <h:form>
            Counter Value  : #{counter.value}

            <h:commandButton value="submit" action="#{counter.counterValue()}"/>
        </h:form>

    </h:body>

</html>

And this is the result :

enter image description here

prasad_
  • 12,755
  • 2
  • 24
  • 36
Ma7
  • 244
  • 4
  • 18
  • Yes i do it but no results – Ma7 Jan 21 '19 at 09:42
  • Have you configured FacesServlet? – vivekdubey Jan 21 '19 at 09:50
  • I just add the jar file on the lib folder and add code as I mentioned before on my post ... what is mean "configured FacesServlet" – Ma7 Jan 21 '19 at 09:53
  • @vivekdubey: not needed anymore with jsf 2.3 and up which thisp does not seem to be, but when starting from scratch, alwayd use latest versions – Kukeltje Jan 21 '19 at 10:08
  • And with jsf 2.1 and up you'd better use cdi managed beans instead of jsf managed ones – Kukeltje Jan 21 '19 at 10:11
  • @Kukeltje I saw few link you can do http://javaonlineguide.net/2015/06/jsf-2-2-hello-world-tutorial-with-example-basic-concepts.html – vivekdubey Jan 21 '19 at 10:23
  • One of the issues with your bean's code is the `CounterValue()` method. And, you are referring it as `counterValue()` in the XHTML. Both the names must be same, preferably `counterValue()`; this is also convention in Java for naming the methods in camelCase. – prasad_ Jan 21 '19 at 10:36
  • @prasad_ yes is a java convention to use camelCase ... but I read when we work with JSF framework is better to use counterValue() instead of CounterValue() when we call methods or variables ... This is true? – Ma7 Jan 21 '19 at 10:41
  • @vivekdubkey: https://stackoverflow.com/tags/jsf/info is a better place to start or https://jsf.zeef.com – Kukeltje Jan 21 '19 at 10:54
  • @Ma7 What I wanted to point out is that the method names in the code `#{counter.counterValue()}` and in the `Counter.java` class must be same. Remaining comment is about the Java naming conventions. – prasad_ Jan 21 '19 at 11:29

1 Answers1

0

You need to configure FacesServlet in web.xml. Just go through configure FacesServlet configuration part in web.xml and let me know if still you are facing issue. You can go through http://javaonlineguide.net/2015/06/jsf-2-2-hello-world-tutorial-with-example-basic-concepts.html

vivekdubey
  • 484
  • 2
  • 7
  • Better to vote for the duplicate instead of amswering. This is all in the duplicate and it prevents fragmentation in stackoverflow – Kukeltje Jan 21 '19 at 10:55