Process f:viewParam only on page load

You can achieve this by creating a custom tag extending <f:viewParam> wherein you store the submitted value as an instance variable which isn’t stored in JSF view state instead of in the JSF view state as the <f:viewParam> by default does. By end of request, all UI component instances are destroyed. They are recreated in … Read more

JSF: Mojarra vs. OmniFaces @ViewScoped: @PreDestroy called but bean can’t be garbage collected

The cause of this problem seems to be due to strange behaviour JVisualVM when attached to Glassfish/Payara. The test case used for this question is still extremely useful, however the conclusions concerning Garbage Collection in the original post (and images) were based on JVisualVM, and I have since found they are not valid. Use the … Read more

Passing parameters to a view scoped bean in JSF

Using view parameters is the most proper way for your case. You don’t really need to perform a REDIRECT, but a plain GET request: <h:button value=”Go to details” outcome=”carDetails”> <f:param name=”carId” value=”#{currentCar.id}” /> </h:button> This will point you to this address: carDetails.xhtml?carId=1 After that, in your carDetails.xhtml page, grab the view param and load the … Read more

Store PDF for a limited time on app server and make it available for download

Make use of File#createTempFile() facility. The servletcontainer-managed temporary folder is available as application scoped attribute with ServletContext.TEMPDIR as key. String tempDir = (String) externalContext.getApplicationMap().get(ServletContext.TEMPDIR); File tempPdfFile = File.createTempFile(“generated-“, “.pdf”, tempDir); // Write to it. Then just pass the autogenerated file name around to the one responsible for serving it. E.g. String tempPdfFileName = tempPdfFile.getName(); // … Read more

How to escape f:selectItem itemLabel attribute

This is a documentary bug in JSF. The actual attribute is named itemEscaped, not escapeItem (as incorrectly documented in VDL) or escape (which Eclipse autocomplete indeed autosuggests for some unknown reason, but is actually totally absent in VDL). The following construct should work for you (at least, it does for me on Mojarra 2.1.17): <h:selectOneRadio> … Read more

JSF 2.0: How to override base renderers with custom ones?

Your initial <renderer> declaration looks fine, so I tried it here. package com.myapp; import java.io.IOException; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import com.sun.faces.renderkit.html_basic.CheckboxRenderer; public class CustomCheckboxRenderer extends CheckboxRenderer { public CustomCheckboxRenderer() { System.out.println(“CustomCheckboxRenderer <init>”); } @Override public void decode(FacesContext context, UIComponent component) { System.out.println(“CustomCheckboxRenderer decode()”); super.decode(context, component); } @Override public void encodeBegin(FacesContext context, UIComponent component) throws IOException … Read more

Why selectOneMenu Send ItemLabel to the converter?

I’m not sure why you got the item label instead of the item value inside getAsObject(). Perhaps your getAsString() is doing it wrong and it is returning student name based on the student ID. At any way, I can tell that your itemValue is definitely not right. <h:selectOneMenu id=”studlist” value=”#{studBean.selectedStudent}”> <f:selectItems value=”#{studBean.student}” var=”s” itemValue=”#{s.studid}” itemLabel=”#{s.name}” … Read more

Customize FacesServlet to get rid of .xhtml extension

If your sole reason is to get rid of the .xhtml extension, then there are various ways depending on the JSF version you’re using. Faces 4.0 Just add the following context parameter to web.xml: <context-param> <param-name>jakarta.faces.AUTOMATIC_EXTENSIONLESS_MAPPING</param-name> <param-value>true</param-value> </context-param> JSF 2.3+ JSF 2.3 offers a new API to collect all views: the ViewHandler#getViews(). Combine this with … Read more