Darth_Sygnious
Darth_Sygnious

Reputation: 558

JSF does not update variable

I am implementing a web page in Java Server Faces (JSF), and I have a problem. First, I will present my code, showing only the relevant code for this question.

From the ProblemDomainModel, there is the class User:

public class User implements java.io.Serializable{
    private String name;

    public String getName(){
        return name;
    }

    public void setName(String name){
         this.name = name;
    }
}

Now for the UserBean class:

import javax.faces.bean.SessionScoped;
import javax.inject.Named;    

@Named("UserB")
@SessionScoped
public class UserBean implements java.io.Serializable{
    private User singleUser = new User();

    public UserBean(){}

    public String logIn() throws Exception{        
        singleUser = Loader.loadSingleUserOnID(1);
        return "mainPage";
    }

    public User getSingleUser{
        return singleUser;
    }

    public setSingleUser(User singleUser){
        this.singleUser = singleUser;
    }
}

And now for the two xhtml-pages I have made so far:

index.xhtml:

<?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">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <p>Weclome to the Web Page. Press "Log In" to log in.</p>
            <p><h:commandButton value = "Log In" action = "#{UserB.logIn}"/></p>
        </h:form>
    </h:body>
</html>

mainPage.xhtml

<?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">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            Welcome back, <h:outputText value="#{UserB.singleUser.name}"/>.
        </h:form>
    </h:body>
</html>

When the application starts up, a web page with the welcome text and the "Log In"-button is visible. When pressing that button, the logIn()-method in UserBean is called, and singleUser is updated. The static method Loader.loadSingleUserOnID() loads a user from a database based on ID-number from a database. The method is tested and it works, so I will not detail it here. When running in debug mode, I also see that the object singleUser indeed gets updated, with change in it's name-variable.

When the user hits the button, he/she is supposed to get to the next page, saying "Welcome back, John Doe." ("John Doe" being the name-String). Instead, this is seen: "Welcome back, ."

I have also done some tests with other strings inside the UserBean. Let's say that I use a string called testString (with getTestString() and setTestString(String testString), with initial value "THIS IS A HOAX" and the logIn() method is supposed to changed this to "Elvis has left the building!". If I replace the original output with this: "Welcome back, , what should be printed out is "Welcome back, Elvis has left the building!.", instead I get "Welcome back, THIS IS A HOAX."

As I said, when running in debug mode, and reading the value on the object, the value changes. But somehow the old value remains in the system, since it is printed out on the webpage. My main question is: What am I doing wrong? (Note: I also have a 2nd question at bottom of this page)

I will also add the contents of beans.xml, faces-config.xml and web.xml, in case you see something there that I have done wrong:

beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

faces-config.xml:

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

    <navigation-rule>
        <navigation-case>
            <from-outcome>index</from-outcome>
            <to-view-id>/index.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>

    <navigation-rule>
        <navigation-case>
            <from-outcome>mainPage</from-outcome>
            <to-view-id>/mainPage.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>

</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

So again, I ask: What am I doing wrong? How can I ensure that the variable stays changed, so that it is presented on the web page?

I also have a secondary question: When do I use @Inject, and when do I not?

Thank you.

Upvotes: 2

Views: 1778

Answers (1)

Masudul
Masudul

Reputation: 21961

In UserBean class you used wrong @SessionScoped annotaion with CDI @Named. Use

 import javax.enterprise.context.SessionScoped;

insead of

 import javax.faces.bean.SessionScoped;

Upvotes: 1

Related Questions