0

Hi I'm totally new in java. I have some basics in c#. I want to make simple task. I added two buttons and a label to form. When you click first button you should get +value in the label. When you click second button you should get -value in the label. This is my code:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>        
            <p:button  value="Add" action="#{Number.addOne()}">
            </p:button>    
            <p:button  value="Substract" action="#{Number.subOne()}" >
            </p:button>    
            <p:outputLabel id="result" value="#{Number.getNum()}"/> 
        </h:form>                
    </h:body>
</html>

Java code:

import javax.faces.bean.ManagedBean;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;

@ManagedBean
@SessionScoped
public class Number implements Serializable{

    private int num=0;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public void addOne()
    {
        this.num+=1;
    }
    public void subOne()
    {
        this.num-=1;
    }
}

Sth. is wrong in my code. In C# web forms there is onclick method but this technology is really different. Can someone show me the code which make my task properly and explain what i did wrong in my code?

Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
  • you used the wrong button. Read the PF documentation and start with some good basic jsf tutorial. And if you are new to all this, drop the `@ManagedBean` in favour of the CDI `@Named`. And you used the wrong `@SessionScoped (the cdi one) with the `@ManagedBean` (jsf). This will be automatically fixed if using @Named And use google: http://blog.mueller-bruehl.de/programming/tutorial-web-development-with-jsf-iii-basic-arithmetics/ – Kukeltje Dec 20 '15 at 20:01
  • And if you debug your **technical** problem instead of your (specific) functional problem, you'd see your technical problem is generic an described in multiple different questions – Kukeltje Dec 20 '15 at 20:09
  • I want to solve this task using primefaces buttons – Razem Ponad-kilo Dec 20 '15 at 20:12
  • 1
    Yes but please do as I stated. Read the docs, look at the showcase. PF has more 'buttons' http://stackoverflow.com/questions/26647909/pcommandbutton-or-pbutton-with-actionlistener-and-outcome and also check http://stackoverflow.com/questions/13070537/difference-between-hbutton-and-hcommandbutton – Kukeltje Dec 20 '15 at 20:15
  • Welcome to Stackoverflow. http://www.primefaces.org/showcase/ui/button/commandButton.xhtml – Mahendran Ayyarsamy Kandiar Dec 20 '15 at 20:54

0 Answers0