Insert/Add JSF component at render time - jsf

Is it possible to add a child component during render? If not what would be the best practice to add a child component dynamically in a JSF 1.2 Environment? Thanks

The better place where you can do that is in a PhaseListener implementation.
For instance the next code snippet samples how you can add a new component in to the view root:
public class ViewModifierPhaseListener implements
javax.faces.event.PhaseListener {
#Override
public void afterPhase(PhaseEvent event) {
}
// Just sampling add component on ViewRoot
#Override
public void beforePhase(PhaseEvent event) {
// Gets the target component from ViewRoot
UIViewRoot viewRoot = event.getFacesContext().getViewRoot();
UIComponent parent = viewRoot.findComponent("parentComponentId");
// UIComponents to create depend on JSF implementation,
// Try to use the available factories when suplied by the implementation
UIComponent child = Factory.getComponent("ComponentClassName");
// Customize the component, for instance it has to be disabled
child.getAttributes().put("disabled", true);
// Adds the fresh created component to the parent
parent.getChildren().add(child);
}
#Override
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
}
Please note that getPhaseId returns the RENDER_RESPONSE phase because in that phase is where you have the components tree complete.
Your phase listener definition has to be set in the faces-config.xml's lifecycle element like this:
<lifecycle>
<phase-listener>your.package.ViewModifierPhaseListener</phase-listener>
</lifecycle>
Or if you work with facelets you could define it in the template of the pages you want to be affected by your listener. This helps you to discriminate when to execute your PhaseListener.
<f:phaseListener type="your.package.ViewModifierPhaseListener"/>

Related

Programatically add Parameter to HtmlCommandLink using a Phase Listener

I need to add a component (UIParameter) to a HtmlCommandLink component dinamically through a Phase Listener.
What I want to achieve is that every element <h:link outcome="out"> renders as <a href="out_url_parsed + ?param=paramvalue">.Where "param" is my component.
I've tried using this
private void addElement(final PhaseEvent event, final Class clazz, final UIComponent component) {
final FacesContext fcontext = event.getFacesContext();
UIViewRoot root = fcontext.getViewRoot();
if (root == null) {
return;
}
root.visitTree(new FullVisitContext(fcontext), new VisitCallback() {
#Override
public VisitResult visit(VisitContext context, UIComponent target) {
if (clazz.isInstance(target)) {
LOGGER.info("Element Found");
UIParameter parameter = new UIParameter();
parameter.setValue("willberonadom");
parameter.setId("sessiontoken");
target.getChildren().add(parameter);
}
return VisitResult.ACCEPT;
}
});
}
But it's not working. The element is actually found on the tree but the UIParameter does not render.
I've found that the UIViewRoot only has child elements after RENDER_RESPONSE phase. So i think this is why my added element is not rendered at the end of the process.
I'm sure I can add this param editing the views but I don't want to do that since it must be present on all h:link in the application and must be present on any other new added too. So I consider this as a better approach to avoid missing tags
On a similar case I've managed to add input hidden elements to every form on view with this code...
HtmlInputHidden hiddenToken = new HtmlInputHidden();
hiddenToken.setId("sessiontoken");
hiddenToken.setValue("willberandom");
hiddenToken.setRendered(true);
root.addComponentResource(event.getFacesContext(), hiddenToken,"form");
But it doesn't work on anchor tags
There are several mistakes:
You want to add a parameter to a HtmlCommandLink component which represents <h:commandLink>, but you're giving an example with <h:link>, which is represented by HtmlOutcomeTargetLink. What exactly do you want?
A PhaseListener on beforePhase() of RENDER_RESPONSE may be too late on GET requests which would only build the view for the first time during render response. At the moment your PhaseListener runs, the UIViewRoot would have no children at all. You'd better hook on view build time instead. For that, a SystemEventListener on PostAddToViewEvent is the best suitable.
You're setting the parameter name as an id instead of name. Use UIParameter#setName() instead of UIParameter#setId().
Provided that you actually meant to add them to <h:link> components, then here's a kickoff example how you can achieve that with a SystemEventListener.
public class YourSystemEventListener implements SystemEventListener {
#Override
public boolean isListenerForSource(Object source) {
return source instanceof HtmlOutcomeTargetLink;
}
#Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
UIParameter parameter = new UIParameter();
parameter.setName("sessiontoken");
parameter.setValue("willberonadom");
((UIComponent) event.getSource()).getChildren().add(parameter);
}
}
(if you actually want to apply them on <h:commandLink> as well, just extend the isListenerForSource() check with a || source instanceof HtmlCommandLink)
In order to get it to run, register it as follows in faces-config.xml:
<application>
<system-event-listener>
<system-event-listener-class>com.example.YourSystemEventListener</system-event-listener-class>
<system-event-class>javax.faces.event.PostAddToViewEvent</system-event-class>
</system-event-listener>
</application>

How to disable elements from within a ViewHandler after jsf has embedded the composite component?

I'm using a ViewHandler to block all input elements on any accessed page, if certain criteria is met.
This works great for the input elements in the 'primary' xhtml files, but the input elements within composite components aren't being blocked. I figured it has to do with the fact that JSF embeds these components only after my ViewHandler has finished it's job.
Does anyone have an idea of how I can disable the elements in the composite as well?
A ViewHandler is the wrong tool for the job. It's intented to create, build and restore views and to generate URLs for usage in JSF forms and links. It's not intented to manipulate components in a view.
For your particular functional requirement, a SystemEventListener on PostAddToViewEvent is likely the best suit. I just did a quick test, it works for me on inputs in composites as well.
public class MyPostAddtoViewEventListener implements SystemEventListener {
#Override
public boolean isListenerForSource(Object source) {
return (source instanceof UIInput);
}
#Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
UIInput input = (UIInput) event.getSource();
if (true) { // Do your check here.
input.getAttributes().put("disabled", true);
}
}
}
To get it to run, register it as follows inside <application> of faces-config.xml:
<system-event-listener>
<system-event-listener-class>com.example.MyPostAddtoViewEventListener</system-event-listener-class>
<system-event-class>javax.faces.event.PostAddToViewEvent</system-event-class>
</system-event-listener>

Validate rich:dataTable value size's on form submit

I have a "new item" form that requires a list of dates, with the following components:
A <rich:calendar> input;
A <a4j:commandButton> that adds the chosen date to a List<Date> chosenDates in the backing bean;
A <rich:dataTable> with it's value set to the List<Date> chosenDates attribute;
A <a4j:commandButton> per dataTable row that removes it's date from theList<Date> chosenDates;
How to validate (JSF's validation phase) the size of the chosenDates list on form submit (creation process)?
RichFaces 4, JSF 2.1 (Mojarra).
I'd advise a cleaner approach with a JSF PhaseListener. The JSF processing will stop skip ahead the other phases if validation fails. Create a PhaseListener that will inspect the size of your list during the validations phase as against during the model update/invoke action phase. Try something like this
Create a phase listener for the validations phase
public class TestPhaseListener implements PhaseListener {
#Override
public void afterPhase(PhaseEvent event) {
throw new UnsupportedOperationException("Not supported yet.");
}
#Override
public void beforePhase(PhaseEvent event) {
if(event.getPhaseId().equals(PhaseId.PROCESS_VALIDATIONS)){
FacesContext ctx = event.getFacesContext();
YourBeanClass theBeanClass = ctx.getApplication().evaluateExpressionGet(ctx, "#{someBean}", YourNeanClass.class); //obtain a reference to the backing bean containing the list
/*
inspect the size of the list here and based on that throw the exception below
*/
throw new ValidatorException(new FacesMessage("Too many dates","Too Many Dates"));
}
}
#Override
public PhaseId getPhaseId() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Register your new listener in the faces_config.xml file
<lifecycle>
<phase-listener>your.package.structure.TestPhaseListener</phase-listener>
</lifecycle>
EDIT: Based on your comment, as an alternative, you can hook into the component's lifecycle using the <f:event/> tag and the preValidate or postValidate events (depending on your preference)
A listener tag to your component
<rich:dataTable>
<f:event type="preValidate" listener="#{yourBean.listener}"/>
</rich:dataTable>
Define a listener method in your backing bean to run per your defined event. The method signature must take an argument of type ComponentSystemEvent
public void preCheck(ComponentSystemEvent evt){
//You're in your backing bean so you can do pretty much whatever you want. I'd advise you mark the request as validation failed and queue FacesMessages. Obtain a reference to FacesContext and:
facesContext.validationFailed();
}
Do something like:
#{yourBean.chosenDates.size()}
I suppose you have a getter called getChosenDates which returns the chosenDates list.
Regarding your "validation concerns":
You can create a Validate method in your bean and return list of ValidationMessages. A sample is below, one that i used in my code.
public List<ValidationMessage> validate() {
List<ValidationMessage> validations = new ArrayList<ValidationMessage>();
int curSampleSize = sampleTable.getDataModel().getRowCount();
if(getNumberOfSamples() != null) {
size += getNumberOfSamples();
} else {
validations.add(new ValidationMessage("Please enter the no of samples to continue."));
return validations;
}
return validations;
}
Then, on submit you can check if you have any ValidationMessages as follows:
List<ValidationMessage> errs = validate();
if(errs.size()>0) {
FacesValidationUtil.addFacesMessages(errs);
return null;
}
Hope this helps!

JSF: <f:event> with custom events

I can't figure out if it is possible to use custom events using f:event. The book by Ed Burns suggests to ad the #NamedEvent annotation to the Event class and to use:
<f:event type="com.foo.bar.myEvent" listener="#{listener} />
but the event seems never to be instantiated.
From my point of view this makes sense, since the component does not know anything about the event, e.g. when to publish, so this might be useful for custom component authors only.
On the other hand, standard components should be able to publish the the event if derived from e.g. PostAddToViewEvent. Anyway, custom events seem to be never published by standard components.
Am I missing something? Is there a convenient way to use custom events with standard components?
Here is what I wanted to do:
<h:inputText id="input">
<f:event type="foo.bar.MyCustomEvent" />
</h:inputText>
public class MyCustomEvent extends javax.faces.event.PostAddToViewEvent {
}
yes you can for this you have to override some method in jsf render or component class
public class MyComponent extends HtmlInputText or public class MyRenderer extends TextRenderer
#Override
public void decode(FacesContext context, UIComponent component) {
super.decode(context, component);
String sourceName = context.getExternalContext().getRequestParameterMap().get("javax.faces.source");
if(sourceName != null && sourceName.equals(component.getClientId())){
component.queueEvent(new MyEvent(component));
}
}
but in MyEvent class you have to override some methods
#Override
public PhaseId getPhaseId() {
return PhaseId.INVOKE_APPLICATION;
}
which will define in which face this event will process (by default it is ANY_PHASE and event trigger in same phase in which it registered)
#Override
public boolean isAppropriateListener(FacesListener listener) {
return false;
}
if you have appropiate listener it must return true
if you have appropiate listener for MyEvent then JSF will call that listener's processAction(ActionEvent event) method when it will trigger event, otherwise it will call broadcast method in component class which has to be override by developer
#Override
public void broadcast(FacesEvent event) throws AbortProcessingException {
super.broadcast(event);
if(event instanceof MyEvent){
try {
processMyEvent(event);
} catch (Exception e) {
// TODO: handle exception
}
}
}
Even you can register any event by your own by using component queueEvent(FacesEvent event) method, it will regiester event and it get the phase in which it will trigger by getPhaseId() method in MyEvent class if getPhaseId() method is not overrided by devloper then it will trigger in same phase in which it registered

JSF - Update model in invoke application phase

in my JSF application i need to update ui component during invoke application phase. Can it be done? Here's the code i've produced so far:
public void resetDataScroller(ActionEvent actionEvent) {
final FacesContext ctx = FacesContext.getCurrentInstance();
ctx.getViewRoot().invokeOnComponent(ctx, "paginator_and_table:scroll_1", new ContextCallback() {
public void invokeContextCallback(FacesContext facesContext, UIComponent uiComponent) {
HtmlDatascroller htmlDatascroller = (HtmlDatascroller) uiComponent;
htmlDatascroller.setPage(1);
htmlDatascroller.setValue(1);
}
});
}
This action listener looks up dataScroller component and sets page and value to 1. Unfortunatelly it doesn't seem to work at all, because rendered dataScroller has page different than 1.
Am i missing something?
I imagine that your resetDataScroller a method called by an actionListener attribute of a command button/link on your page?
I don't really understand what you are trying to do... Do you just need to write this code? :
public void resetDataScroller(ActionEvent evt) {
final FacesContext ctx = FacesContext.getCurrentInstance();
HtmlDatascroller htmlDatascroller = (HtmlDatascroller) ctx.getViewRoot().findComponent("paginator_and_table:scroll_1");
htmlDatascroller.setPage(1);
htmlDatascroller.setValue(1);
}
If you change these properties of the HtmlDatascroller during this phase, they will be used by JSF during the last phase (the Render Response phase) to generate your HTML code...

Resources