JSF User Define Table required multiple Data Storage - jsf

I am able to print the data from a JSF home page in form of table. But the issue is that it is able to print only one entry at a time.What if I enter more names i.e. name and password without using Database. Please find managed bean and format of table is taken by tutorial.
#SessionScoped
#ManagedBean(name = "pprBean")
public class Demos {
static String firstname ;
static String password;
private static final ArrayList<Demos> employees
= new ArrayList<Demos>(Arrays.asList(
new Demos()
));
public ArrayList<Demos> getEmployees() {
return employees;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<f:view>
<h:body>
<h:form>
<h:panelGrid columns="4" cellpadding="5">
<h:outputLabel for="name" value="Name:" style="font-weight:bold"/>
<p:inputText id="name" value="#{pprBean.firstname}" />
<h:outputLabel for="pass" value="Pass:" style="font-weight:bold"/>
<p:inputText id="pass" value="#{pprBean.password}" />
<p:commandButton value="Submit"/>
</h:panelGrid>
<p:outputLabel value="Result"></p:outputLabel>
<h:dataTable value="#{pprBean.employees}" var="result" cellspacing="2"
styleClass="employeeTable"
headerClass="employeeTableHeader"
rowClasses="employeeTableOddRow,employeeTableEvenRow" border="3">
<h:column>
<f:facet name="header">Name</f:facet>
#{result.firstname}
</h:column>
<h:column>
<f:facet name="header">Password</f:facet>
#{result.password}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</f:view>
</html>

Related

JSF datatable values are not displayed

Unfortunately, all the values I entered are not displayed on the website, does anyone have an idea why?
On picture 1 I have screenshotted the output.
Programming with JSF for the first time and with Java was a long time ago...
Here is my products.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://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>JSF Datatable h:dataTable tag demo</title>
</h:head>
<h:body>
<ui:composition template="template/master.xhtml">
<ui:define name="content">
<h:form>
<h:dataTable value = "#{productBean.findAll}"
var = "item"
style="border: 3px solid #FF0000; width:100%"
rows="10"
border="1"
cellpadding="20"
cellspacing="20">
<h:column>
<f:facet name="header"> Produkt ID </f:facet>
<h:outputText value="#{item.id}" />
</h:column>
<h:column>
<f:facet name="header"> Produktname </f:facet>
<h:outputText value="#{item.name}" />
</h:column>
<h:column>
<f:facet name="header" > Preis </f:facet>
<h:outputText value="#{item.preis}" />
</h:column>
<h:column>
<f:facet name="header"> Beschreibung </f:facet>
<h:outputText value="#{item.beschreibung}" />
</h:column>
<h:column>
<f:facet name="header"> Foto</f:facet>
<h:graphicImage library="images" name="item.bildpfad"></h:graphicImage>
</h:column>
<h:column>
<f:facet name="header"> Stückzahl </f:facet>
<h:outputText value="#{item.stückzahl}" />
</h:column>
</h:dataTable>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
My ProductDemo.java
package com.mycompany.kuechenstudio.model;
import java.util.ArrayList;
import java.util.List;
public class ProductDemo {
public List<Product> findAll(){
List<Product> productsList = new ArrayList<>();
productsList.add(new Product(1,"Kühlschrank",400f, "weiß",
"/images/Kühlschrank.jpg",1));
productsList.add(new Product(2,"Tisch ",300f, "braun",
"/images/Küchentisch.jpg",5));
productsList.add(new Product(3,"Schrank1",150f, "klein",
"/images/Küchenschrank_klein.jpg",3));
productsList.add(new Product(4,"Schrank2",200f, "groß",
"/images/Küchenschrank_groß.jpg",2));
productsList.add(new Product(5,"Spülmaschine",350f,"schnell",
"/images/Spülmaschine.jpg",6));
return productsList;
}
}
My Product.java
package com.mycompany.kuechenstudio.model;
public class Product {
private int id;
private String name;
private float preis;
private String beschreibung;
private String bildpfad;
private int stückzahl;
public int getStückzahl(){
return stückzahl;
}
public void setStückzahl(int stückzahl) {
this.stückzahl = stückzahl;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public float getpreis() {
return preis;
}
public String getBeschreibung() {
return beschreibung;
}
public void setBeschreibung(String beschreibung) {
this.beschreibung = beschreibung;
}
public String getBildpfad() {
return bildpfad;
}
public void setBildpfad(String bildpfad) {
this.bildpfad = bildpfad;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPreis(float preis) {
this.preis = preis;
}
public Product(int id, String name, float preis, String beschreibung,
String bildpfad, int stückzahl) {
super();
this.id = id;
this.name = name;
this.preis = preis;
this.beschreibung = beschreibung;
this.bildpfad = bildpfad;
this.stückzahl = stückzahl;
}
}
My beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
bean-discovery-mode="all">
<managed-bean>
<managed-bean-name>productBean</managed-bean-name>
<managed-bean-class>com.jsf.datatable.ProductDemo</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
Die Ausgabe
I got it. The ProductDemo.java must be in a managed bean and the value in the xhtml didnt match with the java class

How to render only an embedded table after removing a row - search expression problem

I've one table embedded into another table. My problem: How to render only the inner table when a row was removed?
Bean:
#Named
#ViewScoped
public class TestBean implements Serializable {
private static final long serialVersionUID = -5633666299306108430L;
private List<String> strLst1;
private List<String> strLst2;
#PostConstruct
public void init() {
strLst1 = Arrays.asList("a", "b", "c");
strLst2 = Arrays.asList("d", "e", "f");
}
public List<String> getStrLst1() {
return strLst1;
}
public List<String> getStrLst2() {
return strLst2;
}
public void removeFromStrLst2(String val) {
strLst2.remove(val);
}
}
Facelet:
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form id="form">
<h:dataTable
id="outerTable"
var="str1"
value="#{testBean.strLst1}"
border="1">
<h:column>
<h:panelGroup id="innerTableContainer">
<h:dataTable
id="innerTable"
var="str2"
value="#{testBean.strLst2}"
border="1">
<h:column>
<h:outputText value="#{str1}: #{str2}" />
</h:column>
<h:column>
<h:commandButton
id="removeButton"
action="#{testBean.removeFromStrLst2(str2)}"
value="RemoveFromStrLst2">
<f:ajax render="innerTableContainer" /> <!-- Problem location -->
</h:commandButton>
</h:column>
</h:dataTable>
</h:panelGroup>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
My problem is the Ajax-render attribute which doesn't get the correct id (examples for the first row):
innerTableContainer leads to mojarra.ab(this,event,'action',0,'innerTableContainer') It doesn't work!
innerTable leads to mojarra.ab(this,event,'action',0,'form:outerTable:0:innerTable:0') It doesn't work!
#parent leads to mojarra.ab(this,event,'action',0,'form:outerTable:0:innerTable:0:j_idt14') It doesn't work!
#namingcontainer leads to mojarra.ab(this,event,'action',0,'form:outerTable:0:innerTable:0') It doesn't work!
#form works but this isn't I was looking for.
The correct and working id would be form:outerTable:0:innerTableContainer but how to get it?
Mojarra 2.3.9.SP01

javax.el.ELException for property not found

I have the following html file:
<h:body>
<h:form id="loginForm">
<p:growl id="msg" showDetail="true" life="3000" />
<p:panel header="Login" style="width: 360px;">
<h:panelGrid id="loginPanel" columns="2">
<h:outputText value="Username" />
<p:inputText id="username" value="#{loginBean.uname}" ></p:inputText>
<p:spacer></p:spacer>
<p:message for="username" ></p:message>
<h:outputText value="Password" />
<p:password id="password" value="#{loginBean.password}" feedback="false"></p:password>
<p:spacer></p:spacer>
<p:message for="password"></p:message>
<p:spacer></p:spacer>
<p:commandButton action="#{loginBean.loginPro}" value="Login" update="loginForm" ajax="true"></p:commandButton>
</h:panelGrid>
</p:panel>
</h:form>
</h:body>
And the LoginBean is as follow:
#ManagedBean(name = "loginBean")
#SessionScoped
public class LoginBean implements Serializable {
private static final long serialVersionUID = 1L;
private String uname;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String loginPro() {
boolean result = false;
try {
result = UserDAO.login(uname, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (result) {
return "home";
} else {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
"Invalid Login!",
"Please Try Again!"));
return "login";
}
}
I keep on receiving the following message eventhough loginPro method exists on LoginBean:
javax.servlet.ServletException: /index.xhtml: Property 'loginPro' not found on type beans.LoginBean
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Any opinions?
try loginBean.loginPro()
with loginBean.loginPro u said there is a property variable with get/set
I had the same issue and resolved adding below code snippet at the beginning
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

Can't get view Scoped bean value on another jsf page?

I am developing a project using JSF. In an opening popup window, i want to show some details about a product but can not get view scoped bean' s value on a datatable.
Can you help me?
Thanks.
Here is my UrunuDenetlemeSayfasi.xhtml code snippet:
<h:commandLink onclick="window.open('UruneGozAt.xhtml',
'Ürün İçeriği', config='width=700, height=400, top=100, left=100,
scrollbars=no, resizable=no');"
action="#{uruneGozAtBean.urunIdsineGoreUrunIcerigiGetir}" value="Ürün İçeriğine Göz At">
<f:param name="urunid" value="#{urun.urunID}" />
</h:commandLink>
Here is UrunuGozAt.xhtml:
<!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:ui="http://java.sun.com/jsf/facelets">
<h:body>
<h:dataTable class="table table-striped"
value="#{uruneGozAtBean.urunIcerik}" var="urun">
<h:column>
<f:facet name="header">
<h:outputText value="barkod no" />
</f:facet>
<h:outputText value="#{urun.barkodNo}" />
</h:column>
</h:dataTable>
</h:body>
</html>
Here is UruneGozAtBean.java
UrunDenetlemeSayfasiBean urunDenetle = new UrunDenetlemeSayfasiBean();
UrunDenetleService urunService = new UrunDenetleService();
private UrunIcerik urunIcerik = new UrunIcerik();
private Long urunIdParametre;
public UrunIcerik getUrunIcerik() {
return urunIcerik;
}
public void setUrunIcerik(UrunIcerik urunIcerik) {
this.urunIcerik = urunIcerik;
}
public Long getUrunIdParametre() {
return urunIdParametre;
}
public void setUrunIdParametre(Long urunIdParametre) {
this.urunIdParametre = urunIdParametre;
}
public void urunIdsineGoreUrunIcerigiGetir() {
setUrunIcerik(urunService.urunIdsineGoreUrunIcerigiGetir(urunIdEldeEt()));
}
public Long urunIdEldeEt(){
FacesContext fc = FacesContext.getCurrentInstance();
setUrunIdParametre(getUrunIdParametre(fc));
return getUrunIdParametre();
}
public Long getUrunIdParametre(FacesContext fc){
Map<String, String> parametre = fc.getExternalContext().getRequestParameterMap();
return Long.valueOf(parametre.get("urunid")).longValue();
}
EDIT:
This is now my current implementation, it returns null.
i am developing a project using JSF. In an opening popup window, i want to show some details about a product but can not get view scoped bean' s value on a datatable.
Can you help me?
Thanks.
Here is my UrunuDenetlemeSayfasi.xhtml code snippet:
<h:commandLink onclick="window.open('UruneGozAt.xhtml','Ürün İçeriği',
config='width=700, height=400, top=100, left=100, scrollbars=no, resizable=no');"
value="Ürün İçeriğine Göz At"> <f:param name="urunId" value="#{urun.urunID}" />
</h:commandLink>
Here is UruneGozAt.xhtml:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:metadata>
<f:viewParam name="urunId" value="#{uruneGozAtBean.urunId}"
required="false" />
<f:viewAction action="#{uruneGozAtBean.urunIdsineGoreUrunIcerigiGetir()}" />
</f:metadata>
<h:head>
<title>Ürün İçeriği</title>
<!-- add this always, even if it's empty -->
</h:head>
<h:body>
<h:dataTable class="table table-striped"
value="#{uruneGozAtBean.urunIcerik}" var="urun">
<h:column>
<f:facet name="header">
<h:outputText value="barkod no" />
</f:facet>
<h:outputText value="#{urun.barkodNo}" />
</h:column>
</h:dataTable>
</h:body>
</html>
Here is UruneGozAtBean.java
#ManagedBean
#ViewScoped
public class UruneGozAtBean {
public UrunDenetlemeSayfasiBean urunDenetle = new UrunDenetlemeSayfasiBean();
public UrunDenetleService urunService = new UrunDenetleService();
private ArrayList<UrunIcerik> urunIcerik = new ArrayList<UrunIcerik>();
private Long urunId;
public Long getUrunId() {
return urunId;
}
public void setUrunId(Long urunId) {
this.urunId = urunId;
}
public ArrayList<UrunIcerik> getUrunIcerik() {
return urunIcerik;
}
public void setUrunIcerik(ArrayList<UrunIcerik> urunIcerik) {
this.urunIcerik = urunIcerik;
}
public void urunIdsineGoreUrunIcerigiGetir() {
setUrunIcerik(urunService.urunIdsineGoreUrunIcerigiGetir(urunIdEldeEt()));
System.out.print("aaa");
}
public Long urunIdEldeEt() {
FacesContext fc = FacesContext.getCurrentInstance();
setUrunId(getUrunId(fc));
return getUrunId();
}
public Long getUrunId(FacesContext fc) {
Map<String, String> parametre = fc.getExternalContext().getRequestParameterMap();
return Long.valueOf(parametre.get("urunId")).longValue();
}
}
#ViewScoped beans are alive per view. If you open a popup window from your current view, then you're opening a new view, so even if it uses the same managed bean to display the data, since they're different views, they use different instances of the same class.
In cases like this, you should pass a parameter through query string, then receive it in your view and process it to load the desired data. In this case, your code would be like this (note: make sure you send the parameter with name "urunId"):
UrunuGozAt.xhtml:
<!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:ui="http://java.sun.com/jsf/facelets">
<h:head>
<!-- add this always, even if it's empty -->
<f:metadata>
<f:viewParam name="urunId" value="#{uruneGozAtBean.urunId}"
required="false" />
<f:viewAction action="#{uruneGozAtBean.loadData}" />
</f:metadata>
</h:head>
<h:body>
<h:dataTable class="table table-striped"
value="#{uruneGozAtBean.urunIcerik}" var="urun">
<h:column>
<f:facet name="header">
<h:outputText value="barkod no" />
</f:facet>
<h:outputText value="#{urun.barkodNo}" />
</h:column>
</h:dataTable>
</h:body>
</html>
UruneGozAtBean managed bean:
#ViewScoped
#ManagedBean
public class UruneGozAtBean {
//your current fields, getters and setters...
private Long urunId;
//getter and setter for this field...
public void loadData() {
if (urunId != null) {
//load the data for the table...
}
}
}
More info:
What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
How to choose the right bean scope?
DataTable expects a list to iterate through, but as far as I can see you return an UrunIcerik object.

primefaces command button/link not working [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 6 years ago.
Can anyone tel me why command button/link is not getting fired. This welcomePage() should be invoked on button press. I checked the managed property "value" is displaying on the execution, but button or link is not getting invoked.
My loginBean:
import java.io.IOException;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
#ManagedBean(name = "loginBean", eager = true)
#RequestScoped
public class loginBean implements Serializable {
private String value = "This text shows, bean is working ";
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
#ManagedProperty(value="#{name}")
public static String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#ManagedProperty(value="#{password}")
public static String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String welcomePage()
{
System.out.println("welcome user according to user name");
return "customerGreeting.xhtml";
}
}
My index.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:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<f:view contentType="text/html">
<h:head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Facelet Title</title>
</h:head>
<h:body>
<ui:composition>
<div align ="center">
<h:form id="form" align="center">
<h:panelGrid id="panel" columns="2" >
<f:facet name="header">
<h:outputText value="Login here:"/>
</f:facet>
<h:outputLabel value="Username" />
<p:inputText id="name" value="#{loginBean.name}" />
<h:outputLabel value="Password" />
<p:password id="password" value="#{loginBean.password}" />
<f:facet name="footer">
<h:panelGroup style="display:block; text-align:center">
<p:commandButton id="submit1" value="Submit1" actionListener="#{loginBean.welcomePage}" />
<p:commandLink actionListener="#{loginBean.welcomePage}">
<h:outputText id= "submit2" value="Submit2" />
</p:commandLink>
</h:panelGroup>
</f:facet>
</h:panelGrid>
#{loginBean.value}
</h:form>
</div>
</ui:composition>
</h:body>
</f:view>
</html>
Any help ??
you should use action instead of actionListener, because action needs to return a string for navigation rules

Resources