JSF issue with changing default radioselection - jsf

Im using the h:selectOneRadio and having issues when I try to change the default radio selection. Below is the JSF code
<h:selectOneRadio id="radioSelectID1" layout="pageDirection" value="#{scheduleBean.selectedRecurrenceValue}" immediate="true" >
<f:selectItem id="dailyID" itemLabel="#{adminBean.adminScreenLabelVO.dailyLabel}" itemValue="#{scheduleBean.dailyValue}"></f:selectItem>
<f:selectItem id="weeklyID" itemLabel="#{adminBean.adminScreenLabelVO.weeklyLabel}" itemValue="#{scheduleBean.weeklyValue}"></f:selectItem>
<f:selectItem id="monthlyID" itemLabel="#{adminBean.adminScreenLabelVO.monthlyLabel}" itemValue="#{scheduleBean.monthlyValue}"></f:selectItem>
<f:ajax event="change" listener="#{scheduleBean.recurrenceChange}" render="recurrencePanelID" />
</h:selectOneRadio>
below is the backingbean code for the selectedRecurrenceValue
public String getSelectedRecurrenceValue() {
if(selectedRecurrenceValue == null)
{
selectedRecurrenceValue = adminScheduledetailsVO.getFrequency();
if(selectedRecurrenceValue.equalsIgnoreCase(dailyValue))
{
dailyPanelRenderer = true ;
}
if(selectedRecurrenceValue.equalsIgnoreCase(weeklyValue))
{
dailyPanelRenderer = false ;
weeklyPanelRenderer = true ;
}
if(selectedRecurrenceValue.equalsIgnoreCase(monthlyValue))
{
monthlyPanelRenderer = true ;
}
}
return selectedRecurrenceValue;
}
The issue occurs when I try to change the radioselection option. Im unable to initialize the value selectedRecurrenceValue, since Im getting it dynamically. When I try to select another radio button, Im receiving a null pointer exception for selectedRecurrenceValue.
Any pointers to fix the issue would be really helpful.Thanks in Advance.

If I read your question right it seems that you don't need the field selectedRecurrenceValue and should bind adminScheduledetailsVO.frequency directly to your selectOneRadio:
<h:selectOneRadio id="radioSelectID1"
layout="pageDirection"
value="#{scheduleBean.adminScheduledetailsVO.frequency}" >
Then of course adminScheduledetailsVO needs to be initialized before to avoid the NPE.

Related

Non rendered p:dataTable ends up in pe:exporter PDF

I have 2 dataTables:
<p:dataTable id="tbl1" var="prop1" value="#{bean.prop1}" rendered="#{bean.listP1.size() != 0}">
// ...
</p:dataTable>
and
<p:dataTable id="tbl2" var="prop2" value="#{bean.prop2}" rendered="#{bean.listP2.size() != 0}">
// ...
</p:dataTable>
On XHTML page, after some action, I got the correct result depending on the size of both listP1 and listP2.
My problem is after clicking on export button
<h:commandLink>
<p:graphicImage value="/resources/icons/download.png" style="width : 35px; height:35px"/>
<pe:exporter type="pdf" target="tbl1, tbl2" fileName="SurveyResults"/>
</h:commandLink>
I got wrong result : I got the two tables instead of one because one of them is with size = 0
Have you please any idea about solving that?
Prior to version 8, the pe:exporter does not care whether the data tables you provide are rendered or not, see https://github.com/primefaces-extensions/primefaces-extensions.github.com/issues/757
If you are not on version 8 yet, you can use EL to create a dynamic value for the target attribute though. For example:
<pe:exporter type="pdf"
target="#{empty bean.listP1 ? '' : 'tbl1'}#{empty bean.listP1 or empty bean.listP2 ? '' : ','}#{empty bean.listP2 ? '' : 'tbl2'}"
fileName="SurveyResults"/>
As these expressions are a bit hairy, you might want to create a method in your bean which creates a target string and do:
<pe:exporter type="pdf"
target="#{bean.exporterTargets}"
fileName="SurveyResults"/>
And in your bean for example:
public String getExporterTargets() {
return Stream.of(listP1.isEmpty() ? null : "tbl1",
listP2.isEmpty() ? null : "tbl2")
.filter(Objects::nonNull)
.collect(Collectors.joining(","));
}

How to evaluate a FacesComponent property inside a custom component

Lately, I've been working on a dynamic form project but is stop with a custom component problem. What I have:
A faces component for formField:
#FacesComponent(value = "formField")
public class FormFieldCompositeComponent {
private boolean email;
}
A custom component jsf:
<o:validator for="email_text" validatorId="emailValidator" disabled="#{not cc.email}" />
OR
<c:if test="#{not cc.email}">
<f:validator for="email_text" validatorId="emailValidator"></f:validator>
</c:if>
OR
<f:validator disabled="#{not cc.email}" for="email_text" validatorId="emailValidator"></f:validator>
And the validator:
#FacesValidator("emailValidator")
public class EmailValidator implements Validator { }
My problems are:
1.) If I use an ordinary f:validator, like the one I use above and then use c:if to enable/disable it, then it will not work. According to some articles I've read it's because f:validator validates on build time, not on render time.
2.) If I use o:validator, it works but the problem is every time you hit submit a new line of invalid email error is added to p:messages. Example I clicked submit button 3 times, then I get 3 times the email error.
Any idea?
More info (anatomy of the project)
Example I have a page user with field email, it will include the following custom components:
+user.xhtml
+formPanel
+formField (this is where the validator is defined)
+formButtons (the action button)
+p:messages is defined
user.xhtml
<formPanel>
<formField field="email" />
<formButtons />
</formPanel>
Command button is like (formButtons):
<p:commandButton id="saveButton" rendered="#{cc.attrs.edit}"
value="#{messages['action.save']}"
action="#{cc.attrs.backingBean.saveOrUpdate()}" icon="ui-icon-check"
ajax="#{cc.attrs.ajaxSubmit}">
<c:if test="#{cc.attrs.backingBean.lcid != null}">
<f:param name="cid" value="#{cc.attrs.backingBean.lcid}" />
</c:if>
</p:commandButton>
The p:messages as defined on formPanel:
<p:messages id="formMessages" showDetail="true" showSummary="false" redisplay="false"></p:messages>
Note:
1.) What I've noticed is that the validator is called n times, where n is the number of submit or click done.
xhtml - https://github.com/czetsuya/crud-faces/blob/master/crud-faces/src/main/webapp/administration/user/user.xhtml
the tags - https://github.com/czetsuya/crud-faces/tree/master/crud-faces/src/main/webapp/resources/tags
bean component - https://github.com/czetsuya/crud-faces/tree/master/crud-faces/src/main/java/org/manaty/view/composite
Seems like there's no chance for the f:validator so I push through o:validator and come up with a workaround. Need to catch if the error is already in the FacesMessages list:
boolean match = false;
for (FacesMessage fm : context.getMessageList()) {
if (fm.getDetail().equals(message)
|| fm.getSummary().equals(message)) {
match = true;
break;
}
}
if (!match) {
throw new ValidatorException(facesMessage);
}

jsf2 f:selectItem itemLabel complex i18n render

I've a f:selectitem with its itemLabel and i want to render the label so "pretty"!
The problem:
<p:selectOneRadio id="selectRadio" value="#{somebean.somevalue}" layout="pageDirection" >
<f:selectItem itemLabel="#{msg['message.1']}" itemValue="1" />
<f:selectItem itemLabel="#{msg['message.2']}" itemValue="2" />
</p:selectOneRadio>
My properties i18n file:
message.1=some message by {0}
message.2=another message by {0}
I want to do a inline replacement of {0} by #{somebean.theUser}
The result should be something like this (bold included):
some message by <b>HUSTON</b>
In tag file i should do something like this (or in similar way)
<f:selectItem itemLabel="#{msg['message.1']{'<b>'+somebean.theUser+'</b>'}}" itemValue="1" />
In other words, i want to add the i18n string param replacement directly into itemLabel tag.
I've try with <f:facet name="itemLabel"> but nothing.
Someone can help me?
Thanks in advice,
Agharta
JSF won't let you do that from the markup side. There is no way to use for example <h:outputFormat /> for it.
Instead you need to simply create a bean, that will read the contents from properties file and format accordingly:
<p:selectOneRadio id="selectRadio" value="#{somebean.somevalue}" layout="pageDirection" >
<f:selectItem itemLabel="#{helperbean.someMessage}" itemValue="1" />
<f:selectItem itemLabel="#{helperbean.anotherMessage}" itemValue="2" />
</p:selectOneRadio>
That was JSF part, in your bean:
public String getSomeMessage() {
// Actually you need some common access helper, it is just simplified example
String messagePattern = null;
try {
ResourceBundle rb = ResourceBundle.getBundle("path/to/properties/file");
String messagePattern = rb.getString("message.1");
return MessageFormat.format(messagePattern, somebean.getTheUser());
} catch (MissingResourceException mre) {
logger.warn("Missing resource file or resource key", mre);
return "!message.1!"; // That will show you where the problem is
}
}
Few additional points regarding Localizability:
Please use meaningful key names. Something like "message.1" gives no context to translators. I don't know what you are up to, but maybe "some-module.select.message.radio.message.sent.by.pattern" will be something better - the translator needs to know a) where it will be displayed, b) what is the purpose of this text (whether it is some description, message pattern, general text, window/dialog title, button caption, etc.). Be sure to provide them such context.
Be sure to include formatting tags into the message itself. That means, the message should look like message.1=some message by <b>{0}</b> in your properties file. You'd be surprised how often these kind of tags need to be removed (or replaced with other emphasis means). You should be especially careful, since you seem to be doing something with RTL languages and bolded fonts does not work with them very well.

Reset value to null in primefaces autocomplete event

I have an autocomplete event that fires correctly once a value is selected. I want another event to fire once I erase the value in the textbox and reset the value to null. I was thinking of using the onChange attribute but I was having issues so I reverted back to my original code.
<p:autoComplete id="deviceAuto" dropdown="true" scrollHeight="250"
value="#{summaryReportController.device.nickname}"
forceSelection="true"
completeMethod="#{summaryReportController.deviceComplete}">
<p:ajax event="itemSelect"
listener="#{summaryReportController.handleDeviceSelect}"
update="printThis" />
</p:autoComplete>
public void handleDeviceSelect(SelectEvent event) {
String deviceSelect = event.getComponent().getId();
if (deviceSelect.equalsIgnoreCase("deviceAuto")) {
Device selectedDevice = deviceMgr.getDevicebyNickname(device.getNickname());
setDevice(selectedDevice);
}
updateInterface();
}
When you modify the text content of the AutoComplete textfield, the search method (aka. completeMethod) will be called on the backing bean. You can reset the value to null there if you get an empty string.
Backing Bean
// insert getter and setter for the device property ...
/** Search for devices by name */
public List<String> deviceComplete(String search) {
if (StringUtils.isBlank(search)) {
setDevice(null); // textfield was cleared, reset device value!
return Collections.emptyList();
} else {
// search for devices ...
return deviceNames;
}
}
Note that I used Apache Commons StringUtils.isBlank(String) to check if the string was null or did only contain whitespace characters.
JSF View
In your XHTML file you probably want to listen to any Ajax event to update your view -- or you figure out the event you need (blur, change, whatever) yourself:
<p:autoComplete ...>
<p:ajax event="itemSelect" listener="..." update="..." />
<p:ajax process="#this" update="..." />
</p:autocomplete>
I hope this helps.
An alternative could be something like a "clear" or "reset" button next to the search textfield to make it clear to the user that the value will be cleared.
The default autoComplete minQueryLength attribute equals 1 and your search string will be updated when you deleting it until it has lenght of 1 character.
E.g.:
You entering 'foo' - and this string is provided to search method (updating after entering first character - minQueryLength = 1)
But when you delete search string - it is also updated until it will have length of 1.
Solution:
set attribute minQueryLength="0"
Or:
if you need bigger value add to your autoCompleteMethod(String search) condition:
if (search.length()<={your minQueryLength attribute} ) field = null;
Old question, but I think it worths another view.
The problem with minQueryLenth = 0 or minQueryLenth = 1 is that it can return hundreds of options (and for sure the user won't read all of them to choose one). My solution was as follows.
First of all I need the input to be sent to the server as soon as the user select one of its values (in my use case the user is not allowed to go to next step in a wizard if this value is null or empty). So I put an ajax function triggered in the event of a selected value.
xhtml:
<p:autoComplete
id="someId"
value="#{myViewScopedBean.selectedValue}"
...
...
minQueryLenth="5"
onblur="autoCompleteLostFocus('someId', 'someCommand()')">
<p:ajax
event="itemSelect"
listener="#{myViewScopedBean.newValueSelected}"
process="#this"
update="commandButtonGoToNextStep" />
</p:autoComplete>
<p:remoteCommand
name="someCommand"
actionListener="#{myViewScopedBean.setValueNull}"
update="commandButtonGoToNextStep" />
<p:commandButton
id="commandButtonGoToNextStep"
...
...
disabled="#{myViewScopedBean.selectedValue == null}" />
If the user clean the text, I need to send that value to "myViewScopedBean" and update the component that allows the user to go to the next step. I solved that putting a javascript function that is called when the autocomplete lose focus.
javascript:
function autoCompleteLostFocus(autocompleteId, comand) {
if ($("[id='" + autocompleteId + "_input']").val().trim() == "") {
eval(comando);
}
}
in myViewScopedBean:
public void setValueNull() {
selectedValue = null;
}
I hope it helps. A lot of work, but the behaviour is exactly what I wanted. The reason for the javascript function is that it just send information to the servlet if the value is equals to "", otherwise it does nothing.
From a completely different angle...
Why do you have summaryReportController.device.nickname as a value in autoComplete?
I'd suggest you to use device as a value and specify
var
itemLabel
itemValue
converter
in your autocomplete, while your completeMethod will return list of devices filtered by nickname. Converter is implementation of javax.faces.convert.Converter.
See the POJO case in PF Showcase.

How to display my application's errors in JSF?

In my JSF/Facelets app, here's a simplified version of part of my form:
<h:form id="myform">
<h:inputSecret value="#{createNewPassword.newPassword1}" id="newPassword1" />
<h:message class="error" for="newPassword1" />
<h:inputSecret value="#{createNewPassword.newPassword2}" id="newPassword2" />
<h:message class="error" for="newPassword2" />
<h:commandButton value="Continue" action="#{createNewPassword.continueButton}" />
</h:form>
I'd like to be able to assign an error to a specific h:message tag based on something happening in the continueButton() method. Different errors need to be displayed for newPassword and newPassword2. A validator won't really work, because the method that will deliver results (from the DB) is run in the continueButton() method, and is too expensive to run twice.
I can't use the h:messages tag because the page has multiple places that I need to display different error messages. When I tried this, the page displayed duplicates of every message.
I tried this as a best guess, but no luck:
public Navigation continueButton() {
...
expensiveMethod();
if(...) {
FacesContext.getCurrentInstance().addMessage("newPassword", new FacesMessage("Error: Your password is NOT strong enough."));
}
}
What am I missing? Any help would be appreciated!
FacesContext.addMessage(String, FacesMessage) requires the component's clientId, not it's id. If you're wondering why, think about having a control as a child of a dataTable, stamping out different values with the same control for each row - it would be possible to have a different message printed for each row. The id is always the same; the clientId is unique per row.
So "myform:mybutton" is the correct value, but hard-coding this is ill-advised. A lookup would create less coupling between the view and the business logic and would be an approach that works in more restrictive environments like portlets.
<f:view>
<h:form>
<h:commandButton id="mybutton" value="click"
binding="#{showMessageAction.mybutton}"
action="#{showMessageAction.validatePassword}" />
<h:message for="mybutton" />
</h:form>
</f:view>
Managed bean logic:
/** Must be request scope for binding */
public class ShowMessageAction {
private UIComponent mybutton;
private boolean isOK = false;
public String validatePassword() {
if (isOK) {
return "ok";
}
else {
// invalid
FacesMessage message = new FacesMessage("Invalid password length");
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(mybutton.getClientId(context), message);
}
return null;
}
public void setMybutton(UIComponent mybutton) {
this.mybutton = mybutton;
}
public UIComponent getMybutton() {
return mybutton;
}
}
In case anyone was curious, I was able to figure this out based on all of your responses combined!
This is in the Facelet:
<h:form id="myform">
<h:inputSecret value="#{createNewPassword.newPassword1}" id="newPassword1" />
<h:message class="error" for="newPassword1" id="newPassword1Error" />
<h:inputSecret value="#{createNewPassword.newPassword2}" id="newPassword2" />
<h:message class="error" for="newPassword2" id="newPassword2Error" />
<h:commandButton value="Continue" action="#{createNewPassword.continueButton}" />
</h:form>
This is in the continueButton() method:
FacesContext.getCurrentInstance().addMessage("myForm:newPassword1", new FacesMessage(PASSWORDS_DONT_MATCH, PASSWORDS_DONT_MATCH));
And it works! Thanks for the help!
You also have to include the FormID in your call to addMessage().
FacesContext.getCurrentInstance().addMessage("myform:newPassword1", new FacesMessage("Error: Your password is NOT strong enough."));
This should do the trick.
Regards.
Remember that:
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage( null, new FacesMessage( "The message to display in client" ));
is also valid, because when null is specified as first parameter, it is applied to the whole form.
More info: coreservlets.com //Outdated
JSF is a beast. I may be missing something, but I used to solve similar problems by saving the desired message to a property of the bean, and then displaying the property via an outputText:
<h:outputText
value="#{CreateNewPasswordBean.errorMessage}"
render="#{CreateNewPasswordBean.errorMessage != null}" />
Found this while Googling. The second post makes a point about the different phases of JSF, which might be causing your error message to become lost. Also, try null in place of "newPassword" because you do not have any object with the id newPassword.
I tried this as a best guess, but no luck:
It looks right to me. Have you tried setting a message severity explicitly? Also I believe the ID needs to be the same as that of a component (i.e., you'd need to use newPassword1 or newPassword2, if those are your IDs, and not newPassword as you had in the example).
FacesContext.getCurrentInstance().addMessage("newPassword1",
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Message"));
Then use <h:message for="newPassword1" /> to display the error message on the JSF page.
Simple answer, if you don't need to bind it to a specific component...
Java:
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Authentication failed", null);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, message);
XHTML:
<h:messages></h:messages>

Resources