Hi i know that i can use some facet variables in jsf like this:
<h:dataTable>
<h:column>
<f:facet name="header">
<h:outputText value="Item Description" />
</f:facet>
<h:outputText value="#{item.description}" />
</h:column>
</h:dataTable>
is there any documentation which lists all available facets?
i already looked at the sourcecode of
javax.faces.component.html.HtmlDataTable
but cant find any information of the facets here, where are the defined?
The facets are available by UIComponent#getFacets().
Do note that you need to obtain the particular one in your code snippet from <h:column> (HtmlColumn; UIColumn), not from <h:dataTable> (HtmlDataTable; UIData).
UIData table = getItSomehow();
for (UIComponent child : table.getChildren()) { // child == UIColumn.
Map<String, UIComponent> facets = child.getFacets();
UIComponent headerFacet = facets.get("header");
// ...
}
By the way, looking in source code is funny, but looking in javadoc is the easiest first step. It also lists the methods inherited from superclasses which are otherwise not directly visible in the class' source. You'd immediately have noticed the self-explaining "getFacets()" method when searching for the word "facets" in the javadoc.
Related
I am using a datatable created from a JPA entity.I need to add a column which will be filled with data from another entity, according to the value of the previous column. I could not map the entities as they are in different datasources.
My solution is:
<h:dataTable value="#{entity1ManagedBean.mydatalist}">
<h:column id="c1">
<f:facet name="header">
<h:outputText id="o1" value="Value1"></h:outputText>
</f:facet>
<h:outputText id="value1" value="#{temp.value1}"></h:outputText>
</h:column>
<h:column id="c2">
<f:facet name="header">
<h:outputText id="o2" value="Value2"></h:outputText>
</f:facet>
<h:outputText id="Value2" value="#{entity2ManagedBean.MapValuesMethod(temp.value1)}"></h:outputText>
</h:column>
but when i run it i get a javax.el.MethodNotFoundException: Method not found: class entity2ManagedBean.MapValuesMethod(java.lang.String)
When i remove the temp.value1 input from <h:outputText id="Value2" value="#{entity2ManagedBean.MapValuesMethod(temp.value1)}"></h:outputText> and test my MapValuesMethod by setting a String input in the method, it works fine.
So i think the problem is i am not passing the temp.value1 string parameter to the managed bean method. Is this possible somehow?
EDIT:
JSF v2.2, eclipselink, web logic 12c
Managed Bean method:
public String getMapValuesMethod(String value1){
//value1= "some string" (testing method)
return sessionFacade.sessionBeanqueryMethod(value1);
}
and the session bean method is a simple query.
as I said, when I test the managed bean method by setting value1="some stringvalue" the method returns correct results from the session bean query.
Weblogic 12.2.1.2 supports EL 2.x.--> documentation (https://docs.oracle.com/middleware/1221/wls/NOTES/whatsnew.htm#NOTES558). I changed my ManagedBean function from public String MapValuesMethod(String value1) to public String getMapValuesMethod(String value1) and my .xhtml code from value="#{entity2ManagedBean.MapValuesMethod(temp.value1)}" to value="#{entity2ManagedBean.getMapValuesMethod(temp.value1)}" which solved the problem.
I'm making a datatable where you can add exrta rows. In this extra rows, there's a inputtext. After putting something in the inputtext, this value should be saved to a list. But it's not. I tried in the method below using Sting as a parameter, but then to a list was saved : "javax.faces.component.html.HtmlInputText#23sdf" and thing like that. When I changed method to javax.faces.component.html.HtmlInputText method below returns null.
here's code of table:
<h:dataTable value="#{correct.newPowody}" var="z" binding="#{table}" >
<h:inputText binding="#{custom}" value="#{z}">
<f:ajax event="blur" listener="#{correct.newPowodyAdder(table.rowIndex, custom)}"
execute="#this" render="#form"/>
</h:inputText>
</h:dataTable>
here is my correct bean code:
public void newPowodyAdder(int rowIndex, javax.faces.component.html.HtmlInputText powod) {
String value = powod.getValue().toString();
this.newPowody.set(rowIndex, value);
}
}
this method just should add data from inputtext into a list. Thank you for help
This code really isn't making any sense. It's an overcomplicated attempt to fix the simple problem of String class being immutable. Don't use <h:inputText value="#{string}". Just use brace notation [] in EL to reference a list item by index like so value="#{bean.strings[index]}":
<h:dataTable binding="#{table}" value="#{correct.newPowody}">
<h:column>
<h:inputText value="#{correct.newPowody[table.rowIndex]}" />
</h:column>
</h:dataTable>
That's all. No ajax listener method mess necessary.
See also:
Using <ui:repeat><h:inputText> on a List<String> doesn't update model values
I have a list of users that I want to display in a JSF page using the primefaces p:datatable. I have no exception, but I can't display the data that a have into the list,even the column titles are not shown, in fact, I user the 'getUserListe()'method to get the data from the database calling my service layer, to be sure of that, I added a sysprint() of some userDomain properties (my model class), when I access to the URL, I want to populate my datatable with the data from the UserListe, so I call it by value="#{userMB.userListe}", the problem is that the list is not displayed. but when the page is charged, with means that getUserListe() is called, in my console, the proprties are printed successfullty, someone has any idea about this ?? :
a part of my managed bean :
#ManagedBean(name="userMB")
#RequestScoped
public class UserManagedBean implements Serializable {
private List<UserDomain> userListe ;
public List<UserDomain> getUserListe() {
this.userListe = new ArrayList<UserDomain>();
this.userListe.addAll(getUserServicee().getAll());
for(UserDomain u : userListe)
System.out.println(u.getFirstName());
return userListe;
}
public void setUserListe(List<UserDomain> userListe) {
this.userListe = userListe;
}
part of my jsf.xhtml file:
<h:form>
<h:outputText value="USER's rights list: "></h:outputText>
<p:dataTable id="userss" var="userr" value="#{userMB.userListe}" style="width: 10%">
<h:column>
<f:facet name="header"><h:outputText value="FirstName" /></f:facet>
<h:outputText value="#{userr.firstName}" />
</h:column>
<h:column>
<f:facet name="header"><h:outputText value="LastName" /></f:facet>
<h:outputText value="#{userr.lastName}" />
</h:column>
<h:column>
<f:facet name="header"><h:outputText value="Action" /></f:facet>
<h:commandLink value="Edit" action="#{userMB.editAction(userr)}" />
</h:column>
</p:dataTable>
<h:commandButton value="OK" action="#{userMB.userProp}" />
</h:form>
Note that my user has all the getters and setters of his properties setted correctly,
thank you for help
I did as #luiggi said in his comment by changing the <h:column> by the <p:column> and it's work fine, so thank you.
I also changed my managed bean from an #RequestScoped to an #ViewScoped one.
the other thing that I want to add and share with you , is that I did some research after reading the comments of this post, so I found an other solution that seems better than others to load the list only one time, It's by attaching a listener to a system event, so in my jsf file I add
<f:view>
<f:event type="preRenderView" listener="#{userMB.loadUserListe}" />
...
the loadUserListe() method in my bean call the business logic code which instantiate the list of users.
Of course, the jsf page is enclosed in an f:view. Note also that the use of this listener requires at least the JSF 2.0
thank you all for your help, and excuse me for my english, I hope this response helps others
I have a dataTable that lists some objects and I want to set a property for those objects using a selectOneListbox. This is my dataTable
<h:dataTable value="#{someHandler.entities}"
binding="#{someHandler.dataTable}" var="entitiy">
<h:column>
<f:facet name="header">
<t:outputText value="Level" />
</f:facet>
<h:selectOneListbox id="level" value="#{entitiy.level}" size="1"
valueChangeListener="#{someHandler.changeLevel}"
onchange="submit()">
<f:selectItem itemValue="-" itemLabel="-" />
<f:selectItem itemValue="ALL" itemLabel="ALL" />
(and so on)
</h:selectOneListbox>
</h:column>
<h:column>
<f:facet name="header">
<t:outputText value="Name" />
</f:facet>
<h:outputText value="#{entitiy.name}" />
</h:column>
</h:dataTable>
The valueChangeListener looks like this:
public void changeLevel(ValueChangeEvent event) {
String newLevel = (String) event.getNewValue();
Logger logger = (Logger) dataTable.getRowData();
logger.setLevel(Level.toLevel(newLevel));
}
(dataTable is an HtmlDataTable object.)
However, the event object is always the same - no matter which row the selectOneListbox was in. (It seems always the logger in the first row). The Logger object I get is also not the one I want.
Any ideas? Thank you!
And anothers questions? Is the entitiy.setLevel() method called even though I have a valueChangeListener? I use entitiy.level because I want to show the chosen level as a default for those entity.
Thank you!
There are two potential problems here:
First, the onchange="submit()" submits the entire form. The valueChangeListener will be invoked on all input elements of which the submitted value differs from the initial value.
You need to preset the value behind #{entitiy.level} with the same value as the default menu option. E.g. in the constructor.
public Entity() {
level = "-";
}
Or, better, make the default value null.
<f:selectItem itemValue="#{null}" itemLabel="-" />
so that the valueChangeListener won't be invoked when the particular menu is not changed.
Or, when you are already on JSF 2.x (please always mention exact JSF impl/version in your JSF questions), you can use <f:ajax> tag for this without the need for a valueChangeListener with a hacky onchange="submit()".
Second, you need to ensure that the datatable value #{someHandler.entities} returns exactly the same list during the submit as it was during the initial request. So, do the data loading in the bean (post)constructor. In JSF 2.x you'd like to put the bean in the view scope as well.
Unrelated to the concrete problem, you can also just use <h:selectOneMenu> instead of a <h:selectOneListbox size="1">.
I have a JSF datatable with a bunch of rows, with each row having a selectOneMenu inside of it like this:
<h:form
<h:dataTable id="myTable"
binding="#{myBean.dataTable}"
value="#{myBean.dataTableRows}" var="row"
first="0" rows="0" dir="LTR" frame="hsides" rules="all">
<h:column>
<f:facet name="header">
<h:outputText value="Sample Name" />
</f:facet>
<h:outputText value="#{row.sampleName}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Role" />
</f:facet>
<h:selectOneMenu value="#{row.role}"
id="roleInput">
<f:selectItems value="#{myBean.allRoles}" />
</h:selectOneMenu>
</h:column>
</h:dataTable>
<h:commandButton value="Save" action="#{myBean.save}" />
</h:form>
However, I can't seem to figure out how get the selected role out of each row in the save method. In other words, I want to save each row's value. I saw this article, which explains how to save an input text box:
http://balusc.blogspot.com/2006/06/using-datatables.html#EditableDatatable
but it doesn't seem to apply to the h:selectOneMenu case. Does anyone have any example code that does this?
Thanks!
I see your table has binding to your bean. In your bean you can use the getDataTable() method and access it. Java doc says:
public Object getRowData()
Return the data object representing the data for the currently selected row index, if any.
So if you do your code like:
List<String> selectedRowData = (List<String>) getDataTable().getRowData()
You can then access all the fields the user has chosen. Im using this in my own project and its working. The only difference is that Im casting to my own type instead of List<String>
There are no obvious errors in the form - if your save method is not being invoked, try adding a messages tag to your form to help track down the source of the problem. It would help if you posted a sample bean that reproduces the problem and state the JSF implementation and version you are using.