Concatenation of property name (EL) in JSF - jsf

How can I concatenate the name of a property using the EL?
This is what I tried:
<ui:repeat value="#{someBean.getParts()}" var="part">
<h:inputTextarea value="#{someOtherBean.result}#{part}" />
</ui:repeat>
But it didn't work.
The bean has the four property resultA, resultB, resultC and resultD. getParts() returns "A", "B", "C", and "D".

It's quite possible though. You can use <ui:param> to prepare the dynamic property name and use the brace notation [] to access it.
<ui:repeat value="#{someBean.parts}" var="part">
<ui:param name="resultPart" value="result#{part}" />
<h:inputTextarea value="#{someOtherBean[resultPart]}" />
</ui:repeat>
Needless to say that I agree with Michael that this is a smell in the model design.

I don't think that can be made to work without changing the design. It's generally a bad idea in Java to have a design that requires you to access methods fields and properties through a name, and worse if the name is built from strings.
Possible solutions:
have getParts() return "resultA", "resultB", etc. and access them #{someOtherBean[getParts()]}
change the property names to a, b, c, d and access them as #{someOtherBean[getParts()]}
have a single property result that contains a Map with "A", "B", etc as keys and access the values as #{someOtherBean.result[getParts()]}

Related

Changing a result from p:autoComplete's completeMethod before using it

I'm implementing a dialog for registering an address. In the street name field, I'm using PrimeFaces' <p:autoComplete> tag to help the user find the name of their street. The completeMethod works as expected, but to avoid confusion between similar street names I would like the drop-down list to also include the municipality the given street is in, for instance on the format "<street name>, <municipality name>".
I don't want the municipality name to be included in the actual field, so I've concluded that I need a method that performs some sort of string manipulation (substring using the position of the first comma, for instance), but I can't figure out where such a method would be called from. I've had a look through the PrimeFaces documentation, but I haven't been able to find anything that would allow me to do this. Is this at all possible in <p:autoComplete>? Alternatively, is there another autocomplete implementation which supports this, or would I have to implement my own javascript component?
EDIT: This is what the xhtml code I'm using looks like:
<div class="form-group row required">
<h:outputLabel value="#{msgs['#common.mailingAddress']}" for="address" styleClass="col-xs-12"/>
<p:autoComplete
id="address"
name="address"
size="50"
maxlength="50"
styleClass="col-xs-12 street-name"
label="#{msgs['#common.search']}"
disabled="#{not configurationController.cardCtrl.editable}"
value="#{configurationController.cardCtrl.selected.address}"
required="true"
completeMethod="#{configurationController.cardCtrl.autoCompleteTest}">
<f:validator binding="#{onlyLettersOrDigitsValidator}"/>
</p:autoComplete>
<h:message id="addressMessage" for="address" styleClass="inline-error inline-error-small"/>
</div>
The autoCompleteTest method in the controller is as follows:
public List autoCompleteTest(String input) {
AddressSearch addressSearch = AddressSearch.builder()
.streetName(input)
.municipality(municipality.getName())
.maxResultsPerPage(10)
.build();
return addressesToStreetNames(mapService.addressSearch(addressSearch).getAddresses());
}
With a helper method addressesToStreetNames which takes a list of Address objects and returns an ArrayList<String> containing those addresses' street names.
EDIT2: Based on suggestions in the comments, I tried setting itemValue and itemLabel to different values, to see if that had any effect. The new xhtml looks like the above, with the addition of the following three lines:
var="address"
itemValue="#{address.streetName}"
itemLabel="#{configurationController.cardCtrl.formatAddress(address.streetName, address.postTown)}"
The autoCompleteTest method now also returns the Address object directly rather than a String representation of the street name, so that these fields are available. The formatAddress method is simply return streetName + ', ' + postTown;
This causes the dropdown list to look how I want it to look, but when I click an item it still inserts the whole string with both street name and post town/municipality into the text field (and in fact, before I've written anything, the text field already contains ", ").

Get HashMap value by dynamic key in EL

I have a data table which contains "Package" objects and 2 columns (packageBarcode and address). I need to add a column (total number of letters in a package), but this value is not in the object. That's why, I used HashMap. I mapped the packageId and totalNumberOfLetters in this HashMap. I want to display this values in dataTable. How can I do this?
<p:column headerText="package"
sortBy="#{package.barcode}"
filterBy="#{package.barcode}"
>
<h:outputText value="#{package.barcode}" />
</p:column>
Provided a
private List<Package> packages;
private Map<Long, Integer> totalNumberOfLettersByPackageId;
you can access it as below
<h:dataTable value="#{bean.packages}" var="_package">
<h:column>#{bean.totalNumberOfLettersByPackageId[_package.id]}</h:column>
</h:dataTable>
Do note that I prefixed package with _, because package is a Java literal and a sane EL implementation would throw a runtime exception on that.

JSF input processing order

Is there a way to specify the order in which the inputs should be set after a submit?
Here is my case:
<h:inputText id="fieldA" value=#{myBean.myObject.fieldA}" />
<h:inputText id="fieldB" value=#{myBean.myObject.fieldB}" />
<p:autoComplete id="myObject" value=#{myBean.myObject" converter="myObjectConverter" />
<h:inputText id="fieldC" value=#{myBean.myObject.fieldD}" />
<h:inputText id="fieldD" value=#{myBean.myObject.fieldC}" />
The issue I am encountering is that, as the inputs are processed in the ordered they are declared, fieldA and fieldB are set in the initial instance of myObject, then myObject is set (with a new instance thus filedA and fieldB values are lost), and finally fieldC and fieldD are set with no problem.
If I could manage to start by setting myObject first, that would solve my problem.
I will temporarily set the fields and myObject into two different attributes of my bean, and populate myObject after clicking a save button. But it looks more like a hack than a real solution.
Needless to say that declaring the autocomplete before the inputtexts is not an option.
Thanks in advance.
In shortcut:
You can use <p:inputText> tag from primefaces. Then, you can disable all inputs. Add ajax to your autoComplete, and update other inputs after processing autoComplete. Inputs disable attribute can be set to depend on whether the autoComplete is not null. This way you will make the user to enter the autoComplet first.
you can try to set immediate="true" to p:autocomplete, so that it will be processed in the APPLY_REQUEST_VALUES phase, before all other components.
The simple solution is to update h:inputTexts when p:autocomplete item is selected to reflect its values:
<p:autoComplete id="myObject" value="#{myBean.myObject}" ...>
<p:ajax event="itemSelect" process="#this" update="fieldA fieldB fieldC fieldD" />
</p:autoComplete>
but this reverts user inputs on h:inputTexts. And since you can't move p:autocomplete on top, probably this is not acceptable too.
In case you can't/don't want to use ajax, you can force an early model update:
<p:autoComplete id="myObject" value="#{myBean.myObject}" immediate="true"
valueChangeListener="#{component.updateModel(facesContext)}" ... />
but, in my opinion, this is not very user friendly...
P.S. this time it's tested :)
There's no pretty way to get around this; your situation is already less than ideal and is hacky (re: not being able to simply reorder the fields). One workaround is for you to set fieldA and fieldB as attributes of myObject. In the converter, you could then pull the values off the components. Observe
Set attributes thus
<h:inputText id="fieldA" binding=#{fieldA}" />
<h:inputText id="fieldB" binding=#{fieldB}" />
<p:autoComplete id="myObject" value=#{myBean.myObject" converter="myObjectConverter">
<f:attribute name="fieldA" value="#{fieldA}"/>
<f:attribute name="fieldB" value="#{fieldB}"/>
</p:autoComplete>
The binding attribute effectively turns those components into page-scoped variables, allowing you to then pass them as attributes on your p:autocomplete
Get the values of those variables in your converter
//Retrieve the fields and cast to UIInput, necessary
//to retrieve the submitted values
UIInput fieldA = (UIInput) component.getAttributes().get("fieldA");
UIInput fieldB = (UIInput) component.getAttributes().get("fieldB");
//Retrieve the submitted values and do whatever you need to do
String valueA = fieldA.getSubmittedValue().toString();
String valueB = fieldB.getSubmittedValue().toString();
More importantly, why can't you just reorder the fields/logical flow of your form? You can avoid all this nasty business if you did

How to get index of selected item of jsf f:selectItems?

I have seleconeradio, for example:
<h:selectOneRadio value="#{myBean.selectedValue}" layout="pageDirection">
<f:selectItems value="#{myBean.myList}" var="a" itemValue="#{a}" itemLabel="#{a}"/>
</h:selectOneRadio>
where myList is list of integers, e.g. 1,3,2,4.
If user selects second element (i.e. 3) I want in myBean selectedValue to be 2, so I want to get index of selectItems item.
What should I write in f:selectItems itemValue tag? Or it is impossible?
P.S. I can do it by creating a new class in which I have the index property and create a new list of that class, giving the right index. But it is very bad solution.
You can actually use c:forEach for this case. This is especially usefull when you have to deal with a collection containing duplicates and therefore can't use indexOf() for example.
<h:selectOneRadio value="#{myBean.selectedValue}" layout="pageDirection">
<c:forEach items="#{myBean.myList}" var="a" varStatus="idx">
<f:selectItem itemValue="#{idx.index}" itemLabel="#{a}"/>
</c:forEach>
</h:selectOneRadio>
Just be sure to include the JSP JSTL Core namespace if you haven't done yet.
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core
you should use indexOf(Object o) .. it returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element...
your code should probably look like this..
int index = myList.indexof(selectedValue);

Convert double to int in inline-text

<o:importFunctions type="java.lang.Math" />//omnifaces, see http://showcase.omnifaces.org/taghandlers/importFunctions
<c:set var="ordersToShow" value="${Math:min(5, processedOrders.size())}" /> // processedOrders is a List
<p:fieldset legend="Last Stuff (${ordersToShow})">[...]</p:fieldset>
${ordersToShow} is of type java.lang.Double (output of "${ordersToShow.class}") and prints sth. like "Last Stuff 2.0".
I want to have the output like "Last Stuff 2", how can I do that?
f:convertNumber will throw error ( Parent not an instance of ValueHolder) for p:fieldSet if its used inside.
Instead you can keep an string in managed bean and in its getter you can use String.format("%.0f", ordersToShow);
Else keep an binding variable for fieldSet and set the above formatted value in bean.
You can use f:convertNumber. Keeping minFractionDigits="0" should do the trick.
See here http://www.tutorialspoint.com/jsf/jsf_convertnumber_tag.htm
I have not used p:fieldset so now sure how the f:convertNumber can be used with the legend but seems not possible.
You can use this, replace c:set and p:fieldset lines with the below. The fmt tag explained here http://www.tutorialspoint.com/jsp/jstl_format_formatnumber_tag.htm
<fmt:formatNumber var="ordersToShow" type="number" maxFractionDigits="0" value="${...}" />
<p:fieldset legend="Last Stuff (${ordersToShow})">[...]</p:fieldset>

Resources