Is it actually possible to pass any data between managed components in JSF? If yes, how to achieve this?
Could anyone provide any sample?
Is it actually possible to pass any data between managed components in JSF? If yes, how to achieve this?
Could anyone provide any sample?
There are several ways. If the managed beans are related to each other, cleanest way would be injection. There are different ways depending on JSF version and whether CDI is available.
Just use @Inject
.
@Named
@SessionScoped
public class Bean1 {
// ...
}
@Named
@RequestScoped
public class Bean2 {
@Inject
private Bean1 bean1; // No getter/setter needed.
}
Other way around can also, the scope doesn t matter because CDI injects under the covers a proxy.
Use @ManagedProperty
.
@ManagedBean
@SessionScoped
public class Bean1 {
// ...
}
@ManagedBean
@RequestScoped
public class Bean2 {
@ManagedProperty("#{bean1}")
private Bean1 bean1; // Getter/setter required.
}
Other way around is not possible in this specific example because JSF injects the physical instance and not a proxy instance. You can only inject a bean of the same or broader scope into a bean of a particular scope.
Use <managed-property>
in faces-config.xml
.
public class Bean1 {
// ...
}
public class Bean2 {
private Bean1 bean1; // Getter/setter required.
}
<managed-bean>
<managed-bean-name>bean1</managed-bean-name>
<managed-bean-class>com.example.Bean1</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>bean2</managed-bean-name>
<managed-bean-class>com.example.Bean2</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>bean1</property-name>
<value>#{bean1}</value>
</managed-property>
</managed-bean>
To add to BalusC s answer, if you are using a dependency-injection framework (spring, guice, etc.), or if using JSF 2.0, you can have one managed bean set into the other using just:
@Inject
private Bean2 bean2;
(or the appropriate annotation based on your DI framework)
How can I pass parameters to a stand alone qooxdoo applicatoin? Thanks in advance :) update: I was thinking of passing URL parameters; something like GET/POST params when an html FORM is submitted. ...
I have the following problem with a program which I wrote in Visual C++ and I hope that anyone can help me please: typedef struct spielfeld { int ** Matrix; int height; int width; Walker walker;...
First of all, I want to know if this is possible: let s say I have an unsigned long which contains some abritrary unsigned shorts, which may or may not be in the number. For example: unsigned short ...
I have a MVC view in which I have to pass a string variable to JavaScript, but that string variable has single quotes in it ( ). I am trying to do something like this <a onclick="JavaScript:...
i have a c-application which should load a shared object at runtime and call an arbitrary function of the shared object. The shared object was build by the user and gave me the signature of his ...
I couldn t solve that mistery question in SQL SERVER. Here an example that I tried to do and it didn t work. DECLARE @Total int; SET @Total = (SELECT COUNT(*)-10 FROM MYTABLE) SELECT TOP @Total ...
I want to use jQuery s .get method to send an ajax call to the server. I am using this line: $.get("InfoRetrieve", { },addContent(data)); As you can see I want to call a function call addContent ...
Here is my scenario: i have a parent web page, in my javascript code I open a new window. This new window is a server-side aspx page. This child page needs to save some data in the database, and after ...