0

I want to Inject a Managed Session Bean into an Entity in JSF.

I have a requirement where I need to show different status text based on Locale selected by User on front-end. (Internationalization) for one status ID I have 4 Status text i.e. StatusText, StatusTextLang1, StatusTextLang2, StatusTextLang3.

To support this feature, I am making changes in getter method of StatusText property. Here I want to take the Locale from SessionBean. Because Its an entity, so I am getting a null sessionBean.

@Entity
@Table(name="Projektstati")
public class Projektstati implements Serializable{

@ManagedProperty("#{sessionBean}")
private SessionBean sessionBean;
public void setSessionBean(SessionBean sessionBean){this.sessionBean = sessionBean;}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Harsh Dadhich
  • 69
  • 1
  • 7
  • 3
    Seems to me like you're trying to solve a view problem with code in an entity - that is very very very bad design. Why not add a method to SessionBean itself to get the proper text? – Gimby Jul 09 '15 at 07:35
  • Agree with you, the trouble is that if I do that, I'll end up making changes to a vast level. I have 11 such entities and around 60 screens where these columns are used. What else can be a solution ? @gimby – Harsh Dadhich Jul 09 '15 at 08:25
  • You're not describing a problem to me here. 11 entities and 60 screens is nothing. – Gimby Jul 09 '15 at 08:56
  • Ok the problem is that I have 60 pages already in place where this field is used (directly while population). Now if I were to make change in SessionBean, then I will have to make changes in all 60 Pages. Whereas if I update the code of the getter method of this field, all the occurrences will get effect automatically. Only issue im getting is that I'm not able to inject sessionBean into entity, so that I can determine which language is selected from frontend. @gimby – Harsh Dadhich Jul 09 '15 at 09:32

1 Answers1

-1

I checked few options and following worked out.

FacesContext facesContext = FacesContext.getCurrentInstance();
    sessionBean = (SessionBean) facesContext.getApplication()
        .getVariableResolver().resolveVariable(facesContext, "sessionBean");

It may be not a great design but definitely a quick solution.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Harsh Dadhich
  • 69
  • 1
  • 7
  • 2
    It's not only not great (gr8 is soooo 2010), it's making a bad solution (the existing code) even worse... Others after you will most likely fail to see why this was done and curse you for it... Just take one day and refactor all. It is 'just' 11 entities and 60 screens. – Kukeltje Jul 09 '15 at 12:34
  • As Kukeltje says, this is a bad decision... Better change the 11 entities and 60 screens, than creating bad code – Emil Kaminski Jul 09 '15 at 12:59