0
votes

I'm trying to write a simple login form with jsf and CDI. the problem is when I inject my SessionScoped bean it doesn't work as I expect. This is my bean

@Named
@SessionScoped
public class LoginInfo implements Serializable {
    private String uname;
    private String pass;
    private String pagename;
    private int count;

    public LoginInfo() {
    }

    public void increment() {
        count++;
    }
}

And this is my controller :

@Named
@RequestScoped
public class LoginPageMg {
    @Inject
    LoginInfo lo;

    public LoginPageMg() {
    }


    public void login() {
        lo.increment();
        lo.setPagename("aaa");
        int x = 8;
    }
}

And a simple Jsf form which calls login function and shows counter field of LoginInfo class.

<h:form prependId="false" id="mainform" styleClass="login-box">
                        <p:inputText value="#{loginPageMg.uname}"/>
                        <p:password value="#{loginPageMg.pass}"/>

                        <h:outputLabel id="counter" value="#{loginInfo.count}"></h:outputLabel>

                        <p:commandButton update="counter"
                                         action="#{loginPageMg.login}"
                                         value="login"></p:commandButton>
</h:form>

By clicking login button and debugging variables I can see "lo" is something like this :

lo = {LoginInfo$Proxy$_$$_WeldClientProxy@16688}"com.mg.LoginInfo@703ec5d5"

On line int x=8 I can see "lo" variables didn't changed at all but in my jsf page I can see the counter increases every time I press login button and bean holds the value after refreshing the page.

  1. What is WeldClientProxy?
  2. Why there are two different instance of a SessionScoped bean? Is that normal or I'm doing something wrong?
  3. How can I inject the same instance that jsf does?

I'm using Wildfly 15 Jsf 2.3.4 CDI 1.1

1
You are probably mixing JSF & CDI annotations. What exactly is the package of @SessionScoped and @RequestScoped you are using?Nikos Paraskevopoulos
@NikosParaskevopoulos no I'm using javax.enterprise.context packagearmin momeni

1 Answers

2
votes

I'm using Wildfly 15 Jsf 2.3.4 CDI 1.1

WildFly 15 means EE 8 compatilibity, hence it comes with CDI 2.0.

Now to answer as much as I can from your questions:

What is WeldClientProxy?

Weld is a CDI reference implementation used by majority of EE servers out there, including WildFly. WeldClientProxy is an internal construct it uses for injection of normal scoped beans (pretty much all except @Dependent). It is not an actual instance of the bean, you can think of it as an "wrapper" that knows how to retrieve actual contextual instance and delegates calls to it. This way you can reuse the proxy instance while still pointing to the correct instance under the hood (hence the reference doesn't need to change).

Why there are two different instance of a SessionScoped bean? Is that normal or I'm doing something wrong?

There is only one, the client proxy isn't an actual instance. You basically verified that by seeing the count increase every time and keeping state between requests.

How can I inject the same instance that jsf does?

JSF doesn't actually inject anything, instead it uses expression language to find the bean based on its name. CDI allows you to define this name via @Named. JSF then gets to use the very same bean you are injecting, again via a proxy.