How to update the property member variable of a managed bean? - java

I have a member variable in a managed bean, and this member variable is tied to the component in XHTML with a getter and setter. If I set the member variable in a function call, when the getter of this member variable is trigger, this member variable will still hold the old value. May I know how could I update this member variable so that the component could get the latest updated value?
The manage bean:
#ManagedBean(name = "myBean")
#SessionScoped
public class MyBean {
public boolean show = false;
/** getter and setter **/
public void theFunc() {
this.show = true;
}
}
XHTML code
<h:panelGroup id="Panel_1" rendered="#{myBean.show == true}">
...
Some rubbish here
...
</h:panelGroup>
<h:panelGroup id="Panel_2">
<h:commandLink action="#{myBean.doFunc}">
<f:ajax event="action" render="Panel_1"/>
<h:outputText value="XX" />
</h:commandLink>
</h:panelGroup>
From this sample, the show variable is showing false even though theFunc() has already set to true.
Update on 06 Oct 2012
I have remove commandButton and replace with commandLink, I think it should be fine in term of usage.

Change your methods return type and name if you want it to be invoked.
Change this
public void theFunc() {
this.show = true;
}
to this
public String doFunc() {
this.show = true;
return null;
}
otherwise this action can not work.
<h:commandLink action="#{myBean.doFunc}">
then update parent of your panel like this
<h:panelGroup id="parentPanelGroupId">
<h:panelGroup id="Panel_1" rendered="#{myBean.show}">
...
Some rubbish here
...
</h:panelGroup>
</h:panelGroup>
<h:panelGroup id="Panel_2">
<h:commandLink action="#{myBean.doFunc}">
<f:ajax render="parentPanelGroupId"/>
<h:outputText value="SHOW/HIDE PANEL 1" />
</h:commandLink>
</h:panelGroup>
NOTE:
use
rendered="#{myBean.show}"
instead of
rendered="#{myBean.show == true}"

Try this way:
<h:panelGroup id="toUpdate">
<h:panelGroup id="Panel_1" rendered="#{myBean.show}">
...
</h:panelGroup>
</h:panelGroup>
<h:commandButton action="#{theBean.doCalculation}">
<f:ajax render="toUpdate" />
</h:commandButton>
There are 2 things to note:
You need to wrap <h:panelGroup id="Panel_1"> inside another <h:panelGroup> or something eles which is always rendered. Otherwise, if the show variable is initially false, the ajax update may not work since JSF renderer cannot find the the component with id="Panel_1" when you use <f:ajax render="Panel_1" />.
rendered="#{myBean.show}" is good enough :P. Since show is a boolean variable, you don't need rendered="#{myBean.show == true}".

Related

JSF page not reloading on form action

Please read to the end, there are many EDITS I have this piece of JSF code:
<h:form>
<h:dataTable class="table-striped" var="_product"
value="#{productManager.products}"
border="1"
binding="#{productManager.table}">
<h:column>
<f:facet name="header">Product</f:facet>
#{_product.name}
</h:column>
<h:column>
<f:facet name="header">Available Units</f:facet>
#{_product.stock}
</h:column>
<h:column>
<f:facet name="header">Category</f:facet>
#{_product.category}
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
#{_product.price}
</h:column>
<h:column>
<f:facet name="header">Description</f:facet>
#{_product.description}
</h:column>
<h:column>
<f:facet name="header">Select</f:facet>
<h:commandButton class="btn btn-primary" value="Select"
action="#{productManager.selectProduct}"/>
</h:column>
</h:dataTable>
</h:form>
<h:form>
<h:outputLabel for="productName">Selected Product: </h:outputLabel>
<h:inputText value="#{productManager.selectedDesiredCategory}"/>
<h:commandButton value="Filter category" action="#{productManager.filterProductsByCategory}"/>
<h:outputText id="productName" value="#{productManager.selectedName}"/><br/>
<h:outputLabel for="units">Units: </h:outputLabel>
<h:inputText id="units" value="#{productManager.selectedUnits}"/>
<h:commandButton value="Add to basket" action="#{productManager.addToBasket(accountManager.username)}"/><br/>
<h:outputText rendered="#{productManager.availableMessages}"
value="#{productManager.message}"/>
</h:form>
The #{productManager.filterProductsByCategory} commandbutton redirects to this java method:
public void filterProductsByCategory() {
this.products = controller.obtainProductListByCategory(selectedDesiredCategory);
showMessage("Filtered by selected category");
}
Here I update the content of the products list with the new set of filtered-by-category products to display them in the view. The thing is the page is not reloading to display the new content. How is this achieved?
EDIT: The showMessage method is actually displaying in the view, so the page IS reloading, but for some reason the table is not updating. Maybe it's a problem with the data the query is returning, I'm actually researching.
EDIT: The query is returning good results, as my debugging process confirmed, but the webpage is not reloading the data properly in the table.
EDIT: I found out something really weird. This is the code the JSF page is referencing to:
public void filterProductsByCategory()
{
filtered = true;
products = controller.obtainProductListByCategory(selectedDesiredCategory);
showMessage("Filtered by selected category");
}
I'm now using a boolean value to actually know when I have to deliver a filtered list (See why in the code below) This is the getter of the products list:
public List<Product> getProducts()
{
if(filtered)
{
filtered = false;
return products;
}
products = controller.obtainProductList();
return products;
}
Here if it's true it should just send the actual filtered products variable. But for some reason it's looping again and again inside the method (even after the return statement inside the if) and sending all the products to the view again. Why is this even happening?
By default, JSF calls the getter methods as much as they're used in the view. For example, for your List<Product> products field and its respective getter, if #{productManager.products appears twice in your view i.e. in the Facelets code, then the getter will be executed twice as well. For this reason, getter and setter methods in managed bean should be as clean as possible and should not contain any business logic involved.
Basically, you should retrieve the product list from database once, after creating the managed bean and before the view render time. To achieve this, you can use #PostConstruct annotation to decorate a void method that will be executed after the bean is created.
In code:
#ManagedBean
#ViewScoped
public class ProductManager {
private List<Product> products;
#PostConstruct
public void init() { //you can change the method name
//if you manually handle the controller, initialize it here
//otherwise, let it be injected by EJB, Spring, CDI
//or whichever framework you're working with
products = controller.obtainProductList();
}
public List<Product> getProducts() {
//plain getter, as simple as this
//no business logic AT ALL
return this.products;
}
public void filterProductsByCategory() {
filtered = true;
products = controller.obtainProductListByCategory(selectedDesiredCategory);
//I guess this method logs a message or something...
showMessage("Filtered by selected category");
}
}
More info
Why JSF calls getters multiple times
Why use #PostConstruct?

Difference between bean method with AjaxBehaviorEvent and bean method without AjaxBehaviorEvent?

I have my XHTML like this
<h:form id="form">
<h:panelGrid columns="3">
<h:outputText value="Keyup: " />
<p:inputText id="counter">
<p:ajax event="keyup" update="out"
listener="#{counterBean.increment}" />
</p:inputText>
<h:outputText id="out" value="#{counterBean.count}" />
</h:panelGrid>
</h:form>
Case I : ajax listener method with AjaxBehaviorEvent
public void increment(AjaxBehaviorEvent event) {
count++;
}
Case II : without AjaxBehaviorEvent
public void increment() {
count++;
}
In both the cases listener will be invoked and does the counter job to increase count on keyup. So, When exactly I need to use AjaxBehaviorEvent and when I don't need to use?
You can bind multiple ajax events to the same method and use getSource() of AjaxBehaviorEvent to know which component trigged the event.

how can I call a method from a subtable from Primefaces?

I am using primefaces for not so long and Ive found that I cant use a <p:commandButton /> because it just can't reach the method, the method is ok, I tried it out of the table (and the subtable) and it works perfectly there (everything is inside a form) , the problem is that I need the user to be able to select all the subtable, so, I thought maybe with a button that could be possible, but seems like subtable doesn't allow that, any other way I can do this? or maybe I have to use another way for call my method from a subtable, anybody knows about it?
Thanks
some of my code
<h:form>
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
<p:dataTable id="case" var="ticket" value="#{CaseBean.selectedCase.tickets}">
<p:columnGroup>
<p:row>
<p:column> Action:</p:column>
<p:column>
<!-- This doesn't work, removed. -->
<p:commandButton value="Aprove" action="#{CaseBean.acept()}">
</p:commandButton>
</p:column>
</p:row>
</p:columnGroup>
<p:subTable var="detail" value="#{ticket.detail}">
<f:facet name="header">
Resume:
</f:facet>
<!-- some data... -->
<p:column>
<!-- doesn't work either -->
<p:commandButton value="Aprove" action="#{CaseBean.aceptTicket()}">
</p:commandButton>
</p:column>
<!-- show my data -->
The table works perfectly, it shows all the data, the log files doesn't show any error, so, when I tried to write my commandButton out of the table it worked perfectly, if I cant write it inside a subtable its ok, but , how could I write it in the table? it doesn't show up there either.
you welcome :)
But if i was you I wouldn't use subtables, Ill think for another solution..maybe Ill do it this way, ill use two different data tables, the first contains the parent list and the second one contains the child list elements, and every selection made triggers an update of the second table...I tried it on my IDE and it works just fine
<h:form id="form">
<p:dataTable var="cas" value="#{beanCase.myListOfCase}"
selection="#{beanCase.selectedCase}" rowKey="#{cas.idCase}"
selectionMode="single">
<p:ajax event="rowSelect" update=":form:TicketTable" />
<p:column headerText="Id Case">
<h:outputText value="#{cas.idCase}" />
</p:column>
<p:column headerText="Case Name ">
<h:outputText value="#{cas.caseName}" />
</p:column>
<p:column headerText="Case Detail">
<h:outputText value="#{cas.caseDetail}" />
</p:column>
<p:column headerText="Action">
<p:commandButton value="Accept Case" update=":form:TicketTable"></p:commandButton>
</p:column>
</p:dataTable>
<p:dataTable id="TicketTable" var="ticket"
value="#{beanCase.selectedCase.tickets}">
<p:column headerText="Ticket Number">
<h:outputText value="#{ticket.idTicket}" />
</p:column>
<p:column headerText="Ticket Details">
<h:outputText value="#{ticket.labelTicket}" />
</p:column>
<p:column headerText="show">
<h:outputText value="#{ticket.show}" />
</p:column>
<p:column headerText="this show is brought to you by">
<h:outputText value="#{ticket.sponsor}" />
</p:column>
<p:column headerText="Make a Reservation">
<p:commandButton value="Buy" action="#{beanCase.buyTicket()}">
<f:setPropertyActionListener value="#{ticket}"
target="#{beanCase.selectedTicket}" />
</p:commandButton>
</p:column>
</p:dataTable>
before that you must create the data model classes for the Case and ticket
public class CaseDataModel extends ListDataModel<Case> implements
SelectableDataModel<Case> {
CaseDAO caseDAO = new CaseDAO();
public CaseDataModel() {
}
public CaseDataModel(List<Case> cases) {
super(cases);
}
#Override
public Case getRowData(String arg0) {
List<Case> listOfMyObjet = (List<Case>) caseDAO.findAll();
for (Case obj : listOfMyObjet) {
if (String.valueOf(obj.getIdCase()).equals(arg0))
;
return obj;
}
return null;
}
#Override
public String getRowKey(Case arg0) {
return String.valueOf(arg0.getIdCase());
}
}
The first columnGroup is not rendered because in your first row the number of columns is 2, one for "action" and the other for the commandButton while in your subtable you just used two rows one for "Resume" and other contains only one Column for the other commandButton.
The number of columns should be the same in every row, so you must use colspan or rowspan to make sure of that.
As for the rest using a DataTable will do the job, I didn't understand what you wanna do exactly but I all assume that you want to select myObject from displayed list of objects within a dataTable. So in order to achieve that, the UidataTable must return an object to the backed Bean.
public class myObjectDataModel extends ListDataModel<myObject> implements SelectableDataModel<myObject> {
public myObjectDataModel() {
}
public myObjectDataModel(List<myObject> data) {
super(data);
}
#Override
public myObject getRowData(String rowKey) {
List<myObject> listOfMyObjet = (List<myObject>) yourDao.getListOfmyOjects();//get your list
for(myObject obj : listOfMyObjet) {
if(obj.getIdObject().equals(rowKey))
return obj;
}
return null;
}
#Override
public Object getRowKey(myObject obj) {
return obj.getIdObject();
}
}
The backed bean:
public class tableBean {
private List<myObject> _Objects;
private myObjectDataModel myListOfObjects;
private myObject selectedObject;
//getters and setters
public tableBean(){
myObjectDataModel = new myObjectDataModel(_Objects);
}
//...
}
xhtml:
<p:dataTable id="table" var="case"
value="#{tableBean.myObjectDataModel}"
selection="#{tableBean.selectedObject}" selectionMode="single"
rowKey="#{case.IdObject}">
<p:column>
<p:commandButton value="
Aprove" action="#{tableBean.someMethod()}">
</p:commandButton>
</p:column>
</p:dataTable>
and make sure to use Ajax — commandButton update attribute, or <p:ajax> — to refresh your UI.
Try removing the p:columnGroup from your JSF page. You don't need it for this (and this might be the cause of your problem). Think of it like this: a table exists of rows and rows exist of columns. ;-)
The #{CaseBean} has got to be in the view scope in order to get this to work, or if you want to keep it request scoped, the #{CaseBean.selectedCase.tickets} has to prepared in the (post)constructor on some request parameters so that it's exactly the same as it was during displaying the table.
When the form is submitted, JSF will namely reiterate over the table in order to find the command component responsible for the action. However, if the bean is request scoped and the value behind #{CaseBean.selectedCase} or #{CaseBean.selectedCase.tickets} is not the same as it was during displaying the table, then JSF won't be able to identify the button which invoked the action.
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated - point 4 applies to you

f:viewParam with Converter and ViewScoped exception out when invoke the second ajax request

I have this f:viewParam that I try to bind validate and convert a userId into Player, and I got an unexpected results.
<f:metadata>
<f:viewParam name="userId" value="#{myBean.selectedPlayer}" converter="pConverter"
converterMessage="Bad Request. Unknown User" required="true"
requiredMessage="Bad Request. Please use a link from within the system" />
</f:metadata>
<h:body>
<p:messages id="msgs"/>
<h:form>
<ul>
<li>Harry</li>
<li>Tom</li>
<li>Peter</li>
</ul>
</h:form>
<h:form>
<h:panelGrid columns="2" rendered="#{not empty myBean.selectedPlayer}">
<h:outputText value="Id: #{myBean.selectedPlayer.id}"/>
<h:outputText value="Name: #{myBean.selectedPlayer.name}"/>
</h:panelGrid>
</h:form>
<h:form id="testForm">
<h:inputText value="#{myBean.text}"/>
<p:commandButton value="Switch" update=":msgs testForm"/>
<h:outputText value="#{myBean.text}" rendered="#{not empty myBean.text}"/>
</h:form>
</h:body>
My Converter look like this
#FacesConverter(value="pConverter")
public class PConverter implements Converter {
private static final List<Player> playerList;
static{
playerList = new ArrayList<Player>();
playerList.add(new Player(1, "Harry"));
playerList.add(new Player(2, "Tom"));
playerList.add(new Player(3, "Peter"));
}
#Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if(value == null || !value.matches("\\d+")){
return null;
}
long id = Long.parseLong(value);
for(Player p : playerList){
if(p.getId() == id){
return p;
}
}
throw new ConverterException(new FacesMessage("Unknown userId: " + value));
}
#Override
public String getAsString(FacesContext fc, UIComponent uic, Object value) {
if(!(value instanceof Player) || value == null){
return null;
}
return String.valueOf(((Player)value).getId());
}
}
As I click the three link (Harry, Tom, Peter), the converter work great. It converter the id and bind the player back to my managed bean. I then type something in the text box, then click Switch, the first time it work fine, what I typed appear next to the button, but then I change what I type, and click Switch again, then error message appear Bad Request. Please use a link from within the system, which is the error message for required for f:viewParam. If I took the f:viewParam out then everything work fine. Surprisingly, if I switch from f:viewParam to o:viewParam (OmniFaces), then it work great.
That's because the <f:viewParam> runs on every single HTTP request, also on postbacks. It works in your case fine for plain GET links, because you're passing exactly that parameter in the links. It fails in your case for POST forms, because you aren't passing that parameter in the button. So it becomes null in the request parameter map and the required validator kicks in and hence this validation error.
To keep the <f:viewParam required="true"> happy on POST forms as well, you basically need to retain the initial request parameter by <f:param> in the command buttons/links.
<p:commandButton value="Switch" update=":msgs testForm">
<f:param name="userId" value="#{param.userId}" />
</p:commandButton>
The OmniFaces <o:viewParam>, which is designed to be used in combination with view scoped beans, has an additional check in the isRequired() getter (source code here):
#Override
public boolean isRequired() {
// The request parameter get lost on postbacks, however it's already present in the view scoped bean.
// So we can safely skip the required validation on postbacks.
return !FacesContext.getCurrentInstance().isPostback() && super.isRequired();
}
So, this skips the required validator on every postback (and additionally, it also skips setting the model value on every postback due to its stateless nature). That's why you don't see the validation error and you still have the proper model value (which isn't reset on every postback).

JSF2 "f:ajax render" renders more than the specified id

I've searched and tried various things from around here and the web; prependId, using full id paths (:form:panelid:componentid kinda thing), and others.
I'm still confused. I'm still a JSF noob :)
My problem is that whatever I specify in the f:ajax render part isn't the only parts the get "executed". What I mean is, the id specified in render won't get rerendered on screen, but the value of that component do get called.
So:
inputText's f:ajax render=outA event=blur
when blur happens, outA's getter is called and it rerendered, but also the getter of other components are called, but they're not rerendered.
Specifically, in light of the listed code below:
A) When val1 loses focus, it's blur event fires, which has it call getValue1() and rerender outval1. Happy. But, getStuff() is ALSO called (it happens for the other blur and btnCalc also) BUT the result from getStuff() is NOT rendered to tblStuff dataTable.
Why?
How can I fix it, so that on the blur event, only the getters relevent to the render=".." component is executed?
Should I maybe use different sections?
B) when the refresh button is clicked, then it will call getStuff AGAIN, and now show the new dataTable / ie that new data with ALL THE MANY ValueTO's that was added during the blur events and the btnReload click event.
C) For any one event named in A, the getStuff method is called exactly 8 times. ie, click inside inputbox, click outside input box, getStuff() * 8.
Yet, getValue1 is called only twice?
D) Any good JSF2 books out there?
Here is the 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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Stuff</title>
</h:head>
<h:body>
<h:form id="frmTrxMain" prependId="true">
<h1>
Stuff
</h1>
<h:dataTable title="Stuff" id="tblStuff" var="s" value="#{bean.stuff}" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet>
<h:outputText value="#{s.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{s.name}" />
</h:column>
</h:dataTable>
<h:outputText value="No stuff to display" rendered="#{empty bean.stuff}"/>
<h:commandButton value="Refresh" id="btnReload">
<f:ajax render="tblStuff" event="click"/>
</h:commandButton>
<hr/>
<h:panelGrid columns="3" id="pnlTwo">
<h:outputLabel value="Value1"/>
<h:inputText value="#{bean.value1}" id="val1">
<f:ajax event="blur" render="outVal1"/>
</h:inputText>
<h:outputText id="outVal1" value="Entered: #{bean.value1}" />
<h:outputLabel value="Value2"/>
<h:inputText value="#{bean.value2}" id="val2">
<f:ajax event="blur" render="outVal2"/>
</h:inputText>
<h:outputText id="outVal2" value="Entered: #{bean.value2}" />
<h:commandButton value="Calc" id="btnCalc">
<f:ajax event="click" render="outSum"/>
</h:commandButton>
</h:panelGrid>
<h:outputLabel id="outSum" value="#{bean.sum}"/>
</h:form>
</h:body>
</html>
And the backing bean:
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name = "bean")
#SessionScoped
public class TestStuffBean {
private int id=1;
private String name="a";
//test.xhtml
private String value1;
private String value2;
private List<TestVO> stuff = new ArrayList<TestVO>();
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
public String getSum() {
String result = "";
int sum;
try {
sum = Integer.parseInt(value1) + Integer.parseInt(value2);
result = Integer.toString(sum);
} catch (NumberFormatException e) {
result = "Enter two integers";
}
return result;
}
public List<TestVO> getStuff() {
//the VO is just a POJO with a contructor that takes
//two strings and a getter/setter for each id and name
stuff.add(new TestVO(Integer.toString(id), name+id));
id++;
return stuff;
}
}
I'm not a JSF 2 expert since we're still using 1.2 but I guess they have the same properties here: the getters are called often, e.g. when reconstrucing the view in the restore-view phase. Thus you can't rely on the getters only being called for the actually rendered part. In fact, you shouldn't do any costly operations (like db access) in your getters - in most cases you don't know how often they're actually called in one cycle.
Especially your getStuff() method should be changed, it's also almost always bad design to have a getter cause side effects like something being added to an internal list, like in this case.
Edit: To clarify the comment on getters:
Normally a getter should not do costly operations, since you don't know how often JSF will call them.
Since you still have to get some data from some source (like a database) you might provide methods to only do that (like loadDataFromDB()) and fill some data structure in the managed bean. The getter might then just deliver that data structure.
You might add a check for the data structure not being initialized and if not call the load method in the getter, but I'd only use that as a last option. Generally you should be able to separately call the load method, e.g. by clicking a refresh button or when navigating to the page - that's what we do.

Categories