Does anyone know how do I show streamming tweet in .jsf page. I want to show user status directly when it comes on my page. I created backing bean named Tweet:
#Named(value = "tweet")
#Dependent
public class Tweet {
private String user;
private String status;
private String date;
And then set data when it comes in TweetBean:
#Named(value = "tweetBean")
#Dependent
public class TweetBean {
...
public void open() {
StatusListener listener = new StatusListener() {
#Override
public void onStatus(Status status) {
Tweet tweet = new Tweet();
tweet.setUser("#" + status.getUser().getName());
tweet.setStatust(status.getText());
tweet.setDate(String.valueOf(created.format(status.getCreatedAt())));
}
I am having facelet named tweets.xhtml but newly streamed tweets won't show. why? Should I use f:ajax render = "#form" or something else?
<h:body>
<h:form >
<div>
User: #{tweet.user}<br/>
Status: #{tweet.status} <br/>
Date: #{tweet.date} <br/>
</div>
</h:form>
</h:body>
I have found the solution using PrimeFaces p:poll component like so:
<h:form id="form">
<p:dataTable id="statuses"
value="#{tweetBean.statusesLinkedList}"
var="st"
sortMode="multiple"
rows="5"
paginator="true" >
<p:column headerText="Message:">
<h:outputText value="#{st.status}"/>
</p:column>
<p:column headerText="Date:" sortBy="#{st.date}">
<h:outputText value="#{st.date}"/>
</p:column>
<p:column headerText="User:">
<h:outputText value="#{st.user}"/>
</p:column>
</p:dataTable>
<p:commandButton type="submit" value="Stop"
actionListener="#{tweetBean.close()}"
ajax="false" action="index?facesredirect=true">
</p:commandButton>
<p:poll interval="3" update="statuses" />
Related
in my xhtml i define a inputText variable from which i want to set a specific value to a specific field in my List. Every time i click confirm and debug my code the value is returned as "" (that i set in my init function). I have also tried hardcoding my inputText value to be set to dashboardFilterDialogBean.dashboardObject.texts[0] as in the only item in the list i define but still no result. I get 0 exceptions in my console
Xhtml
<p:dialog id="dashboardFilterDialog">
<h:form id="dashboard_filter_dialog_form">
<ui:repeat value="#{dashboardFilterDialogBean.dashboardObject.texts}" varStatus="loop">
<p:row>
<p:column>
<h:outputText value="Text" />
</p:column>
<p:column>
<p:inputText value="#{dashboardFilterDialogBean.dashboardObject.texts[loop.index]}" />
</p:column>
</p:row>
</ui:repeat>
</h:form>
<f:facet name="footer">
<h:form id="submit_dashboards_dialog_form">
<p:commandButton id="confirm_button" actionListener="#{dashboardFilterDialogBean.confirm()}" />
</h:form>
</f:facet>
</p:dialog>
Bean
public class DashboardFilterDialogBean {
private DashboardObject dashboardObject;
#PostConstruct
public void init() {
dashboardObject.getTexts().add("");
}
public void confirm() {
for (String txtValue : getDashboardObject().getTexts()) {
if (!txtValue.equals("")) {
...;
}
}
}
public DashboardObject getDashboardObject() {
return dashboardObject;
}
public void setDashboardObject(DashboardObject dashboardObject) {
this.dashboardObject = dashboardObject;
}
}
Object
public class DashboardObject {
private List<String> texts;
public DashboardObject() {
texts = new ArrayList<String>();
}
public List<String> getTexts() {
return texts;
}
public void setTexts(List<String> texts) {
this.texts = texts;
}
}
I've fixed my issue by adding <p:ajax event="blur" process="#this" /> into my inputText so it saves the value before i click confirm. So the end code looks like this:
<p:inputText value="#{dashboardFilterDialogBean.dashboardObject.texts[loop.index]}" />
<p:ajax event="blur" process="#this" />
</p:inputText>
I'm using JSF 2.2 in a web application and I'm having problems in the view when I use f:validateRegex and fails (because when I use immediate="true" and try to navigate to the same page again, the view isn't updated when I have a new Instance of the object in my backing bean). I was thinking richfaces has a bug (because I'm using jsf and richfaces in my main application) so I made a test code with richfaces and without richfaces (only jsf) to identify where is the error, but in both cases the view fails.
Here is my test code without richfaces (Only jsf):
View:
<?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:f="http://xmlns.jcp.org/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<h:head>
<title>Mis pruebas con JSF</title>
</h:head>
<h:body>
<h:form id="lista">
<h:panelGrid id="principal">
<h:dataTable value="#{indexBB.personas}" var="persona">
<h:column>
<f:facet name="header">Activo</f:facet>
<h:selectBooleanCheckbox value="#{persona.activo}"></h:selectBooleanCheckbox>
</h:column>
<h:column>
<f:facet name="header">Nombre</f:facet>
<h:outputText value="#{persona.nombre}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Correo</f:facet>
<h:outputText value="#{persona.correo}"></h:outputText>
</h:column>
</h:dataTable>
<h:commandButton action="#{indexBB.crearPersona}" value="Crear Persona">
</h:commandButton>
<h:commandButton action="#{indexBB.activarBoton}" value="Activar Boton">
</h:commandButton>
</h:panelGrid>
</h:form>
<h:form id="crear">
<h:panelGrid id="secundario" rendered="#{indexBB.crear}">
<h:outputText value="Activo?">
</h:outputText>
<h:selectBooleanCheckbox label="Activo" value="#{indexBB.persona.activo}">
</h:selectBooleanCheckbox>
<br></br>
<h:outputText value="Nombre"></h:outputText>
<h:inputText label="Nombre" value="#{indexBB.persona.nombre}">
</h:inputText>
<br></br>
<h:outputText value="Correo"></h:outputText>
<h:inputText label="Nombre" value="#{indexBB.persona.correo}">
<f:validateRegex
pattern="[\w\.-]*[a-zA-Z0-9_]#[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
</h:inputText>
<br></br>
<h:commandButton action="#{indexBB.guardarPersona}" value="Guardar Persona">
</h:commandButton>
<h:commandButton action="#{indexBB.cancelar}" value="Cancelar" immediate="true">
</h:commandButton>
</h:panelGrid>
</h:form>
</h:body>
</html>
Bean:
package com.kanayet.martin.view.bb;
import com.kanayet.martin.model.entity.Persona;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
#Named(value = "indexBB")
#ViewScoped
public class indexBB implements Serializable {
private Persona persona;
private List<Persona> personas;
private boolean crear;
/**
* Creates a new instance of indexBB
*/
public indexBB() {
}
#PostConstruct
public void onInit(){
personas = new ArrayList<>();
personas.add(new Persona("Martin", "martin#gmail.com", true));
personas.add(new Persona("Andrea", "andrea#gmail.com", true));
personas.add(new Persona("Camilo", "camilo#gmail.com", true));
personas.add(new Persona("Felipe", "felipe#gmail.com", true));
personas.add(new Persona("David", "david#gmail.com", true));
}
public void activarBoton() {
persona = personas.get(0);
}
public void crearPersona(){
crear = true;
persona = new Persona();
}
public void guardarPersona(){
personas.set(0, persona);
}
public void cancelar(){
}
public Persona getPersona() {
return persona;
}
public void setPersona(Persona persona) {
this.persona = persona;
}
public List<Persona> getPersonas() {
return personas;
}
public void setPersonas(List<Persona> personas) {
this.personas = personas;
}
public boolean isCrear() {
return crear;
}
public void setCrear(boolean crear) {
this.crear = crear;
}
}
Model: (Object)
package com.kanayet.martin.model.entity;
public class Persona {
private String nombre;
private String correo;
private Boolean activo;
public Persona() {
}
public Persona(String nombre, String correo, Boolean activo) {
this.nombre = nombre;
this.correo = correo;
this.activo = activo;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getCorreo() {
return correo;
}
public void setCorreo(String correo) {
this.correo = correo;
}
public Boolean getActivo() {
return activo;
}
public void setActivo(Boolean activo) {
this.activo = activo;
}
}
Here is my test code with richfaces: (Bean and Model are the same)
View:
<?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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Mis pruebas con RichFaces</title>
</h:head>
<h:body>
<h:form id="lista">
<a4j:outputPanel id="principal">
<rich:dataTable id="personas" value="#{indexBB.personas}"
var="persona" rows="50">
<rich:column>
<h:selectBooleanCheckbox label="Activo" value="#{persona.activo}">
</h:selectBooleanCheckbox>
</rich:column>
<rich:column>
<h:outputText value="#{persona.nombre}"></h:outputText>
</rich:column>
<rich:column>
<h:outputText value="#{persona.correo}"></h:outputText>
</rich:column>
</rich:dataTable>
<h:commandButton action="#{indexBB.crearPersona}" value="Crear Persona">
</h:commandButton>
<h:commandButton action="#{indexBB.activarBoton}" value="Activar Boton">
</h:commandButton>
</a4j:outputPanel>
</h:form>
<br></br>
<h:form id="crear">
<a4j:outputPanel id="secundario" rendered="#{indexBB.crear}">
<h:outputText value="Activo?">
</h:outputText>
<h:selectBooleanCheckbox label="Activo" value="#{indexBB.persona.activo}">
</h:selectBooleanCheckbox>
<br></br>
<h:outputText value="Nombre"></h:outputText>
<h:inputText label="Nombre" value="#{indexBB.persona.nombre}">
</h:inputText>
<br></br>
<h:outputText value="Correo"></h:outputText>
<h:inputText label="Nombre" value="#{indexBB.persona.correo}">
<f:validateRegex
pattern="[\w\.-]*[a-zA-Z0-9_]#[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
</h:inputText>
<br></br>
<h:commandButton action="#{indexBB.guardarPersona}" value="Guardar Persona">
</h:commandButton>
<h:commandButton action="#{indexBB.cancelar}" value="Cancelar" immediate="true">
</h:commandButton>
</a4j:outputPanel>
</h:form>
</h:body>
</html>
The problem is when I click "Crear Persona" button, I write for example "Nombre": Felix and "Correo": Felix and click "Guardar Persona" button so f:validateRegex fails because isn't a valid email, then click "Cancelar" because my final user doesn't know email required value (immediate="true"). Again, click "Crear Persona" button, (new object in my bean) and jsf page isn't updated, the form should be empty but it isn't, in field "Nombre" stills "Felix" value, but in my bean I have a new and empty object without values in its attributes, do you know why?
The problem is with and without richfaces (because I thought the problem could be richfaces, but it isn't), so I don't know why jsf page isn't updated if I have a new object in my bean, I used netbeans debug tool to verify but I'm right, the object that I see in my bean is different (server side new and empty object) but in my JSF page "Nombre" has "Felix" value and I want to know why it happens, and how I can resolve this problem.
Thank you so much.
The problem is that JSF maintains two representations of your model. There is the Java object, IndexBB, but there is also the component tree, the thing that keeps track of UI state.
When you fail validation, the component tree still contains the values entered. (This is a useful feature so that the user can correct the values.) You've used immediate=true to skip validation, but that doesn't reset the component tree values.
In JSF 2.2, you can use resetValues to reset component tree values:
<h:form id="crear">
<h:panelGrid id="secundario" rendered="#{indexBB.crear}">
<h:outputText value="Activo?">
</h:outputText>
<h:selectBooleanCheckbox label="Activo" value="#{indexBB.persona.activo}">
</h:selectBooleanCheckbox>
<br></br>
<h:outputText value="Nombre"></h:outputText>
<h:inputText id="nombreId" label="Nombre" value="#{indexBB.persona.nombre}">
</h:inputText>
<br></br>
<h:outputText value="Correo"></h:outputText>
<h:inputText id="correoId" label="Nombre" value="#{indexBB.persona.correo}">
<f:validateRegex
pattern="[\w\.-]*[a-zA-Z0-9_]#[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
</h:inputText>
<br></br>
<h:commandButton action="#{indexBB.guardarPersona}" value="Guardar Persona">
</h:commandButton>
<h:commandButton
action="#{indexBB.cancelar}" value="Cancelar">
<f:ajax resetValues="true" render="crear:nombreId crear:correoId"/>
</h:commandButton>
</h:panelGrid>
</h:form>
Changes:
Remove immediate=true.
Add ids to inputs you want to reset.
Add f:ajax to Cancelar button.
Add resetValues property to f:ajax and list your IDs (separate IDs with spaces, not comma).
Make sure your cancelar method actually resets persona -- the code you posted doesn't do this.
If you also want to reset the error messages, add an h:messages to the form, give it an ID, and reset it too.
See also
JSF 2.2: Reset input fields
How to skip validation when a specific button is clicked?
I am Creating a jsf application and I need to perform CRUD. So far I have managed to delete,create,and read but am unable to update the record.So my problem is, I want when the user click the update button a dialog box to pop with the details of the selected row and update the details. Here is My sample code.
<p:panelGrid columns="2">
<h:outputLabel value="Account Id"/>
<h:inputText value="#{accCtr.acc.accountNum}" />
<h:outputLabel value="Account Bal"/>
<h:inputText value="#{accCtr.acc.balance}"/>
<h:outputLabel />
<p:commandButton action="#{accCtr.create()}" value="Enter" update="dt"/>
</p:panelGrid>
<p:dataTable value="#{accCtr.list}" var="i" id="dt" style="width: 40%;" rowStyleClass="height" rowKey="#{accCtr.acc.accountNum}" >
<p:column>
<f:facet name="header">Account Num</f:facet>
#{i.accountNum}
</p:column>
<p:column>
<f:facet name="header">Account Balance</f:facet>
#{i.balance}
</p:column>
<p:column>
<f:facet name="header">Action</f:facet>
<p:commandButton value="Remove" styleClass="height"
action="#{accCtr.removeAccount(i)}"
/>
<p:commandButton value="Edit" styleClass="height"
onclick="pop.show()"
action="#{accCtr.edit(i)}"
>
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<p:dialog widgetVar="pop" header="Account Edit">
<h:form>
<p:panelGrid columns="2">
<h:outputLabel value="Account Balance"/>
<h:inputText value="#{accCtr.acc.balance}"/>
<h:outputLabel/>
<p:commandButton value="Update"/>
</p:panelGrid>
</h:form>
</p:dialog>
can someone help me.
and my backing bean.
#ManagedBean(name="accCtr")
#SessionScoped
public class AccountController {
List<AccountTable> list=new ArrayList<>();
public AccountController() {
}
private Account_dao getDao()
{
return new Account_dao();
}
public List<AccountTable> getList() {
return getDao().findAll();
}
public void setList(List<AccountTable> list) {
this.list = list;
}
public void removeAccount(AccountTable acc) {
getDao().remove(acc);
}
public AccountTable acc=new AccountTable();
public AccountTable getAcc() {
return acc;
}
public void setAcc(AccountTable acc) {
this.acc = acc;
}
public void edit(AccountTable acc) {
setAcc(acc);
}
public String create()
{
this.acc.setUserid(10);
getDao().create(this.acc);
return "index";
}
Change your
<p:commandButton value="Edit" styleClass="height"
onclick="pop.show()"
action="#{accCtr.edit(i)}"
/>
to
<p:commandButton value="Edit" styleClass="height"
oncomplete="pop.show()"
actionListener="#{accCtr.edit(i)}"
process="#this" update=":your_dialog_form_id"
/>
A few things: In general the action atributte is used for navigation (redirect to another page for instance). Also is better to use oncomplete beacuse it gets executed when your ajax request is completed, instead of onclick that fires the action (open the dialog in your case) in the moment when you press the button, skipping validation and such things.
If your problem was, refreshing the dialog content, with the update/process (ajax) mechanism, will be updated with your current selection if you do like the second snippet.
I'm using PrimeFaces 4.0 and Netbeans 6.9.1. I followed primefaces demo here:
DataTable Single Selection
Everything working fine expect button view. Here is my code:
Customer_list.xhtml
<p:growl id="msgs" showDetail="true" />
<h:form id="formTable">
<p:dataTable styleClass="table" id="customers" var="customer" value="#{customerBean.customer}">
<p:column>
<f:facet name="header">First Name</f:facet>
#{customer.firstName}
</p:column>
<p:column>
<f:facet name="header">Last Name</f:facet>
#{customer.lastName}
</p:column>
<p:column>
<f:facet name="header">Email</f:facet>
#{customer.email}
</p:column>
<p:column>
<f:facet name="header">DOB</f:facet>
#{customer.dob}
</p:column>
<p:column style="width:4%">
<p:commandButton id="selectButton" update=":formCreate" oncomplete="dialogCustomerCreate.show()" icon="ui-icon-search" title="Update">
<f:setPropertyActionListener value="#{customer}" target="#{customerBean.selectedCustomer}" />
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<h:form id="formCreate">
<p:dialog header="Create New Customer" widgetVar="dialogCustomerCreate" resizable="false" id="dlgCustomerCreate"
showEffect="fade" hideEffect="explode" modal="true">
<h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">
<h:outputText value="First Name:" />
<h:outputText value="#{customerBean.selectedCustomer.firstName}" style="font-weight:bold"/>
<h:outputText value="Last Name:" />
<h:outputText value="#{customerBean.selectedCustomer.lastName}" style="font-weight:bold"/>
<h:outputText value="Email:" />
<h:outputText value="#{customerBean.selectedCustomer.email}" style="font-weight:bold"/>
<h:outputText value="DOB:" />
<h:outputText value="#{customerBean.selectedCustomer.dob}" style="font-weight:bold"/>
</h:panelGrid>
</p:dialog>
</h:form>
customerBean.java
public class customerBean {
private List<Customer> customer;
private Customer selectedCustomer;
/** Creates a new instance of customerBean */
public customerBean() {
customer = new ArrayList<Customer>();
}
public List<Customer> getCustomer() {
CustomersDao cust_dao = new CustomersDao();
customer = cust_dao.findAll();
return customer;
}
public Customer getSelectedCustomer() {
return selectedCustomer;
}
public void setSelectedCustomer(Customer selectedCustomer) {
this.selectedCustomer = selectedCustomer;
}
}
CustomersDao.java
public class CustomersDao {
public List<Customer> findAll(){
List<Customer> list_cust = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
String sql = "FROM Customer";
try{
session.beginTransaction();
list_cust = session.createQuery(sql).list();
session.beginTransaction().commit();
}catch(Exception e){
session.beginTransaction().rollback();
}
return list_cust;
}
}
Hope anyone suggest me what wrong in my code. It takes me 2 days to solve it. Thanks for reading!
You need to have a converter for your Customer class:
#FacesConverter(forClass = Customer.class)
public class CustomerConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value != null && !value.equals("") && !value.equals("0")) {
//find and return object from DAO
} else {
return null;
}
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
//return object id as string
}
}
1) Try removing your <h:form/>, since you're not submitting any information in that dialog;
2) Try to change your <p:commandButton update=""/> to update=":display";
3) Add process="#form" to your <p:commandButton/>;
4) If that works, i don't think you need that <h:form/>.
you did not mention the correct id for displaying popup dialog at commandButton attribute update.
<p:commandButton id="selectButton" update=":formCreate:display" oncomplete="dialogCustomerCreate.show()" icon="ui-icon-search" title="Update">
<f:setPropertyActionListener value="#{customer}" target="#{customerBean.selectedCustomer}" />
</p:commandButton>
I'm trying to filter a dataTable using Primefaces much like this example. (In a web browser) I type the text I want to filter by, it works once but when I remove the text I've written the result stays the same when it should go back to it's original state.
So it works once and then won't respond. (I can remove or re-type the filter text I've written but it does not affect the table anymore)
Sorry about the weird attribute names in the code, bear with me. :)
xhtml-page:
<h:form>
<p:dataTable var="aggr" value="#{aggregationManagedBean.logiskAdressatModel}"
widgetVar="aggrTable"
emptyMessage="No aggr found with given criteria">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Filter:" />
<p:inputText id="globalFilter" onkeyup="aggrTable.filter()" />
</p:outputPanel>
</f:facet>
<p:column filterBy="#{aggr.name}">
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{aggr.name}" />
</p:column>
</p:dataTable>
</h:form>
backing bean:
#ManagedBean
#SessionScoped
public class AggregationManagedBean {
private List<LogiskAdressat> logiskaAdressater;
private DataModel<LogiskAdressat> logiskAdressatModel;
public AggregationManagedBean() {
logiskaAdressater = getLogiskaAdressater();
logiskAdressatModel = new ListDataModel<LogiskAdressat>(logiskaAdressater);
}
private static List<LogiskAdressat> getLogiskaAdressater(){
List<LogiskAdressat> logiskaAdressater = new ArrayList<LogiskAdressat>();
logiskaAdressater.add(new LogiskAdressat("01 addr_id 01", "Joe"));
logiskaAdressater.add(new LogiskAdressat("02 addr_id 02", "John"));
logiskaAdressater.add(new LogiskAdressat("03 addr_id 03", "Jake"));
return logiskaAdressater;
}
public DataModel<LogiskAdressat> getLogiskAdressatModel() {
return logiskAdressatModel;
}
public void setLogiskAdressatModel(DataModel<LogiskAdressat> adressatModel) {
this.setLogiskAdressatModel(adressatModel);
}
}
Is LogiskAdressat Serializable?
If not, then try making it Serializable -
public class LogiskAdressat implements Serializable {
//....