@ManagedProperty does not reflect changes and retains a null return value

I am trying to insert the value of a single sessioncoped bean into a viewcoped bean, but it continues to return null, here's a snippet:

import javax.faces.application.FacesMessage; import javax.faces.bean.SessionScoped; import javax.faces.bean.ManagedBean; import javax.faces.context.FacesContext; //Class for managing the current logged-in user @ManagedBean(name="user") @SessionScoped public class User implements Serializable{ private String userName; public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return this.userName; } 

And it is used in:

 @ManagedBean(name="databrowser") @ViewScoped public class dataBrowser implements Serializable { private List<UploadData> dataItems; private SelectItem[] dataTypeOptions, qualityOptions, accessOptions; private UploadData selectedData; private String filelocation; @ManagedProperty(value="#{user.userName}") private String userName; public String getUserName() { return this.userName; } 

The dataBrowser is used to populate Primefaces data, when it is called userName, is null, and I'm not sure why.

+3
source share
5 answers

I recently had a problem with nesting bean nested managed properties using @ManagedProperties too. After the injection, he never changed. I made a workaround by evaluating the EL in the getter instead of injecting it.

Try the following:

 public String getUserName() { FacesContext context = FacesContext.getCurrentInstance(); return (String) context.getApplication().evaluateExpressionGet(context,"#{user.userName}", String.class); } 

You can also try to enter the entire user bean and get the userName field in it.

+2
source

When all the setters / receivers are in place, I had the same problem ( null user reference) due to the missing empty constructor in the user class .

In the example you provided, instances of dataBrowser and user beans are created before creating the table, so the link #{dataBrowser.userName} should already find the correctly entered userName @ManagedProperty (not a @PostConstruct problem).

0
source

I ran into the same problem and accidentally discovered that it does not work if I try to use firefox (actually icedove under linux), but works well if I try to use the built-in eclipse browser.

Even if that doesn't make sense to me, have you tried it with different browsers?


michal777 answer works very well. I added the following to this:

  @ManagedProperty("#{nameBean}") private NameBean nameBean; public NameBean getNameBean() { return nameBean; } public void setNameBean(NameBean nameBean) { this.nameBean = nameBean; } public NameBean getNameBean_Workaround() { FacesContext context = FacesContext.getCurrentInstance(); return (NameBean) context.getApplication().evaluateExpressionGet(context,"#{nameBean}", NameBean.class); } 

and then:

  if (nameBean != null) { nameBean.setName("achsooo"); } else { getNameBean_Workaround().setName("achsooo2222"); } 

Now, in the eclipse browser, "achsooo" is installed, and in icedove, "achsooo2222" is installed.

0
source

#{user.userName} interpreted by JSF as getUser().getUserName()

So it’s best to have a @ManagedProperty type User , with its getter / setter getUser / setUser . With this, you can access the username #{user.userName} .

0
source

I had this problem and the problem was actually twofold. (Note that @ManagedProperty will only work in the @ManagedBean class, and if the @ManagedProperty class has the same or smaller scope (application, session, presentation, request, etc.). Here's how I fixed it: / p>


Problem 1: JSF is stupid and does not handle @ManagedProperty proper injection in abstract classes.

Decision:

  • Make every class that uses @ManagedProperty annotate with @ManagedBean .
  • Make each abstract class that uses the property not be annotated with @ManagedProperty , but instead provide only the abstract getter and setter methods that the non- @ManagedProperty classes will override.
  • Use the gett abstract method of the class instead of the @ManagedProperty itself in abstract classes.

Problem 2: JSF is stupid and does not handle @ManagedProperty correctly inject in @ManagedBean classes not created by JSF (i.e. you create these classes yourself using new ).

Solution Options:

  • Let JSF create a class that uses @ManagedProperty .
  • Use the following code:

 MyClass example = Utils.getELValue("EL Expression Goes Here", MyClass.class); public static <T> T getELValue(final String elName, final Class<T> clazz) { FacesContext fc = FacesContext.getCurrentInstance(); return (T) fc.getApplication().getELResolver().getValue(fc.getELContext(), null, elName); // Potential (untested) alternative: // ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getSession().getAttribute("") } 
0
source

Source: https://habr.com/ru/post/1443939/


All Articles