FacesContext.getCurrentInstance() returns null in Runnable class

The FacesContext is stored as a ThreadLocal variable in the thread responsible for the HTTP request which invoked the FacesServlet, the one responsible for creating the FacesContext. This thread usually goes through the JSF managed bean methods only. The FacesContext is not available in other threads spawned by that thread. You should actually also not … Read more

Automatically open the printer dialog after providing PDF download

With JasperReports When using JasperReports, simply add this parameter to JasperReports exporter: exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, “this.print();”); This basically instructs Adobe Acrobat to execute the script this.print() when opening the PDF. See also page 79-80 of Adobe Acrobat Scripting Guide. Below is an extract of relevance: Printing PDF Documents It is possible to use Acrobat JavaScript to specify … Read more

How to use Parameterized MessageFormat with non-Value attributes of JSF components

You could create a custom EL function for this with which you can ultimately end up like: <h:commandLink … title=”#{my:format(msg[‘someMessage’], someBean.value)}” /> You can use the MessageFormat API to perform the job, exactly as <h:outputFormat> is doing under the covers. An alternative is to create a custom component which does the same as JSTL’s good … Read more

LazyInitializationException in selectManyCheckbox on @ManyToMany(fetch=LAZY)

You need to fetch it while inside a transaction (thus, inside the service method), and not while outside a transaction (thus, inside e.g. JSF managed bean init/action method), that would thus throw a LazyInitializationException. So, your attempt hardware.getConnectivities().size(); has to take place inside a transaction. Create if necessary a new service method for the purpose … Read more

JSF redirect to other page

Just bind the buttons to different action methods which each return a different outcome. <h:commandButton value=”Go to page 1″ action=”#{bean.goToPage1}” /> <h:commandButton value=”Go to page 2″ action=”#{bean.goToPage2}” /> with public String goToPage1() { // … return “page_1”; } public String goToPage2() { // … return “page_2”; } Navigation cases are not necessary. JSF 2.0 supports … Read more

Accessing properties file in a JSF application programmatically

The Class#getResourceAsStream() can take a path which is relative to the location of the Class which you’re using there as starting point. So, for example, if the class is located in the com.example package and you request the path foo/filename.properties, then it will actually load the com/example/foo/filename.properties file. But if you use /foo/filename.properties, then it … Read more

JSF 2 dataTable row index without dataModel

Just bind the table to the view itself instead of to a bean. <h:dataTable binding=”#{table}” …> Then you can use #{table.rowIndex} where necessary. E.g. <h:column>#{table.rowIndex + 1}</h:column> Note that the code is as-is and that the EL variable name table is fully to your choice. See also: How does the ‘binding’ attribute work in JSF? … Read more