Named Bean not injected into another named bean - jsf

I am trying to inject a named bean in another one.
When I try to access properties of the injected bean , it returns null which means that the injection fails.
Here is the first Bean:
#Named
#SessionScoped
public class FirstMBean {
String caracter;
..........
public String goToNext(){
String caracter=FacesContext.getCurrentInstance().getExternalContext().
getRequestParame‌​terMap().get("caracter"); this.caracter=caracter;
System.out.println("the selected caracteris"+ caracter);
return "/pages/next?faces-redirect=true"; }
}
The second named bean:
#Named
#SessionScoped
public class SecondMBean {
#Inject
FirstMBean firstMBean ;
String injectedCaracter ;
#PostConstruct
public void init() {
this.injectedCaracter = this.firstMBean.getCaracter();
System.out.println("injectedCaracter ----" + this.injectedCaracter);
}
In my .xhtml page :
<p:commandButton value="see caracter" action="#{firstMBean.goToNext}">
<f:param name="caracter" value="#{caracter}" />
</p:commandButton>
Please note that I am using tomcat server to which I added WELD to support CDI AND when I put the scope to ApplicationScoped, everything works fine.

Related

How to set a bean property of an injected CDI bean from the contains Bean?

I have a CDI Bean which is injected to another CDI bean,
Bean1Controller:
#ViewScoped
public class bean1Controller
{
#Inject
Bean2Controller bean2;
// + setter and getter
// here I initialise the injected Bean2
#PostConstruct
public void init()
{
bean2 = new Bean2Controller();
}
public void changeFlagBoolean()
{
bean2.setFlag(true);
}
}
Bean2Controller:
#ViewScoped
public class bean2Controller
{
boolean flag=false;
// + getters+setters
}
XHTML sample:
<h:commandLink style="font-size: 10px"
value="link"
action="#{bean1Controller.changeFlagBoolean()}"
target="content" />
I have a link in my XHTML Page when I click It I run the method changeFlagBoolean() of bean1Controller to set the property flag to true of the injected bean2Controller. I proceed like That but unfortunately it doesn't work.
Any suggestion or something needs to be change in the code.
Thank you
Updated Answear,
I got the solution after such research,
The problem was from the #viewScoped scope, I couldn't change the value of the flag property in the injected bean and it still always false after doing bean2.setFlag(true);
the problem if I go from view1.xhtml to view2.xhtml I have a new instance of the bean1Controller thats why I have always false as a value because it is a view Scope based.
The olution was with the Flash https://memorynotfound.com/passing-variables-in-jsf-flash-scope/ . I keep the value in a flash and set its value as ' true' and I called it in the bean2Controller so that it is available in the bean1Controller and get it by callling flag = (boolean)flash.get("flag");

After migration from JSF #ManagedBean to CDI #Named, constructor called multiple times and submitted input values always null

Edit: The comment section solved my problem! The problem was that I was using incorrect imports for the Scopes.
I have a simple JSF application (login, pull data from database, allow user to edit data). It works well, I want to update the code to use CDI (Weld), but I am having trouble.
I am following / looking at: http://docs.jboss.org/weld/reference/latest/en-US/html/example.html
Original stuff without Weld:
login.xhtml
<h:form id="inputForm">
<h:panelGrid columns="2" cellpadding="5" cellspacing="1">
<h:outputText id="nameDesc" value="Name"></h:outputText>
<h:inputText id="nameInput" value="#{login.loginName}" binding="#{name}"></h:inputText>
<h:outputText id="passwordDesc" value="Password"></h:outputText>
<h:inputSecret id="passwordInput" value="#{login.password}" binding="#{password}"></h:inputSecret>
</h:panelGrid>
<h:commandButton value="Login" action="#{login.login(name.value, password.value)}"/>
</h:form>
LoginBean.java:
#ManagedBean(name="login")
#SessionScoped
public class LoginBean implements Serializable {
private static final long serialVersionUID = 1L;
#ManagedProperty(value="#{db}")
private DatabaseBean db;
private String password;
private String loginName;
// other stuff and functions
public String getLoginName () {
return loginName;
}
public void setLoginName (String name) {
this.loginName = name;
}
public String getPassword () {
return password;
}
public void setPassword (final String password) {
this.password = password;
}
public void setDb(DatabaseBean db) {
this.db = db;
}
DatabaseBean.java:
#ManagedBean(name="db", eager=true)
#ApplicationScoped
public class DatabaseBean implements Serializable {
#PostConstruct
public void init() {
//... connect to database etc
}
}
---------What I tried to get it running with Weld (only changes from above to make it a bit shorter): --------
LoginBean.java, changed to #Named from #ManagedBean, added #Inject for DatabaseBean
#Named("login")
#SessionScoped
public class LoginBean implements Serializable {
// stuff
private #Inject DatabaseBean db;
}
DatabaseBean.java, changed to #Named from #ManagedBean:
#Named("db")
#ApplicationScoped
public class DatabaseBean implements Serializable {
}
LoginBean has a function:
public String login(String name, String password) {
System.out.println("login called"+name);
// other stuff
}
With my second implementation (the one where I try to use Weld), the print is called once: "login called", and the username is empty (I checked this with name.IsEmpty()).
I have also tried injecting it by constructor:
loginBean.java
#Inject
public LoginBean(DatabaseBean db) {
System.out.println("constructor");
this.db = db;
}
When I do this the I get lots of "constructor" prints, so it is called several times, but I don't see why - I guess this is the problem though, only one instance of LoginBean gets the input (username and password) and then lots of new ones are created for some reason. Why is that?
I use Eclipse and Tomcat8 to run it.
Thank you for reading!
managed bean constructor called multiple times
CDI may call constructor more often than expected while generating/creating enhanced subclasses/proxies. See also Field.get(obj) returns all nulls on injected CDI managed beans, while manually invoking getters return correct values. Just do not log constructor invocation, it would only confuse yourself. #PostConstruct is the only interesting method to hook on.
the print is called once: "login called", and the username is empty (I checked this with name.IsEmpty()).
As to the concrete problem of form input values being null when the action method is invoked, and thus the #SessionScoped CDI managed bean seemingly being recreated on every access, this matches the behavior of a #Dependent scoped bean. This is the default scope when no valid CDI managed bean scope can be found. See also What is the default Managed Bean Scope in a JSF 2 application?
This in turn suggests you imported #SessionScoped from the wrong package. Make sure it's from the javax.enterprise.context package and not from e.g. javax.faces.bean. JSF managed bean scopes are not recognizable as valid CDI managed bean scopes.
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
#Named("login")
#SessionScoped
public class LoginBean implements Serializable {
// ...
}

How to share data between two #ViewScoped beans?

I am trying to pass id (long) from one.xhtml to another .xhtml.
Both the backing beans are #ViewScoped and I am trying to share long id between them.
I am getting error with <f:viewParam/>
com.sun.faces.mgbean.ManagedBeanCreationException: Unable to create
managed bean saleOrder. The following problems were found:
- The scope of the object referenced by expression #{param.foo}, request, is shorter than the referring managed beans (saleOrder) scope of view.
I am have following code.
#ManagedBean
public class InvoiceView{
private long number;
// setter getter.
}
#ManagedBean
#ViewScoped
public SearchInvoice{
private List<InvoiceView> views;
private InvoiceView selectedView; // this is coming from <p:dataTable>
}
#ManagedBean
#ViewScoped
public class SaleOrder {
#ManagedProperty("#{param.foo}")
private String number;
#PostConstruct
public void init(){
//sysout number;
}
}
I have following code in searchInvoice.xhtml file.
<!-- I have not desclared <f:metadata/> -->
<h:commandButton value="Place Sale Order"
action="#{searchInvoice.forwardToSaleOrder}" <!-- this return saleOrder.xhtml string -->
rendered="#{not empty searchInvoice.views}">
<f:viewParam name="foo" value="#{searchInvoice.selectedView.number}" />
</h:commandButton>
You can use <f:viewParam> (JSF 2) in saleOrder.xhtml, which works with view scoped beans.
Check out this article.

Button not submit PrimeFaces 3.5

i'm using jsf + primefaces 3.5. And my button isn't calling one method in my managed bean.
I have this xhtml:
<h:form>
<p:inputText id="name" value="#{userMB.userSelected.name}" />
<p:commandButton id="btnSave" value="Salvar" actionListener="#{userMB.save}"/>
</h:form>
And my managed bean is:
#ManagedBean
#SessionScoped
public class UsuarioMB implements Serializable{
User userSelected;
public void save(){
System.out.println(userSelected.getName());
//call my daos and persist in database
}
}
The most curious is that if i remove the , the method is called!
If i put a atribute in p:commandButton "imediate = true ", the method is called, BUT, the information (userSelected.name) is null !
Thanks very much :)
It failed because it threw a NullPointerException because you never initialized userSelected.
Add this to your bean:
#PostConstruct
public void init() {
userSelected = new User();
}
If you have paid attention to the server logs, you should have seen it. As to the complete absence of feedback about the exception in the webbrowser, whereas in normal synchronous (non-ajax) you would have seen a HTTP 500 error page, it's because you're sending an ajax request without apparently an ExceptionHandler configured.
That it works when you set immediate="true" on the button is simply because it will then bypass the processing of all input components which do not have immediate="true" set.
See also:
What is the correct way to deal with JSF 2.0 exceptions for AJAXified components?
You have not given a name to the managedbean UsuarioMB. As suche it will be named usuarioMB.
#ManagedBean – marks this bean to be a managed bean with the name
specified in name attribute. If the name attribute in #ManagedBean is
not specified, then the managed bean name will default to class name
portion of the fully qualified class name.
read more about it in this blog: http://mkblog.exadel.com/2009/08/learning-jsf2-managed-beans/
Secondly, if your code above is complete, you are lacking public getter and setter for userSelected.
Thirdly you are missing the ActionEvent as you have declared a parameterless actionlistener, see Differences between action and actionListener
In order to get you code working you will need to change your xhtml to
<h:form>
<p:inputText id="name" value="#{usuarioMB.userSelected.name}" />
<p:commandButton id="btnSave" value="Salvar" actionListener="#{usuarioMB.save}"/>
</h:form>
And your managed bean as follows
import javax.faces.event.ActionEvent;
// ...
#ManagedBean
#SessionScoped
public class UsuarioMB implements Serializable{
private User userSelected;
public void save(ActionEvent event){
System.out.println(userSelected.getName());
}
public User getUserSelected() {
return userSelected;
}
public void setUserSelected(User userSelected) {
this.userSelected = userSelected;
}
}

JSF 2.0 #ManagedProperty is not been set

I am just starting using JSF and I don't understand why my service is not injected into my controller.
#ManagedBean
#ApplicationScoped
public class MyController {
#ManagedProperty(value = "#{service}")
private MyService service;
public void setService(MyService service) {
this.service = service;
}
public MyService getService() {
return service;
}
public void callToService(AjaxBehaviorEvent event) {
System.out.println(service);
}
}
Q: What is the purpose of the value in the #ManagedProperty?
#ManagedBean
#ApplicationScoped
public class MyService {
}
Clicking on the button calls the method callToService but the service is null.
<h:form>
<h:commandButton value="Call Service">
<f:ajax listener="#{myController.callToService}"/>
</h:commandButton>
</h:form>
That can happen when #{service} actually resolves to null.
When you use #ManagedBean without the name attribute as you did, the managed bean name will by default resolve to the classname with 1st char lowercased (at least, conform the Javabeans spec), so your MyService bean will effectively get a managed bean name of myService.
So there are basically 2 ways to fix this problem,
Use #{myService} instead.
#ManagedProperty("#{myService}")
private MyService service;
Specify the managed bean name yourself so that it becomes available as #{service}.
#ManagedBean(name="service")
#ApplicationScoped
public class MyService {
}
Unrelated to the concrete problem, since you don't seem to be interested in the ajax event, but rather in the action event, then you can also simplify the use of <f:ajax> as follows:
<h:commandButton value="Call Service" action="#{myController.callToService}">
<f:ajax />
</h:commandButton>
with
public void callToService() {
System.out.println(service);
}
so that it'll still work when the enduser doesn't have JS enabled.
Finally, a business service is normally designed as a #Stateless EJB, not as a JSF managed bean since it should have no direct relationship with a JSF view. You could then just use
#EJB
private MyService service;

Resources