How to define <p:calendar> pattern to entire application? - jsf

I would like to define the p:calendar pattern to the entire application instead of to put pattern="dd/MM/yyyy" in each p:calendar.

What I did was overload the renderer for the calendar component with my own and set it there.

Related

Global date/boolean formatting in frontend of JSF application

I am developing a JSF dashboard web application which includes several very large PrimeFaces datatables. It would be really handy if I could set a global format for the representation of certain types – currently the only way I can think of is having backing bean methods like
public String formattedBoolean(Boolean inputBoolean) {
return inputBoolean ? "Yes" : "No";
}
which I could then manually call from the frontend (and similarly, I could use f:convertDateTime components for formatting date objects as string). However, this type of approach would require adding a lot of boilerplate XHTML, such as
<p:column headerText="Start Date" id="startDate"
sortBy="#{dataContainer.startDate}"
filterBy="#{dataContainer.startDate}">
<h:outputText value="#{dataContainer.startDate}">
<f:convertDateTime pattern="yyyy-MM-dd"/>
</h:outputText>
</p:column>
which I'd rather avoid. Does anyone know if there is a more elegant way of globally overriding the default Java string representation of objects in the frontend, or at least within the context of a data table?
Following advice above, I have decided to override the default FacesConverter for the given classes, as these are formats I'd want to present consistently in text throughout the application.

Apply converter throughout the whole project

I would like to apply a converter to all my h:outputText in a project. Is there a way to do it for the whole project, so I don't have to repeat the tag for each field separately?
For example, assume I want the following converter.
<f:convertNumber type = "number" />
Is there a way not to repeat this for every h:outputText field that contains numeric values?
I'm afraid but this is not possible.
But you could write your own JSF outputText Component. That's pretty easy:
https://www.logicbig.com/tutorials/java-ee-tutorial/jsf/custom-component-basic.html

Number converter wrapping ignores locale? bug?

Is it a Bug that the first does selects between a dot or comma (always dot) according to the used f:view locale? Or is there a rule how to nest number converters.
1.
<f:converter converterId="javax.faces.BigDecimal">
<f:convertNumber maxIntegerDigits="3" maxFractionDigits="2"/>
</f:converter>
2.
<f:convertNumber maxIntegerDigits="3" maxFractionDigits="2">
<f:converter converterId="javax.faces.BigDecimal"/>
</f:convertNumber>
You cannot nest converters. They will just be applied on the closest parent UIComponent in the order as they're declared. You can also not specify multiple converters. Only the last one would really be used. Each ValueHolder component can have only one converter while each EditableValueHolder can have more than one validators.
Just stick to <f:convertNumber>. When used on input components which are already bound to a property of BigDecimal type, it'll respect the type.

i have two text box in two different pages a

i have two text box in two different pages and same variable use in two text box when enter value in first text box it's reflection show in second text box , how i reduce it in jsf ?
You can create a #SessionScoped bean named GeneralBean
Set a variable there e.g.:
private String name;
add getters and setters.
In the pages where the user enters his data you should have
<h:form>
<h:inputText value="#{generalBean.getName}">
<f:ajax event="change" />
</h:inputText>
</h:form>
Using Ajax, whenever he changes the value it will affect the name variable which appears also in the other screen.
EDITED:
I presumed that you are using JSF2.0. If not, please indicate it.
EDITED:
Since you are using JSF1.1 you will need external tool support.
You can use RichFaces a4j:support.
For that you need to download richfaces.
Use a4j:support. See an example here:
a4j:support
EDITED:
This answer is if the pages are not opened simultaneously.If they are opened at the same time you will have to use richfaces push optionin the other one.

Conditionally setting the font color of an item in a drop down box in JSF

I'm a newbie to web programming, so please bear with me.
I am using JSF 1.2 and I want to conditionally set the color of the items in my drop down list.
In other words, I have a collection of items that, along with a key and a value, also have a boolean property, warning, that is dynamically set by user action. I want the drop down to show all of the items, but those whose warning property is set to true should be displayed in red.
I assume I have to extend the SelectItem class to add in the boolean property. How do I then conditionally set the color of the font of those items whose warning property is set to true in my JSP pages?
Thanks in advance
Unfortunately, the JSF standard implementation of the HTML <select> element, the h:selectOneMenu doesn't provide facilities to set style classes on each individual <option> element.
You can however create a custom renderer which does that and configure your webapp to use that renderer instead. Basically you just need to add an extra attribute to the component wherein you pass some separated string with all option style classes which are to be applied on the options repeatedly. The renderer should then take care about picking this attribute and applying the style classes on the option elements accordingly.
You can find code examples and explanations in this article. You can then end up with something like:
<h:form>
<h:selectOneMenu value="#{myBean.selectedItem}">
<f:attribute name="optionClasses" value="option1, option2" />
<f:selectItems value="#{myBean.selectItems}" />
</h:selectOneMenu>
<h:commandButton value="submit" action="#{myBean.submit}" />
</h:form>
You can of course also generate and return the value from the bean:
<f:attribute name="optionClasses" value="#{myBean.optionClasses}" />

Resources