tags:

views:

508

answers:

2

i have very basic question about How JSF2.0 binds Managed Beans with xhtml?

say i have inputtext with value="#{MymanagedBean.property}"

how this MymanagedBean reference is resolved in JSF 2 ?

please consider following points while answering.

  1. in ealier version i.e. with JSF 1.2 we have to write binding in faces-config.xml but with JSF 2 it is not mandatory to have faces-config.xml

  2. if you do view source of xhtml pages.. you will not find anywhere MymanagedBean reference.

then how it is done?

+3  A: 

in ealier version i.e. with JSF 1.2 we have to write binding in faces-config.xml but with JSF 2 it is not mandatory to have faces-config.xml

In JSF2 this is done by the @ManagedBean annotation.

@ManagedBean
public class MymanagedBean {
    // ...
}

During webapp startup, JSF will scan the classpath for all classes with this annotation and collect them in the memory.

if you do view source of xhtml pages.. you will not find anywhere MymanagedBean reference.

That's correct. JSF runs at the server machine, produces a (X)HTML page and the webserver sends it to the webbrowser. The webbrowser has no notion of JSF or any other server side languages. The webbrowser only understands HTML, CSS and JavaScript.

BalusC
thank u BalusC.. one more think can i call my managed bean method purely trough JavaScript only and not with f:ajax tag .. just little curiosity you can say about how ajax is woking with jsf 2.
rahul_d_m
You can do a `form.submit()` to submit a POST form, or change the `window.location` to fire a new GET request (the constructor of any request scoped bean will then be invoked).
BalusC
Hmmmm. I tried switching from XML to annotations for a sample app and it doesn't appear to find the bean with the annotation. Worked fine with the XML though. Are there other annotations that must be used too?
Brian Knoblauch
Cay Horstmann advocates using CDI instead of JSF to handle this (decouples from the JSF API, which is nice for testing). http://weblogs.java.net/blog/cayhorstmann/archive/2010/01/03/how-stay-away-jsf-api
Thorbjørn Ravn Andersen
A: 

During webapp startup, JSF will scan the classpath for all classes with this annotation and collect them in the memory.

Wow! That sure will make application startup slow especially if your application has thousands of class files.

Nerrve
Uh... This isn't an answer.
harto