aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Question regarding javaeetutorial.pdf -- about .xhtml and .java files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Question regarding javaeetutorial.pdf -- about .xhtml and .java files" Watch "Question regarding javaeetutorial.pdf -- about .xhtml and .java files" New topic
Author

Question regarding javaeetutorial.pdf -- about .xhtml and .java files

Arie Morgenstern
Greenhorn

Joined: Oct 30, 2012
Posts: 11
    
    1
I am working through the Java EE Tutorial document javaeetutorial.pdf, and have a question regarding one of their simpler EE Project.
This is my first post to JavaRanch, so I am not positive on the etiquette. I will post the question, and the associated code.

The Java managed bean is quite simple, and the xhtml files are simple too. But, I am confused with the Expression Language found in the xhtml files. It says #{hello.name}.
Is Expression Language not case sensitive?
How does it interface with the attribute "name" when name is "private"?
Lastly, I am to assume one xhtml file is setting the attribute name, and the other is getting it. But, the Expression Language is the same #{hello.name}. How does that work?

Thanks so much for any help!









Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1096
    
  10

Hi Moxy,
Welcome to the Ranch

Expression language is indeed case-sensitive. The reason 'hello' works is: for any managed bean, a 'name' is generated. This name follows the regular JavaBean convention.
i.e the first letter of the class name is changed to lower case and the rest remains as it is - for example, if the Java class name was HelloBean, the EL would refer it as helloBean. Imagine creating an instance of the Java class. You would typically declare with a variable like:

The same convention is used here.
For properties, when EL refers, hello.name, behind the scenes it calls the getter method - in this case getName(). In the second .xhtml file, you are making a call just directly in the html, so, it needs to first get the value which it does by calling the getName() method.

Your question about how it identifies when to call the getter and setter is a good one actually. The .xhtml are facelets which is the view technology for JSF pages. JSF has phases and there is a phase called Update Model Values and so, during this phase the setter methods are used to move the value from the html form to the JavaBean property. In the Render Response phase, JSF has to display the html, so, it calls the getters during this phase.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Arie Morgenstern
Greenhorn

Joined: Oct 30, 2012
Posts: 11
    
    1
Ranganathan,

Thanks for the welcome. I don't have someone around me to ask (at times) boneheaded questions, so I hope the Ranch gives me the space for it. Unfortunately, I am too green to contribute back at this time.

So, it is convention, a pre-agreed upon format if you will, that takes the Class Hello, and converts it into 'hello' in Expression Language. I would assume then, that if I deviated from Java's convention and called the class 'hello' instead of 'Hello', EL would get lost?
Second followup, you say EL takes 'hello.name' and uses the getter and setter. So, it has nothing to do with the Java attribute being declared private or public? But, may I assume then that if I deviated from Java's convention, and called the getter method getTheName, then EL would be lost?

If my assumptions are correct, then I find this quite interesting. The whole technology of JavaBeans/JSF Pages, etc., is based on Java syntax conventions that are not enforced by the compiler. Is that accurate?

I thank you for your discussion on the phases, I have allot of reading to do in that area yet.
What I was wondering though, was if both EL expressions are #{hello.name}, how does it know to use the getter in one instance and a setter in the other instance. It makes perfect sense to me, but what in the code tells the ... the ... server, I guess ... to use the getter vs. the setter?


Thank you very much,

Moxy
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1096
    
  10

Moxy Fruvous wrote:I don't have someone around me to ask (at times) boneheaded questions, so I hope the Ranch gives me the space for it.

Sure, that is what the ranch is meant for.

Moxy Fruvous wrote:Unfortunately, I am too green to contribute back at this time.

Not a problem. We have all been green at some point of time.

Moxy Fruvous wrote:So, it is convention, a pre-agreed upon format if you will, that takes the Class Hello, and converts it into 'hello' in Expression Language.

Yes, but note that, like I pointed out earlier, an instance of the class Hello is created. The name of the instance is, by convention, 'hello' and that is used. So, it is like you are using an instance of the class Hello in another place (like we do typically between pure Java classes)

Moxy Fruvous wrote:I would assume then, that if I deviated from Java's convention and called the class 'hello' instead of 'Hello', EL would get lost?

Correct. However, if someone insists on using a different name, that can be done as:

You can then use this 'Hello' within your EL. But, that would be poor convention and also misleading/confusing.

Moxy Fruvous wrote:Second followup, you say EL takes 'hello.name' and uses the getter and setter. So, it has nothing to do with the Java attribute being declared private or public? But, may I assume then that if I deviated from Java's convention, and called the getter method getTheName, then EL would be lost?

Correct. The EL would be lost.

Moxy Fruvous wrote:The whole technology of JavaBeans/JSF Pages, etc., is based on Java syntax conventions that are not enforced by the compiler. Is that accurate?

Correct. You would get an error at runtime if what the EL refers to is not valid. However, some IDEs provide support in the editor by marking wrong expressions in the jsf page with a red line or so (like in a word processor). So, good tooling support can help you detect this problem at development time.

Moxy Fruvous wrote:What I was wondering though, was if both EL expressions are #{hello.name}, how does it know to use the getter in one instance and a setter in the other instance. It makes perfect sense to me, but what in the code tells the ... the ... server, I guess ... to use the getter vs. the setter?

Yes, like I mentioned earlier, the 'phase' would help the server to decide that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding javaeetutorial.pdf -- about .xhtml and .java files