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.
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 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 have a rich:dataTable and like to show details to each row in a rich:popupPanel when the user clicks on the detail button.
I do this like this
<h:panelGrid columns="3" columnClasses="titleCell">
<h:form id="form">
<rich:dataScroller for="table" maxPages="5" />
<rich:dataTable value="#{tournSelectionBean.tournaments}"
var="tourn" id="table" rows="10">
<rich:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{tourn.name}" />
</rich:column>
<rich:column>
<a4j:commandButton value="Detail"
action="#{tournSelectionBean.setCurrentTournament(tourn)}"
render=":detailpopup"
oncomplete="#{rich:component('detailpopup')}.show();" />
</rich:column>
</rich:dataTable>
<rich:dataScroller for="table" maxPages="5" />
</h:form>
</h:panelGrid>
<rich:popupPanel id="detailpopup" modal="true" resizeable="false"
autosized="true">
<f:facet name="header">
<h:outputText value="#{tournSelectionBean.currentTournament.name}" />
</f:facet>
<f:facet name="controls">
<h:outputLink value="#"
onclick="#{rich:component('detailpopup')}.hide(); return false;">
</h:outputLink>
</f:facet>
<h:panelGrid columns="2" columnClasses="titleCell">
<h:outputLabel value="City" />
<h:outputLabel
value="#{tournSelectionBean.currentTournament.city}" />
</h:panelGrid>
Close
</rich:popupPanel>
The setPropertyActionListener sets the ID correctly and the popup opens as expected. But the popup shows the details of the tournament that was in the bean when the view was created (and not the one that was set by the propertyactionlistner).
How can I achieve this?
EDIT: Updated above code and added Bean:
#Named("tournSelectionBean")
#ViewScoped
public class TournamentSelectionBean implements Serializable {
#EJB
private TournamentControllerInterface tournamentController;
private List<Tournament> tournaments;
private Tournament currentTournament;
#PostConstruct
public void init() {
tournaments = tournamentController.loadTournaments(true, false);
}
/**
* #return the tournaments
*/
public List<Tournament> getTournaments() {
return tournaments;
}
/**
* #param tournaments
* the tournaments to set
*/
public void setTournaments(List<Tournament> tournaments) {
this.tournaments = tournaments;
}
/**
* #return the currentTournament
*/
public Tournament getCurrentTournament() {
return currentTournament;
}
/**
* #param currentTournament the currentTournament to set
*/
public void setCurrentTournament(Tournament currentTournament) {
this.currentTournament = currentTournament;
}
}
You need to tell JSF to re-render the popup html before you open it:
<a4j:commandButton value="Detail"
action="#{tournSelectionBean.setCurrentTournament(tourn)}"
render=":detailpopup"
oncomplete="#{rich:component('detailpopup')}.show();" />
Be careful: if you have a <h:form> inside the popupPanel, you may have issues when you re-render the complete popup. Put everything in a <h:panelGroup> inside the form, then re-render that one.
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" />
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 {
//....