Search code examples
jsfjakarta-eejsf-2xhtmljavabeans

After form submission , using java ee and jsf 2,2, my backing bean properties are still null


Basically , in the input.xhtml there is a form which takes a username and password and if they are equal to a specific value(doesn't matter the value) the program should print a message in the browser, but this doesn't happen. To make sure of the problem i added 2 lines of "System.out.println(...)" where i print the value of the property and what i found out is that the properties are still null even after i submit. So after i click send in the console is written "null null". Any help is appreciated!

This is the UserBean class (backing bean)

 package bean;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;



@ManagedBean
@SessionScoped
public class UserBean implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String name;
    private String password;
    private String output_message;



    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String go(){
        output_message = "";

        if(name == null && password == null){
            output_message += "Both fields cannot be empty!";
        }else if(name == "name" && password == "pass"){
            output_message += "Success!";
        }else{
            output_message += "Data is wrong!";
        }

        System.out.println(name);
        System.out.println(password);

        return output_message;
    }

    public String getOutput_message() {
        return output_message;
    }
    public void setOutput_message(String output_message) {
        this.output_message = output_message;
    }
    public String ret(String r){
        return r;
    }

}

This is the input.xhtml file, that contains the form that will submit the data to the bean. (Ignore the url to template.xhtml, it's just a parent template that has a header and a footer other than the mid-content that input.xhtml defines)

   <!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:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:cc="http://java.sun.com/jsf/composite/myComponent">

<ui:composition template="/WEB-INF/templates/template.xhtml">

    <ui:define name="content">

        <style type="text/css">
            #form1{
                position: absolute;
                top: 20%;
                left:40%;
                border: 1px solid black;
                background-color: orange;
            }
            td{
                padding-top:10px;
            }

            input[type="text"]{
                backgorund-color:blue;
            }

        </style>

        <form id="form1" method="post">
        <table columns="2">
            <tr>
                <td><span>Emri:</span></td>
                <td><input type="text" id="emri" value="#{userBean.name}"/></td>
            </tr>
            <tr>
                <td><span>Password:</span></td>
                <td><input type="password" id="pass" value="#{userBean.password}"/></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><button type="submit" onclick="#{userBean.go()}">Send</button></td>

            </tr>
        </table>
        </form>

        <p>#{user.output_message}</p>


    </ui:define>
</ui:composition>
</html>

Solution

  • I just solved this. the problem was with the input tag, the value attribute does not represent the value written in the textbox is just some kind of a default value or somekind of a pre-value. Anyway instead of input and form tags i used and which worked out fine h is a namespace with url xmlns:h="http://xmlns.jcp.org/jsf/html"