JSF - Bean injection trouble

markzzz picture markzzz · Nov 30, 2010 · Viewed 8.9k times · Source

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

Answer

BalusC picture BalusC · Nov 30, 2010

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.