Forums de discussion

Can't get a session bean through ValueExpression

Denis Korzh, modifié il y a 13 années.

Can't get a session bean through ValueExpression

New Member Publications: 9 Date d'inscription: 30/03/11 Publications récentes
I'm trying to get a session bean in another session bean:

	
        String elExpression = "#{filter}";
        ELContext elContext = facesContext.getELContext();
        ValueExpression valueExpression = facesContext.getApplication().getExpressionFactory().
                createValueExpression(elContext, elExpression, Filter.class);
        
        Filter filter = (Filter) valueExpression.getValue(elContext);


But instead the bean in current state, it returns default object (i.e. the object is not null but is empty). Also I can't set bean's values.
thumbnail
Neil Griffin, modifié il y a 13 années.

RE: Can't get a session bean through ValueExpression

Liferay Legend Publications: 2655 Date d'inscription: 27/07/05 Publications récentes
I normally do it like this:


public Object resolveExpression(String elExpression) {
FacesContext facesContext = FacesContext.getCurrentInstance();

return facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, elExpression);
}

And would pass "filter" as the elExpression and not "#{filter}"
Denis Korzh, modifié il y a 13 années.

RE: Can't get a session bean through ValueExpression

New Member Publications: 9 Date d'inscription: 30/03/11 Publications récentes
Your method returns the same object (i.e. empty bean).
I found out result is the same by using ManagedProperty annotation.
However I can get access to a request scoped bean from a session scoped bean.
thumbnail
Neil Griffin, modifié il y a 13 années.

RE: Can't get a session bean through ValueExpression

Liferay Legend Publications: 2655 Date d'inscription: 27/07/05 Publications récentes
I just tried it with the following XHTML markup and Java code and I got the desired output:

RequestBean.submit() sessionBean1=org.portletfaces.example.bean.SessionBean1@c76efc
RequestBean.submit() calling sessionBean1.doSomething()
SessionBean1.doSomething() - sessionBean1.getFoo()=1234
SessionBean1.doSomething() - sessionBean2.getBar()=5678


<h:commandbutton action="#{requestBean.submit}" value="Submit" />


@ManagedBean
@RequestScoped
public class RequestBean {

	@ManagedProperty(value="#{sessionBean1}")
	private SessionBean1 sessionBean1;

	public void submit() {
		System.err.println("RequestBean.submit() sessionBean1=" + sessionBean1);
		System.err.println("RequestBean.submit() calling sessionBean1.doSomething()");
		sessionBean1.doSomething();
	}

	public void setSessionBean1(SessionBean1 sessionBean1) {
		this.sessionBean1 = sessionBean1;
	}
	
}



@ManagedBean
@SessionScoped
public class SessionBean1 implements Serializable {

	private static final long serialVersionUID = 5416902450653893043L;

	@ManagedProperty(value="#{sessionBean2}")
	private SessionBean2 sessionBean2;
	
	public void setSessionBean2(SessionBean2 sessionBean2) {
		this.sessionBean2 = sessionBean2;
	}

	private String foo = "1234";

	public String getFoo() {
		return foo;
	}

	public void setFoo(String foo) {
		this.foo = foo;
	}
	
	public void doSomething() {
		System.err.println("SessionBean1.doSomething() - sessionBean1.getFoo()=" + getFoo());
		System.err.println("SessionBean1.doSomething() - sessionBean2.getBar()=" + sessionBean2.getBar());
	}
}



@ManagedBean
@SessionScoped
public class SessionBean2 implements Serializable {

	private static final long serialVersionUID = 4063979438316544187L;

	private String bar = "5678";

	public void setBar(String bar) {
		this.bar = bar;
	}

	public String getBar() {
		return bar;
	}
	
}
Denis Korzh, modifié il y a 13 années.

RE: Can't get a session bean through ValueExpression

New Member Publications: 9 Date d'inscription: 30/03/11 Publications récentes
In your example the issue does not tracked, because you see another object in initial state (i.e. real session bean keeps initial state and received as managed property bean keeps initial state too).
I've prepared test portlets: 'first' portlet contains button to increment firstBean's value and 'second' portlet displays received as managed property firstBean's value.
I'm using tomcat bundle 6.0.6.
thumbnail
Neil Griffin, modifié il y a 13 années.

RE: Can't get a session bean through ValueExpression

Liferay Legend Publications: 2655 Date d'inscription: 27/07/05 Publications récentes
Oh I think I see the problem. You're trying to share data between two portlets using JSF2 @Session Scope. That won't work because the JSR 329 spec requires that the session scope be kept in javax.portlet.PortletSession.PORTLET_SCOPE which keeps a wall between portlets. In order to have shared session data, you have to use javax.portlet.PortletSession.APPLICATION_SCOPE. There's an example of how to do that here:
https://github.com/liferay/liferay-faces/blob/master/demos/bridge/icefaces3-ipc-ajax-push-portlet/src/main/java/com/liferay/faces/demos/util/PortletSessionUtil.java

Unfortunately we don't have an annotation or a custom JSF2 scope yet in the bridge to do this in a nice JSF2 way. So instead you'll have to do something like PortletSessionUtil.