0

I am learning JSF and was trying to implement a small exercise on EL (with JSF page and JSP2). i found a strange behavior and completely clueless of the reason behind it. Please find my code below and scenario

Backing bean class

package task2;  
public class FavortiteColors_1 {  
private String firstName = "Tarun";  
public String getFirstName() {  
    return firstName;  
    }  
public void setFirstName(String firstName) {  
    this.firstName = firstName;  
    }  
}

Below is the snapshot of faces config xml which is required for this example Faces config.xml

<managed-bean>  
    <managed-bean-name>favColors_1</managed-bean-name>  
    <managed-bean-class>task2.FavortiteColors_1</managed-bean-class>  
    <managed-bean-scope>request</managed-bean-scope>  
</managed-bean>  

JSP Code

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>  
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>  
<f:view>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
<HTML>  
    <HEAD><TITLE>Accessing Bean Properties</TITLE>  
        <LINK REL="STYLESHEET"  
        HREF="./css/styles.css"  
        TYPE="text/css">  
    </HEAD>  
    <BODY>  
        <tr><B>Accessing Bean Properties using JSP 2 EL: version 1<B></tr>  
        <% System.out.println("Accessing Bean Properties using JSP 2 EL: version 1");        %>  
        <OL>  
            <LI>First Name ${favColors.firstName}  
       </OL>  
       <BR><BR>  
       <tr><B>Accessing Bean Properties using JSF 1.1 EL <B></tr>  
       <% System.out.println("Accessing Bean Properties using JSF 1.1 EL"); %>  
       <UL>  
           <LI>First Name = <h:outputText   
           value="#{favColors.firstName}"/>  
       </UL>  
       <BR><BR>  
       <tr><B>Accessing Bean Properties using JSF 1.1 with JSP2EL: version 2<B></tr>  
       <% System.out.println("Accessing Bean Properties using JSF 1.1 with JSP2EL: version 2"); %>  
       <OL>  
           <LI>First Name ${favColors.firstName}  
       </OL>  
       <BR><BR><BR>  
       <P>  
   </f:view>  

*The output is as below: *

 Accessing Bean Properties using JSP 2 EL: version 1 
 First Name 

 Accessing Bean Properties using JSF 1.1 EL 
 First Name = Tarun 

 Accessing Bean Properties using JSF 1.1 with JSP2EL: version 2 
 First Name Tarun 

With this the background below are my questions

Q1: I am trying to fetch a string from the backing bean using JSF 1 El and JSP 2 EL. I am trying to fetch the name using three expressions. First and 3rd one are JSP2 EL and the funny thing is that the first one is not able to fetch the name where as the third one (with the exactly same syntax is able to fetch the name. Using the SOP, I am getting the that first expression is unable to trigger the backing bean whereas the second and third one are doing it successfully. Why is such a scenario happening? Looking forward for your explanation.

Regards Tarun

Tarun Bhatt
  • 727
  • 2
  • 8
  • 28
  • Your jsp code is not properly formed. – Aritz Jan 14 '14 at 07:52
  • apologies , new to the forum so I am not aware of the protocols of asking question. The JSP is giving no compilation issues. Its just that the output is not as expected. Please elaborate on what do you mean by "Your jsp code is not properly formed" – Tarun Bhatt Jan 15 '14 at 10:34
  • I mean, tags are not being properly closed, are they? Look at the `li` and `br` tags. – Aritz Jan 15 '14 at 10:37
  • Apart from that, I highly encourage you to get rid from scriptlets and use jstl if you want to get into JSF. Scriptlets are an ancient technology which should be avoided, as injecting pure Java code into view files (either facelets or jsp) is discouraged. See this http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – Aritz Jan 15 '14 at 10:43
  • JSF (or your JSF implementation) automagically creates backing beans and/or places them in the request scope when they are first requested. That's probably what happens when you first use JSF EL. That said, this is an implementation detail you shouldn't really care about in the first place. Mixing JSP, JSF, and (oh god) scriptlets in the same page is a scenario that probably does not have any usefully specified behaviour, mostly because the JSF lifecycle is rather complex, and the JSP execution model doesn't actually work with it well at all. (Which is why we have Facelets now.) – millimoose Jan 15 '14 at 11:42
  • Stop. JSF 1.x and JSP are obsoleted/deprecated more than 4 years ago. You should focus on learning JSF 2.x and Facelets. – BalusC Jan 15 '14 at 19:06
  • Thanks all, Actually the application I support is written in JSF 1. but I gt the point. Have to start reading JSF2. Thanks all for your inputs. – Tarun Bhatt Jan 16 '14 at 06:12

0 Answers0