Misra’s blog

JSF notes

Posted by mtwinkle on July 8, 2007

jsfintro-lifecycle.gif

What is JSF and how is it useful?

JSR-127

FacesServlet is the Controller for the entire web application.

Every JSF page that contains JSF specific markup will begin with the <f:view> tag.

The faces-config.xml file is the heart of a JSF web application. In this file we define our Java Beans and our navigation rules.

JSF is a server side user interface component framework for Java based web apps.

One of the greatest advantages of JavaServer Faces technology is that it offers a clean separation between behavior and presentation.

JavaServer Faces technology provides a rich architecture for managing component state, processing component data, validating user input, and handling events.

For example, page authors with no programming expertise can use JavaServer Faces technology UI component tags to link to server-side objects from within a web page without writing any scripts.

Life Cycle of a JSF page

The life cycle of a JavaServer Faces page is similar to that of a JSP page: The client makes an HTTP request for the page, and the server responds with the page translated to HTML.

Click a hyperlink on an HTML page which opens a JSF page.

Request- non-Faces. Response – Faces

When a request for a JavaServer Faces page is made, such as when a link or a button is clicked, the JavaServer Faces implementation begins the restore view phase. During this phase, the JavaServer Faces implementation builds the view of the page, wires event handlers and validators to components in the view, and saves the view in the FacesContext instance. All the application’s component tags, event handlers, converters, and validators have access to the FacesContext instance.

After the component tree is restored, each component in the tree extracts its new value from the request parameters by using its decode method. The value is then stored locally on the component. If the conversion of the value fails, an error message associated with the component is generated and queued on FacesContext. This message will be displayed during the render response phase, along with any validation errors resulting from the process validations phase.

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSFIntro10.html

 

Main advantage is that it’s going to be part of J2EE 5.0.

1. Makes it easy to construct a UI from a set of reusable UI components

2. Helps manage UI state across server requests

3. Allows custom UI components to be easily built and re-used

4. Simplifies migration of application data to and from the UI

Difference between Struts and JSF or Why would someone choose JSF over classic JSP and Struts?

Struts is a Controller Framework. There are pieces of it that operate as a View- like, HTML tags and Tiles library.

JSF is a View Framework. It provides a UI component model that enables a much richer component set and user interaction model.

What is JSF UI component?

A user interface control that outputs data to a client or allows a user to input data to a JSF application.

What is JSF conversion model?

A mechanism for converting between string-based markup generated by JSF UI components and server-side Java objects.

2. How to pass a parameter to the JSF application using the URL string?

http://your_server/your_app/product.jsf?id=777

FacesContext fc = FacesContext.getCurrentInstance();
String id = (String) fc.getExternalContext().getRequestParameterMap().get("id");

<h:outputText value="#{param['id']}" />

3. How to add context path to URL for outputLink?

<h:outputLink value="#{facesContext.externalContext.requestContextPath}/myPage.faces">

4. How to get current page URL from backing bean?

FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest()

 context.getViewRoot().getViewId();
will return you the name of the current JSP

5. How to access web.xml init parameters from java code?

You can get it using externalContext getInitParameter method.

<context-param>
 <param-name>connectionString</param-name>
 <param-value>jdbc:oracle:thin:scott/tiger@cartman:1521:O901DB</param-value>
</context-param>

FacesContext fc = FacesContext.getCurrentInstance();

String connection = fc.getExternalContext().getInitParameter("connectionString");



6. How to access web.xml init parameters from jsp page?

Product Id: <h:outputText value="#{initParam['productId']}"/>

7. How to terminate the session?

public String logout() {

  FacesContext fc = FacesContext.getCurrentInstance();
  HttpSession session = (HttpSessionfc.getExternalContext().getSession(false);
  session.invalidate();
  return "login_page";
}

<% session.invalidate(); %> <c:redirect url=”loginPage.jsf” />

8. How to implement “Please, Wait…” page?

9. How to reload the page after ValueChangeListener is invoked?

At the end of the ValueChangeListener, call FacesContext.getCurrentInstance().renderResponse()

10. How to download PDF file with JSF?

public void viewPdf(ActionEvent event) {
 String filename = "filename.pdf";

 // use your own method that reads file to the byte array
 byte[] pdf = getTheContentOfTheFile(filename)

 FacesContext faces = FacesContext.getCurrentInstance();
 HttpServletResponse response = (HttpServletResponsefaces.getExternalContext().getResponse();
 response.setContentType("application/pdf");
 response.setContentLength(pdf.length);
 response.setHeader"Content-disposition""inline; filename=\""+fileName+"\"");
 try {
  ServletOutputStream out;
  out = response.getOutputStream();
  out.write(pdf);
 catch (IOException e) {
  e.printStackTrace();
 }
 faces.responseComplete();
}

This is a jsp file snippet:

<h:commandButton immediate="true" actionListener="#{backingBean.viewPdf}" value="Read PDF" />

11. How to show Confirmation Dialog when user Click the Command Link?

12. What is the different between getRequestParameterMap() and getRequestParameterValuesMap()

13. Is it possible to have more than one Faces Configuration file?

Yes

14. How to mask actual URL to the JSF page?

15. How to print out html markup with h:outputText?

In case of <h:outputText escape=”false” value=”<b>This is a text</b>”/>

16. h:inputSecret field becomes empty when page is reloaded. How to fix this?

Set redisplay=true, it is false by default.

4 Responses to “JSF notes”

  1. TestName said

    qX3xGR Test myfunction comment

  2. Done busko said

    Dear Misra,
    This article is very nice.
    first i am explaning you the scenario
    1) first jsf page i have id in output text value and a command button
    2) if I click on command button I want to send the value in text box to next page how can I achieve this

    is by using hidden field or by using param tag

  3. Andrii said

    useful, tnx

  4. cheap content…

    […]JSF notes « Misra’s blog[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: