This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A Very Newbie JSF Question

 
John Douglas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All

I am very new to JSF (v2.0) and I am attempting to learn it at places like netbeans.org and coreservlets.com. I am working on a very simple "add/subtract/multiply/divide" Java webapp and I have run into a problem. When I first started out, the application was enter two numbers and hit a '+' key and they would be automatically added together. Now that I have added more complexity I am having trouble getting the operation to the managed bean. This is what I had when it was just "add":



For the "answer" page, I displayed the answer like this:



I had the proper getters and setters in the Calculator.java managed bean and the operation worked perfectly.

Now I have added the other three operations and I am having trouble visualizing how to get the operation parameter to the bean so that I can switch around it. I tried this:



However, Glassfish complained that I have already used "operation" once and I am trying to use it four times here.

Any adivce/tips on how to get multiple operations to the managed bean so that it can preform the desired operation?

Thank you for taking the time to read.
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John Douglas.

I've tried to test something like you do and I got the error: javax.servlet.jsp.JspException: Duplicate Id Found:operation
on Tomcat 6.
So we can't declare more than one component to have the same id.

There is some solutions such as using parameters to be sent with URL or setting attribute values for your commandButton components. For more details using these kind of solutions take a look at: Communication in JSF - From BalusC

But I prefer to define an action method for each operation inside the Calculator bean so it can do the right operation, hold the result in some Calculator attribute and then return the outcome string "answer".

I wrote something to give you some example:

Code for the jsp.


Code for the managed bean:
 
John Douglas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow ... thank you for the detailed response. I feel it's going to take me forever to pick this up.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!