• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Keeping text hidden until a button is pressed?  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am in school for Java, and this week's homework is to write a JSF script that will allow a user to use a website to enter some information and it will calculate their taxes. This script is VERY basic, you just enter your yearly income and filing status and click Calculate. The example picture shows the output in red below the Calculate button. But this text doesn't show up *until* the button is pressed. This is the last piece I need to get working. I currently have the red text
1.jpg
[Thumbnail for 1.jpg]
2.jpg
[Thumbnail for 2.jpg]
 
David Swordsman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we not edit our posts? I accidentally clicked Submit before I finished my above post.
 
Bartender
Posts: 19801
93
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, if it's JSF, it's not a script. A script is usually interpreted, where JSF code is compiled.

Aside from that, you can do this in JSF by simply referencing a boolean property on a backing bean, like so:



Make your button click method set the value of the "showMe" property to true to make the text visible, and false to hide it. The "rendered" attribute can be applied to many JSF elements, including entire tables.
 
David Swordsman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Technically, if it's JSF, it's not a script. A script is usually interpreted, where JSF code is compiled.

Aside from that, you can do this in JSF by simply referencing a boolean property on a backing bean, like so:



Make your button click method set the value of the "showMe" property to true to make the text visible, and false to hide it. The "rendered" attribute can be applied to many JSF elements, including entire tables.



Thanks. I have added the rendered, but now the red text doesn't appear at all. Here is my updated code:


The I have added a boolean variable named result and when the user clicks the Compute button, the setResult() method sets the boolean to true. But the red text never appears :(.
 
Tim Holloway
Bartender
Posts: 19801
93
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most likely because of this:


This should be an EL property reference, not a function call. That is:
 
David Swordsman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Most likely because of this:


This should be an EL property reference, not a function call. That is:



Ok, I think I know what was going on. I'm guessing the JSF looks for getters that are associated with the property names (maybe?). So when it was executing the "computeTaxBean.result" (which "result" is my boolean flag) it was actually trying to access the "getResult()" method which is what displays the red text, but since there was no boolean, it wasn't showing. I changed my "result" boolean to "boolResult" and tried to run it. This resulted in an error saying there was no "boolResult" property. So I added a "getBoolResult()" method that returned the "boolResult" boolean and it all started working :)

Thank you for your assistance.
 
Tim Holloway
Bartender
Posts: 19801
93
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you really shouldn't be doing "function calls" in EL. The example I showed you should work.
 
David Swordsman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:No, you really shouldn't be doing "function calls" in EL. The example I showed you should work.



Well that pic is the error I get when I try to access the variable without having a getter function. Also, how do you tell the JSF that something is supposed to "set" a variable without calling it's setter function?
3.jpg
[Thumbnail for 3.jpg]
 
Tim Holloway
Bartender
Posts: 19801
93
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you do a property reference in Expression Language (EL), then the system assumes that for a property named "x", then it should automatically call getX() when it needs to render and setX() when it needs to transfer data from a form to a backing bean. Which is why you do not need - and should not have an explicit function call (the other reason is that web pages are not executable code anyway).

I wish you had done a cut-and-paste of the screen text. The picture doesn't show up on the message editor and besides pictures eat up storage on our servers.

The error message that you pasted would imply that the backing bean named computeTaxBean does not implement a method whose signature is:


Actually "getBoolResult()" should work, too, but the POJO standard for boolean property-get methods is the "isXXX" form.
 
David Swordsman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:When you do a property reference in Expression Language (EL), then the system assumes that for a property named "x", then it should automatically call getX() when it needs to render and setX() when it needs to transfer data from a form to a backing bean. Which is why you do not need - and should not have an explicit function call (the other reason is that web pages are not executable code anyway).

I wish you had done a cut-and-paste of the screen text. The picture doesn't show up on the message editor and besides pictures eat up storage on our servers.

The error message that you pasted would imply that the backing bean named computeTaxBean does not implement a method whose signature is:


Actually "getBoolResult()" should work, too, but the POJO standard for boolean property-get methods is the "isXXX" form.



Thank you for all of this. I will try and keep the picture thing in mind.

It's been a while since I've programmed, so thank you for the reminder about the IS when working with booleans.

I will rework my.... what is the correct term for this? Is it defined as a "program"? to reflect the information you have given me.

Thank you.
 
Tim Holloway
Bartender
Posts: 19801
93
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terminology:

A "program" for the web is commonly known as a web application. Unlike regular application programs, a webapp doesn't run independently, but instead its code is invoked by the webapp server when a web request comes in, the application code constructs a response, then the webapp server sends that response back to the client. Repeat if/as required.

JavaServer Faces is a web application framework. It facilitates common webapp tasks according to the Model/View/Controller application design paradigm. Among other things, it constructs and initializes resources (Managed Beans) on demand and passes data between Model and View (both directions) using the Inversion of Control (IoC) design paradigm. It also compiles View Templates (xhtml) to construct a Component Tree which is basically a Directed Acyclic Graph (DAG) used to help render the View for the Client (in other words, generates HTML), to manage form values coming from a client View - including validation - and to know what properties and methods are used in relation to the View.

Unlike many other MVC frameworks, you almost never design or code Controllers in JSF, only Models and Views. The Controllers in JSF are the FacesServlet and the code that implements the internal functions of the JSF View Template tags.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!