JSF & Facelets Flow

I have a dynamic Facelets page that needs to show information from database when the page loads. At this point in the flow, there have not been any form submissions. Every JSF example I can find only shows a form submission with dynamic results on the next page.

Every call I make to our database is currently takes place after an action has been triggered by a form submission. Where should this code go if there hasn't been a form submission, and how do I trigger it? A code snippet would really help me out!

Asked by: Ned675 | Posted: 21-01-2022

Answer 1

You should be able to do your initialization work in the constructor (or lazily in one of your accessors) of your managed bean.

Answered by: Carina415 | Posted: 22-02-2022

Answer 2

If you're using Spring integration (see here also), it's easy.

In your backing bean, simply use something like:

public class BackingBean implements InitializingBean
    public void afterPropertiesSet()

If you're not integrating with Spring there are two options:

  1. Load the initial data in the class constructor;
  2. In your faces-config.xml, you can set properties to be injected. Properties are guaranteed to be set in the order they're specified in the config file. So, just create a dummy property and then within that method load up your default data. i.e. create a method public void setLoaded(boolean loaded) { loadInitialData(); }, and in your faces-config.xml have 'loaded' being set as a property on that backing bean.

Hope this is all clear!

Answered by: Caroline768 | Posted: 22-02-2022

Answer 3

You write (with my emphasis added):

Every call I make to our database is currently takes place after an action has been triggered by a form submission. Where should this code go if there hasn't been a form submission, and how do I trigger it? A code snippet would really help me out!

It sounds to me that you want to retrieve information from the database prior to form submission.

It seems to me that you want to make an Ajax call to query the database. The Ajax call can fire on a different event than the form submisson event. This will probably entail using Javascript rather than the Faces framework.

Answered by: Michael781 | Posted: 22-02-2022

Similar questions

java - ForEach and Facelets

My managed bean : public List<String> getLiQuickNav(){ System.out.println("I'm here..."); List<String> l = new ArrayList<String>(); l.add("toto"); l.add("tata"); l.add("titi"); return l; } My forEach : <c:forEach var="categorie" items="#{mainControleur.liQuickNav}"> <h:outputLabel value="${categorie}"/>...

java - Why won't my Facelets loop variable go out of scope?

I know this looks like a lot of text, but I think it's a pretty simple concept I'm missing. I'm writing a web application with Facelets. I've got a custom tag rq:request-list that takes a list of requests as a parameter and outputs a lovely table to display them. So far, so good. rq:request-list starts out like you'd expect: <!-- ... --> <ice:dataTable value="#{list}" var="req">...

java - How can I pass a dynamic backing bean into a JSF 2.0 page using facelets?

I am using a JSF 2.0 to create a web app (purely jee6, without spring/seam etc.). I would like to have a single xhtml page but pass the proper backing bean / entity into it. For example, I would like to be able to edit a user other than the logged in user, I have a user edit page which displays the information of the logged in user (being tracked by my session), I would like to instead pass in a user selected from a list a...

java - Using JSP 2.0 tags in Facelets

Is someone using Facelets with JSP 2.0 tags? How to add tag library to xhtml page? In JSP I used: <% taglib prefix="example" tagdir="/WEB-INF/tags/my" %> and : <example:sample/> How can I do the same in facelets with JSP 2.0 tags?

java - How to make facelets work?

I have created a new web project and enabled its JSF and Facelets capabilities on MyEclipse, then I deployed it over weblogic. Everything look fine but it does not work for me, it seems that the view-handler is not called at all. What is wrong with it? Could anyone help me? This is my web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun....

Java Facelets and session?

How can I use session for facelets ? What's the syntax...? I would put a code like this <% String loginSession = (String)session.getAttribute("login"); %> <% if(loginSession != null){ %> Welcome <%= session.getAttribute("firstName") %> ! <% }else{ %> Guest <% } %> Thanks

java - JSF vs Facelets vs JSP

This question already has answers here:

java - How can I tell the Facelets page, what object it shall load, when the user returns the form?

I had a problem, which I solved, but I feel like my solution is a bad hack. Is there a better way? I have a page, on which I placed the form, which shows properties of some object, as in example (obvious details omitted). Ticket.java: @Entity public class Ticket { @Id private Long id; private String title; private byte priority; // Getters, setters... } ...

java - Facelets parsing problem using JBoss v4.2.1

I have a some.xhtml file with jsf libs in it. In <h:body> tag I am trying to display 2 words: For the first one I use plain text: Hello For the second one I use: <h:outputText value=" there"/> I expect to see "Hello there", but only Hello is printed on the page. So I assume that the facelet is not parsed somehow in JBoss 4.2.1. It worked Apache 7. How can I fix ...

java - Where is Facelets?

I am trying to use Facelets with MyFaces 1.2. It appears as though the Facelets download at http://facelets.dev.java.net no longer exists. Does anyone know 1) where I can download Facelets? 2) why has Facelets been removed from this site? e.g. has it been integrated into JSF 2.0 or something?

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)