set a managed bean property from composite component - jsf-2

I have model class called Person and a view scope managed bean PersonController which contain a list of person
I have created a composite component that take this list of persons . What i want to do is to set the List of persons in other managed bean called TestCompositeComponent from the composite component directly .. Any solution ?.. This is my code :
#ManagedBean
#ViewScoped
public class PersonController {
private List<Person> persons;
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
#PostConstruct
public void init() {
persons = new ArrayList<>();
Person person1 = new Person();
person1.setFirstname("blah");
person1.setLastname("blah");
Person person2 = new Person();
person2.setFirstname("blah");
person2.setLastname("blah");
persons.add(person1);
persons.add(person2);
}
}
#ManagedBean
#ViewScoped
public class TestCompositeComponentController {
private List<Person> persons;
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface componentType="testCompositeController">
<composite:attribute name="persons" />
</composite:interface>
<composite:implementation>
<h:outputText value="composite"></h:outputText>
</composite:implementation>
</html>
<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:test="http://java.sun.com/jsf/composite/test"
xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:body>
<test:test persons="#{personController.persons}" />
</h:body>
</html>

Do you want to change the List persons passed from the PersonController to the TestCompositeComponentController back to PersonController?
If this is the case, change the scope of PersonController to #SessionScoped and inject it to TestCompositeComponentController. This way you will be able to modify the persons in the PersonController.
#ManagedBean
#ViewScoped
public class TestCompositeComponentController {
private List<Person> persons;
#Inject PersonController personController;
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
public void updateTheOtherList() {
personController.setPersons(this.persons);
}
}
In a real case scenario, the modifications in your composite component would be persisted in a database and when you navigate back the changes would be visible by a new fetch on the person list.

Related

Injected cdi bean doesn't hold variable

Hi I have experimented #Inject annotation with cdi bean. I have one variable in first bean which is injected into second bean. When I call method on second bean using variable in first bean, during that method call first bean's variable returns null.
Here is xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Experiments</title>
</h:head>
<h:body>
<h:form id="main_form">
<h:inputText id="nameInput" value="#{myBean.name}"></h:inputText>
<h:commandButton value ="ok" action="#{myBean2.print()}">
<f:ajax execute="nameInput" render="nameOutput"/>
</h:commandButton>
<h:outputText id ="nameOutput" value="#{myBean2.name}"/>
</h:form>
</h:body>
</html>
beans:
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable
#Named(value = "myBean")
#SessionScoped
public class MyBean implements Serializable {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* Creates a new instance of myBean
*/
public MyBean() {
}
}
second bean: when I call print method of mybean2, injected myBean's 'name' property returns null.
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named
#Named(value = "myBean2")
#SessionScoped
public class MyBean2 implements Serializable{
String name;
#Inject
MyBean myBean;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String print(){
this.name = this.myBean.name;
return null;
}
/**
* Creates a new instance of myBean2
*/
public MyBean2() {
}
}
Is that normal or something wrong with code or config?

Creating dynamic inputs in jsf2

i want to create dynamic textbox in jsf2 in datatable having textboxes based on clicking add row button. Iam a newbie for jsf programming.Can someone tell me a basic example for dynamic for generation . I have read ui:repeatand c:foreach but i need some practical example.
For your sample,
xhtml code:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form id="form1">
<h:commandButton value="Add new Row" actionListener="#{myBean.addNewEmployee}"/>
<h:dataTable value="#{myBean.employeeList}" var="emp">
<h:column>
<f:facet name="header">
Employee Id
</f:facet>
#{emp.empId}
</h:column>
<h:column>
<h:inputText value="#{emp.empName}"/>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
jsf manged bean
#ManagedBean
#ViewScoped
public class MyBean implements Serializable {
List<Employee> employeeList;
public List<Employee> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<Employee> employeeList) {
this.employeeList = employeeList;
}
public void addNewEmployee(ActionEvent event) {
employeeList.add(new Employee(employeeList.size(), null));
}
#PostConstruct
public void init() {
employeeList = new ArrayList<Employee>();
employeeList.add(new Employee(1, "Emp1"));
employeeList.add(new Employee(2, "Emp2"));
}
public MyBean() {
}
}
Employee Class(You can use your entity class)
public class Employee {
Integer empId;
String empName;
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Employee(Integer empId, String empName) {
this.empId = empId;
this.empName = empName;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Employee other = (Employee) obj;
if (this.empId != other.empId && (this.empId == null || !this.empId.equals(other.empId))) {
return false;
}
return true;
}
#Override
public int hashCode() {
int hash = 5;
hash = 41 * hash + (this.empId != null ? this.empId.hashCode() : 0);
return hash;
}
}
You can directly use above code. And also I suggest to use PrimeFaces component for advanced manipulations

EJB method is not calling from constructor

I created an ejb
#Stateless
#LocalBean
public class BasitBean {
public String helloBasit() {
return "Basit";
} //end of helloBasit()
} //end of class BasitBean
I am calling it from JSF like
<h:body>
<h:outputLabel value="#{helloBasit.callBasit()}" />
</h:body>
#ManagedBean
#SessionScoped
public class HelloBasit {
#EJB
private BasitBean basitBean;
/** Creates a new instance of HelloBasit */
public HelloBasit() {
}
public String callBasit() {
return basitBean.helloBasit();
} //end of callBasit()
} //end of class HelloBasit
This code is working fine. But when i change the code like this
<h:body>
<h:outputLabel value="#{helloBasit.label}" />
</h:body>
#ManagedBean
#SessionScoped
public class HelloBasit {
#EJB
private BasitBean basitBean;
String label;
/** Creates a new instance of HelloBasit */
public HelloBasit() {
System.out.println();
String label = basitBean.helloBasit();
System.out.println(label);
}
public BasitBean getBasitBean() {
return basitBean;
}
public void setBasitBean(BasitBean basitBean) {
this.basitBean = basitBean;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
} //end of class HelloBasit
Then i get the exception
SEVERE: Error Rendering View[/index.xhtml]
com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: pk.mazars.basitMahmood.HelloBasit.
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193)
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
......
Why i am getting this exception? The flow should be what i understand is when my page encounters #{helloBasit.label} then my constructor get call, instance variable get initialized, injected the bean instance into the basitBean, then the bean method should call. But i am getting null in the bean instance in this case why? Why previous code is working and it is not ? How can i call bean from the constructor ?
Thank you.
try to move your content of the constructor into a post constructor instead...
like this
#PostConstruct
private void init() {
System.out.println();
String label = basitBean.helloBasit();
System.out.println(label);
}
Cause the ejb bean should be injected only after the managed bean has been initiated
The #PostConstruct is being run after the constructor (after that the managed bean itself was created by the JSF) and only then the EJB is being injected into the bean and can be accessed...
Your idea is correct, but I see some things that may be fixed.
#LocalBean annotation is not required if your EJB is not directly implementing an interface. In this case, with or without the #LocalBean annotation you have the same effect. You may leave that if you want to make it explicit though. See this.
Make sure both #ManagedBean and #SessionScoped import from javax.faces.bean package.
Please, see this working sample:
EJB
import javax.ejb.Stateless;
#Stateless
public class PersonService {
public String getName() {
return "Cloud Strife";
}
}
Managed Bean
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class PersonBean {
#EJB
private PersonService ps;
private String name;
#PostConstruct
public void init() {
name = ps.getName();
}
public String getName() {
return name;
}
}
XHTML Page
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<f:view contentType="text/html">
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h1>Welcome, #{personBean.name}</h1>
</h:body>
</f:view>
</html>
If your value should be loaded only once, say at your bean construction, always prefer a method with #PostConstruct annotation instead of the constructor.
Also, in order to call bean methods before rendering a view you could use a f:event tag, for example:
<f:event type="preRenderView" listener="#{personBean.init}" />
I hope it helps!

JSF Initialize Map Object

I'm starting my first steps in JSF.
I've already read this link
http://docs.oracle.com/javaee/6/tutorial/doc/bnawq.html#bnaww
in regards to map initialization.
The problem is, I want to populate my map with values residing in a file.
How can I do that?
I've tried not using faces-config.xml and calling a support method in the bean's constructor, but my select list box isn't populated.
My bean class:
#ManagedBean
public class ADGroupListBean {
private static final String WITH_ACCESS = "D:\\workspace\\AccessControl\\permissions.txt";
private static final String WITHOUT_ACCESS = "D:\\workspace\\AccessControl\\noPermissions.txt";
private Map<String,String> withAccess, withoutAccess;
private LDAPQueries queries;
public ADGroupListBean(){
withAccess = new LinkedHashMap<String, String>();
withoutAccess = new LinkedHashMap<String, String>();
queries = new LDAPQueries();
initList(WITH_ACCESS, withAccess);
initList(WITHOUT_ACCESS, withoutAccess);
}
private void initList(String filename, Map<String,String> list) {
File f = new File(filename);
if ( !f.exists() && f.getAbsolutePath().equals(WITHOUT_ACCESS) )
{
queries.queryAllGroups(WITHOUT_ACCESS);
}
try
{
Scanner sc = new Scanner(f);
while (sc.hasNext())
{
String group = sc.nextLine();
list.put(group, group);
}
}catch (IOException e) {
e.printStackTrace();
}
}
// public void populateList() {
//
//
// }
public Map<String,String> getWithAccess() {
return withAccess;
}
public Map<String,String> getWithoutAccess() {
return withoutAccess;
}
public void setWithoutAccess(Map<String,String> withoutAccess) {
this.withoutAccess = withoutAccess;
}
public void setwithAccess(Map<String,String> withAccess) {
this.withAccess = withAccess;
}
public void test() {
System.out.println("workssssssssssssssssss");
}
}
As for my JSF file, it is like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head><title>Your Title Here</title>
</h:head>
<h:body>
<h1>Your Heading Here</h1>
<h:form>
<h:selectOneMenu value="teste">
<f:selectItem itemLabel="" itemValue="" />
<f:selectItems value="#{ADGroupListBean.withoutAccess}" />
</h:selectOneMenu>
</h:form>
</h:body>
</html>
I've tested the bean's functions in a test application, and everything works fine.
So my guess is the bean isn't instantiated?
Regards,
Nuno.

FacesException: DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled

I am trying to learn PF so I started with displaying datatable first and navigating to next page on rowClick passing parameters, but got stuck with the following error. I found similar problem for that question but no luck yet. I hope somebody will help me out.
I am getting following error:
DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.
Caused by:
javax.faces.FacesException - DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.
my Page:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Primefaces 3.1</title>
</h:head>
<h:body>
<h:form id="form">
<p:dataTable value="#{tableBean.cars}" var="var" rowkey="#{var.model}"
selection="#{tableBean.car}" selectionMode="single">
<p:column> <f:facet name="header">
<h:outputText styleClass="outputText" value="Model"></h:outputText>
</f:facet>
<h:outputText styleClass="outputText"
value="#{var.model}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText styleClass="outputText" value="Color"></h:outputText>
</f:facet>
<h:outputText styleClass="outputText"
value="#{var.randomColor}"></h:outputText>
</p:column></p:dataTable>
</h:form>
</h:body>
</html>
my bean Classes:
#ManagedBean
#ViewScoped
public class TableBean extends ListDataModel<Car> implements SelectableDataModel<Car>{
private List<Car> cars;
private Car car;
public List<Car> getCars() {
cars = new ArrayList<Car>();
Car car1 = new Car();
car1.setModel("BMW");
car1.setRandomColor("Black");
cars.add(car1);
Car car2 = new Car();
car2.setModel("Audi");
car2.setRandomColor("White");
cars.add(car2);
return cars;
}
public String onRowSelect(){
System.out.println("Row Click!!!");
return "otherpage";//Does this nav works???if not how???
}
public Car getCar() {
return car;
}
#Override
public Car getRowData(String pArg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public Object getRowKey(Car pArg0) {
// TODO Auto-generated method stub
return null;
}
}
OtherBean:
public class Car{
private String model;
private String randomColor;
public String getRandomColor() {
return randomColor;
}
public void setRandomColor(String pRandomColor) {
randomColor = pRandomColor;
}
public String getModel() {
return model;
}
public void setModel(String pModel) {
model = pModel;
}
}
The #{tableBean.cars} must implement SelectableDataModel if you don't specify the rowKey attribute on the <p:dataTable>.
You have however implemented it on the #{tableBean} instead. This is not right. You can find correct examples in the PrimeFaces showcase. Basically, your TableBean class must look like this according to the showcase code example:
#ManagedBean
#ViewScoped
public class TableBean implements Serializable {
private List<Car> cars;
private Car car;
private CarDataModel carsModel;
public TableBean() {
cars = new ArrayList<Car>();
// Populate cars here and thus NOT in the getter method!
carsModel = new CarDataModel(cars);
}
// ...
}
Where the CarDataModel look like this (again, just copied from PrimeFaces showcase code example):
public class CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car> {
public CarDataModel() {
}
public CarDataModel(List<Car> data) {
super(data);
}
#Override
public Car getRowData(String rowKey) {
List<Car> cars = (List<Car>) getWrappedData();
for(Car car : cars) {
if(car.getModel().equals(rowKey))
return car;
}
return null;
}
#Override
public Object getRowKey(Car car) {
return car.getModel();
}
}
Finally use #{tableBean.carsModel} instead of #{tableBean.cars} as value of <p:dataTable>. Exactly as in the showcase example.
<p:dataTable value="#{tableBean.carsModel}" var="car" ... />
Another, easier, way is to just specify the rowKey attribute on the <p:dataTable>.
<p:dataTable value="#{tableBean.cars}" var="car" rowKey="#{car.model}" ... />
This way you don't need the whole SelectableDataModel. You only need to ensure that it's never null and always unique across the rows. See also DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled, but I have already defined rowKey.
Ensure to set the rowKey parameter in the bean method that populates the "value=.." of the datatable. Mine returned this error because the rowKey was null.
It worked for my project using a composite key for rowkey e.g.: rowKey="#{course.getCompositeKey()
Since I didn't have a compositeKey variable I simply created one in my Course Class (in your case the Car Class). The 2 primary keys are strings so I just said this.compositeKey=this.courseNumber+this.product--you just use whatever 2 primary keys are in your Car class instead of courseNumber and product.
Youd on't have to implement the datamodal in your managedbean, just specify in the proprety of the datatable the "rowkey" like this: rowkey="{mbean.atribute1}"
-- atribute1 must be diplayed in the data table column.
Must

Resources