i have two beans DoctorDetailsBean,PatientDetailsBean ,want to populate a combo box with all doctor names in patientRegistration.xhtml
<h:selectOneMenu value="#{patientDetailsBean.refDr}" style="font-size: medium" >
<f:selectItems value="#{patientDetailsBean.drDetailsbeanList}" var="dr" itemLabel="#{dr.doctorName}" itemValue="#{dr.doctorId}" />
</h:selectOneMenu>
DoctorDetailsBean:
#ManagedBean(name="doctorBean")
#SessionScoped
public class DoctorDetailsBean {
private Long doctorId;
private String doctorName;
private String place;
private Long phoneNumber;
//setters&getters
}
PatientdetailsBean:
# ManagedBean(name= "patientDetailsBean")
#SessionScoped
public class PatientDetailsBean implements Serializable {
private Long patientId;
private String patientName;
private long refDr;
#ManagedProperty (value ="#{doctorBean}")
private DoctorDetailsBean doctorDetailsBean;
private List<Doctor> drList;
private List<DoctorDetailsBean> drDetailsbeanList;
private PatientDAO patientDAO;
private DoctorDAO doctorDAO;
//setters&getters
public PatientDetailsBean() {
}
#PostConstruct
public void init() throws AppException,AppSysException{
drList = new ArrayList<Doctor>();
drList = doctorDAO.getAllDoctors();
drDetailsbeanList = new ArrayList<DoctorDetailsBean>();
for (Doctor doctor : drList) {
DoctorDetailsBean doctorDetailsBean = new DoctorDetailsBean();
doctorDetailsBean.setDoctorId(doctor.getDoctorId());
doctorDetailsBean.setDoctorName(doctor.getDoctorName());
doctorDetailsBean.setPlace(doctor.getPlace());
doctorDetailsBean.setPhoneNumber(doctor.getPhoneNumber());
drDetailsbeanList.add(doctorDetailsBean);
} }
How to solve my problem
Your problem is with type of drDetailsbeanList. List<DoctorDetailsBean> is not correct. Create method which returns List<SelectItem> and use it in xhtml.
Related
I am try to develop a web application where I need the Conversation scope to carry on with the same String value in multiple xhtml pages with Primefaces 3.5.
When I begin the conversation with conversation.begin(), it throws null pointer exception with conversation being as null.
I know I can't create a instance of Conversation using 'new'. But I can't figure out where am I going wrong and why is it coming to be null.
Please guide.
Snippet of the xhtml where the bean method is called:
<p:column style="text-align: left" headerText="Deal ID"
width="30">
<p:commandLink value="#{selectedDealBean.getDealID()}"
action="#{SearchBean.action(selectedDealBean.getDealID())}"
process="#this" >
</p:commandLink>
</p:column>
Snippet of bean:
#Named()
#ManagedBean
#ConversationScoped
public class SearchBean implements Serializable {
#Inject
private Conversation conversation;
private DealBean selectedDealBean;
private String selectedID;
private SearchObject searchObj = new SearchObject();
public void start() {
this.conversation.begin();
}
public void end() {
conversation.end();
}
public void submit() {
System.out.println(selectedDealBean);
}
public String action(String selectedID) {
String actionstatus = null;
setSelectedID(selectedID);
actionstatus = "/common/dealDisplay.xhtml?faces-redirect=true" ;
start();
return actionstatus;
}
public String onFinish() {
end();
return "/common/create.xhtml";
}
}
You're mixing annotations. Since you're using CDI you should remove #ManagedBean annotation which is possibly the reason why your injection isn't working.
This is my method SaveClient() that create a new client even if all fields are empty !
public void SaveClient() {
client = new Client();
client.setNom(nom);
client.setPrenom(prenom);
client.setAdresse(adresse);
client.setDomaine(domaine);
client.setRaisonSociale(raisonSociale);
client.setDateDAbonnement(dateDAbonnement);
client.setDateFAbonnement(dateFAbonnement);
client.setType(type);
client.setEmail(email);
client.setnFixe(nFixe);
client.setnGsm(nGsm);
client.setnFixe2(nFixe2);
client.setnGsm2(nGsm2);
client.setVille(ville);
client.setPays(pays);
Set<ConstraintViolation<Client>> violations = validator
.validate(client);
basicController.<Client> processValidation(violations);
if (violations.size() == 0) {
clientBean.creerClient(client);
basicController.addMessage(" Client has been created successfully",
FacesMessage.SEVERITY_INFO, null);}
Just bind the inputs to the model directly instead of flattening out and/or duplicating the model into the backing bean.
I.e. do not do
#Named
public class Bean {
private String firstname;
private String lastname;
private String address;
// ...
public void save() {
Client client = new Client(firstname, lastname, address /* , ...*/);
clientService.save(client);
}
}
with
<h:inputText value="#{bean.firstname}" />
<h:inputText value="#{bean.lastname}" />
<h:inputText value="#{bean.address}" />
...
But instead do
#Named
public class Bean {
private Client client;
#PostConstruct
public void init() {
client = new Client();
}
public void save() {
clientService.save(client);
}
}
with
<h:inputText value="#{bean.client.firstname}" />
<h:inputText value="#{bean.client.lastname}" />
<h:inputText value="#{bean.client.address}" />
...
Provided that you've a
#Entity
public class Client {
#NotNull
private String firstname;
#NotNull
private String lastname;
#NotNull
private String address;
// ...
}
This way JSF will automatically take any bean validation annotations in Client class into account. Also, this way you end up with much simpler code without unnecessary duplication.
I am trying access the form backing bean data from by controller bean. The value from form
gets set in the bean but when i am trying to access it from the controller bean the value comes null.The null value is in createTicket method :---> ticketData.getSummary());
/*from data bean everything sets here*/
import java.io.Serializable;
import java.util.Date;
import java.util.List;
#SuppressWarnings("PMD")
#ManagedBean(name = "createTicketModelData")
#SessionScoped
public class CreateTicketModelData implements CreateTicketData, Serializable {
private static final long serialVersionUID = 1L;
protected String incidentType;
private TicketId ticketId;
protected UserId receiverId;
protected String summary;
private String description;
private String asset;
private Date dateTime;
protected Date reported;
private Date dateChangeNeeded;
private String priority;
protected List<Attachment> attachments;
public void setAttachments(final List<Attachment> attachments) {
this.attachments = attachments;
}
protected String orgUnit;
protected String location;
protected String costCenter;
private Type type = Type.INCIDENT;
private String severity;
#Override
public String getAsset() {
return asset;
}
#Override
public List<Attachment> getAttachments() {
return attachments;
}
public String getCostCenter() {
return costCenter;
}
.....//getter seters
----------------------------------------------------------------------
/*this is the controller bean where i am not able to get the form data values*/
#ManagedBean(name = "createTicketBaseBean")
#SessionScoped
public class CreateTicketBaseBean implements Serializable {
private static final long serialVersionUID = 1L;
private static final transient Logger LOGGER = LoggerFactory.getLogger(CreateTicketBaseBean.class);
#ManagedProperty(value = "#{ticketData}")
private transient CreateTicketModelData ticketData;
#ManagedProperty(value = "#{ticketingService}")
transient TicketingService dispatcher;
#PostConstruct
protected void init() {
this.workplaceBean = JSFUtils.findBean("selectWorkplaceComponentBean");
this.selectUserBean = JSFUtils.findBean("selectUserBean");
}
public void createTicket(final ActionEvent event) {
Response response = null;
System.out.println("ticket summary------------" + ticketData.getSummary());
setTicketData();
LOGGER.info("Incident type in ticketdatabean---->" + incidentType);
try {
response = dispatcher.createTicket(ticketData);
} catch (Exception e) {
LOGGER.error(e);
FacesMessageUtil.addGlobalUnexpectedErrorMessage();
}
LOGGER.info("Response is---->" + response.getTicketId());
ticketId = response.getTicketId();
}
Try changing the value in the annotation #ManagedProperty(value = "#{ticketData}") to use the bean name that you have annotated it with "createTicketModelData".
Using jsf I want to edit a employee profile, when user will click on any particular datatable row,then
I am able to get all that deatils of selected patient in an
arraylist. Now I want to set all the attritbutes in arraylist to
page1.xhtml backingbean , so When user will select a particular row,
he will navigate to page1.xhtml where he will get all these fields in
the form set already by arraylist attributes.
I am trying in this way.
> page1.xhtml
<h:outputLabel value="Name" />
<p:inputText id="name1" value="#{employeeBB.emp.name}" >
</p:inputText>
<h:outputLabel value="age" />
<p:inputText id="ag" value="#{employeeBB.emp.age}" >
</p:inputText>
<h:outputLabel value="code" />
<p:inputText id="code1" value="#{employeeBB.emp.code}" >
</p:inputText>
#ManagedBean(name = "employee")
#ViewScoped
public class emp {
private String name;
private String age;
private String code;
public String getName()
{ return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
#SessionScoped
#ManagedBean
public class EmployeeBB implements serializable{
private Employe emp;
public Employee getEmp() {
return emp;
}
public void setEmp(Employee emp) {
this.emp = emp;
}
}
#SessionScoped
#ManagedBean
public class AddEmployeeBB{
private ArrayList<Employee>empList;
private ArrayList<Employee>empList;
public ArrayList<Employee> getEmpList() {
if(empList==null){
empList=new ArrayList<Employee>();
}
return empList;
}
public void setEmpList(ArrayList<Employee> empList) {
this.empList = empList;
}
public void method() throws IOException{
String code='123';
EmployeeDAO obj=new EmployeeDAO(); // DAO class
empList=obj.getAllEmplInfo(code); // will get all needed information about employee of this code in this arrayist
for(int i=0;i<empList.size();i++){
String name=empList.get(i).getName();
String age=empList.get(i).getAge();
String code=empList.get(i).getCode();
Employee e=new Employee();
e.setName(name);
e.setAge(age);
e.setCode(code);
EmployeeBB obj1=new EmployeeBB();
obj1.setEmp(e); // now according to my logic object e will set to emp object of Employee, and
// that means all these values name ,agem and code will be set to my page1.xhtml and I will be able to see it.
}
}
But I am unable to get pag1.xhtml with filled values.
Show me the way.
The reason for it not being shown is that you are setting values in a object which you are creating
EmployeeBB obj1=new EmployeeBB();
obj1.setEmp(e);
JSF lifecycle doens't know about this object and everytime you are seeing blank.
In AddEmployeeBB add this
#ManagedProperty(value="employeeBB")
private EmployeeBB employeeBB = null; // create getter setter for this
then instead of this :
EmployeeBB obj1=new EmployeeBB();
obj1.setEmp(e);
Use this:
this.employeeBB.setEmp(e);
This is my first post after many research on this problem.
This example is running under Jboss 7.1 with seam 3.1 (solder + persistence + faces) with seam managed persistence context
I'm facing a problem, the classical failed to lazily initialize a collection, no session or session was closed: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed when using a converter on Entity beans. The aim is to stay 100% Object oriented, by reusing the JPA model.
in beans.xml, org.jboss.seam.transaction.TransactionInterceptor is activated
Entity beans :
#Entity
public class Member implements Serializable {
#Id
#GeneratedValue
private Long id;
private String name;
private String email;
#Column(name = "phone_number")
private String phoneNumber;
#ManyToMany
private List<Statut> listeStatut = new ArrayList<Statut>();
// getters, setters, hashcode, equals
}
#Entity
public class Statut implements Serializable {
#Id
#GeneratedValue
private Long id;
private String name;
#ManyToMany(mappedBy="listeStatut")
private List<Member> members = new ArrayList<Member>();
// getters, setters, hashcode, equals
}
The JSF page :
<h:form>
<h:selectManyCheckbox id="stat" value="#{memberModif.member.listeStatut}">
<f:converter converterId="statutConverter"/>
<f:selectItems value="#{memberModif.statutsPossibles}" var="statut" itemValue="#{statut}" itemLabel="#{statut.name}" />
</h:selectManyCheckbox>
<h:commandLink id="register" action="#{memberModif.modifier()}" value="Modifier">
<f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
</h:commandLink>
</h:form>
The backing bean (I tried with ConversationScoped after SessionScoped --> same problem)
#ConversationScoped
#Named
public class MemberModif implements Serializable {
private static final long serialVersionUID = -291355942822086126L;
#Inject
private Logger log;
#Inject
private EntityManager em;
#Inject Conversation conversation;
private Member member;
#SuppressWarnings("unused")
#PostConstruct
private void init() {
if (conversation.isTransient()) {
conversation.begin();
}
}
public String modifier() {
em.merge(member);
}
public Member getMember() {
if (member == null) {
member = em.createQuery("from Member m where m.id=:id",Member.class).setParameter("id", new Long(0)).getSingleResult();
}
return member;
}
public List<Statut> getStatutsPossibles() {
return em.createQuery("from Statut", Statut.class).getResultList();
}
}
And the converter (strongly inspired by seam ObjectConverter) :
#FacesConverter("statutConverter")
public class StatutConverter implements Converter, Serializable {
final private Map<String, Statut> converterMap = new HashMap<String, Statut>();
final private Map<Statut, String> reverseConverterMap = new HashMap<Statut, String>();
#Inject
private transient Conversation conversation;
private final transient Logger log = Logger.getLogger(StatutConverter.class);
private int incrementor = 1;
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (this.conversation.isTransient()) {
log.warn("Conversion attempted without a long running conversation");
}
return this.converterMap.get(value);
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (this.conversation.isTransient()) {
log.warn("Conversion attempted without a long running conversation");
}
if (this.reverseConverterMap.containsKey(value)) {
return this.reverseConverterMap.get(value);
} else {
final String incrementorStringValue = String.valueOf(this.incrementor++);
this.converterMap.put(incrementorStringValue, (Statut)value);
this.reverseConverterMap.put( (Statut)value, incrementorStringValue);
return incrementorStringValue;
}
}
}
Please note that I put this converter here to avoid you searching over the net for the seam implementation, but it is the same as using <s:objectConverter/> tag instead of <f:converter converterId="statutConverter"/>
Any help would be greetly appreciated.
You should access the objects in the same transaction. If you are sure you are doing that already, you could try getting the entitymanager by looking it up in the context instead of injecting it. Ive had a simular problem which was resolved that way. You can also initialize the collection in the transaction when you first got your reference to it.
Hibernate.initialize(yourCollection);
Take a look at this: selectManyCheckbox LazyInitializationException on process validation
Try:
<f:attribute name="collectionType" value="java.util.ArrayList" />;
on your <h:selectManyCheckbox>