2

As suggested by a user time ago in another question, I need to inject a bean in another bean.

So, i do the following :

@ManagedBean
@RequestScoped
public class Articles {
    private String selectedMenu;

    @ManagedProperty(value="#{user}")
    private UserManager user;

    @PostConstruct
    public void init() {
        if(selectedMenu==null || selectedMenu.trim().isEmpty()) {
            this.selectedMenu="0";
        }
    }

    // now here i should access to user.methods

    // getter and setter
}

In fact, i can't access to UserManager data. I get these error :

BROWSER malformedXML: INVALID_STATE_ERR: Dom Exception 11

SERVER LOG 30-nov-2010 15.36.58 javax.faces.component.UIViewRoot$ViewMap put AVVERTENZA: Setting non-serializable attribute value into ViewMap: (key: profileSelector, value class: model.ProfileSelector) 30-nov-2010 15.36.59 com.sun.faces.mgbean.BeanManager preProcessBean GRAVE: JSF will be unable to create managed bean articles when it is requested. The following problems where found: - Property user for managed bean articles does not exist. Check that appropriate getter and/or setter methods exist. 30-nov-2010 15.36.59 com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback visit GRAVE: com.sun.faces.mgbean.ManagedBeanCreationException: Unable to create managed bean articles. The following problems were found: - Property user for managed bean articles does not exist. Check that appropriate getter and/or setter methods exist.

If i comment @ManagedProperty(value="#{user}") and private UserManager user; than i don't see any error. So that's the problem. What am I wrong?

Cheers

markzzz
  • 47,390
  • 120
  • 299
  • 507

1 Answers1

5

malformedXML: INVALID_STATE_ERR: Dom Exception 11

This XML error indicates an error in your view (the XHTML), not in the bean. I don't understand why this error occurred, but likely you've a pretty complex component tree with several rendered attributes of which one is depending on the user property which caused a wrong state in the XML tree in the client side.

As to the server logs:

AVVERTENZA: Setting non-serializable attribute value into ViewMap: (key: profileSelector, value class: model.ProfileSelector)

This does not necessarily harm, but to fix it, you need to let the class implement java.io.Serializable.

public class ProfileSelector implements Serializable {}

This way the server will be able to transfer the instance over network and/or store the instance on harddisk instead of in memory whenever necessary (when the server reboots or is placed in a server cluster).


30-nov-2010 15.36.59 com.sun.faces.mgbean.BeanManager preProcessBean GRAVE: JSF will be unable to create managed bean articles when it is requested. The following problems where found: - Property user for managed bean articles does not exist. Check that appropriate getter and/or setter methods exist.

This is self-explaining as well. Ensure that there are proper getter/setter methods for the property user of the managed bean with name articles.

@ManagedBean
@RequestScoped
public class Articles {

    @ManagedProperty(value="#{user}")
    private UserManager user;

    public UserManager getUser() {
        return user;
    }

    public void setUser(UserManager user) {
        this.user = user;
    }

}

You can if necessary let your IDE autogenerate them. In Eclipse, check the Source section in rightclick context menu (Alt+Shift+S).


30-nov-2010 15.36.59 com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback visit GRAVE: com.sun.faces.mgbean.ManagedBeanCreationException: Unable to create managed bean articles. The following problems were found: - Property user for managed bean articles does not exist. Check that appropriate getter and/or setter methods exist.

This has the same problem cause.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So, the inject property call a getter/setter. I meant it with "write the name of the bean instance". – markzzz Nov 30 '10 at 14:52
  • I resolved 2 of these problem. The last still happen (`com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback`) – markzzz Nov 30 '10 at 14:54
  • If you are certain that the proper getter/setter methods are added, then rebuild, redeploy and restart. – BalusC Nov 30 '10 at 14:57
  • I've done it. I add the proper getter/setter to the UserManager bean (and, on postcostruct, i've write `this.user=this`, because it need to recognize the user instance when it return trought the getter), but i get the third error message. Also, now (by adding `implements Serializable`) i get some other further errors, like `Cannot serialize session attribute com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap for session 4B49A9591CAC0968C79EE88BD303533A java.io.NotSerializableException: model.ProfileSelector` – markzzz Nov 30 '10 at 15:02
  • Huh? `this.user = this`? This should have resulted in a compilation error. Are you adding the getter/setter to the `Articles` class? – BalusC Nov 30 '10 at 15:07
  • Yeah but this should be due to the fact i debugged the application. In fact now it doesnt happen. Still the last error i don't understand why happen. – markzzz Nov 30 '10 at 15:08
  • Yeah. I need to instantiate the user variable. I mean on `UserManager` bean. Also, what should it return on `public UserManager getUser() { return user; }` ? No, i didnt on Articles (why?) – markzzz Nov 30 '10 at 15:10
  • The exception is telling that you need to add a getter and setter for `user` **on the `Articles` bean**! You don't need them in the `UserManager` bean at all. I expanded the code example. – BalusC Nov 30 '10 at 15:11