Beans with JSF  
Author Message
csharpdotcom





PostPosted: 2007-11-12 1:32:00 Top

java-programmer, Beans with JSF I'm pretty new to programming with JSF and beans, and have been
reading up the documentation in the book "Core JavaServer Faces"
by D.Geary and C. Horstmann as well as online, and managed to
get some simple programs to run, so at least I now understand
some of the basics.

However, I've got quite a lot of existing code in HTML and
JavaScript that works, although not fully cleaned up yet, and I want
to make as few changes as possible in order to modify it for using
beans with JSF. Specifically, some months ago I wrote some
client-side code which is backed up online at
http://csharp.com/simulator/simulator.html, which I'm in the process
of modifying for JSF. The links at the bottom take you to
.../option1.html and .../option2.html, with the original source code
at
respectively .../option1.txt and .../option2.txt. At the moment the
code is not linked to code on a server, so only the JavaScript works
with the forms.

The index page didn't need much work, with the main change being
to include the JSF tags at the top (I'm leaving work on the counter
until later). The option1 page is mostly one large form, and what I
have since done is to keep the form as it is, except that it only
operates with JavaScript, with the post, action, and JavaScript
function calls at the beginning removed, together with the "Submit"
and "Reset" buttons at the end. I put these two buttons together
with calls to the JavaScript in a JSF form immediately below the
end of original form, and after making a few changes to the
JavaScript, got the JavaScript to work correctly and validate the
form.

My question is how do I send to the server the contents of the
original form, including the hidden fields? Do I have to create a
hidden field in the JSF form for each value entered in the main form,
and create a bean for each of these values, or is there some way
of sending the whole contents of the form to a bean, which can then
parse the values. I want to avoid turning the original form into a
JSF form, as it would probably mean making many changes to the
JavaScript, which would require extra work and debugging.

Exactly the same will be done with the option2 page once option1
has been sorted out, and I would be most grateful for some advice.

Christopher Sharp

 
CK





PostPosted: 2007-11-12 4:22:00 Top

java-programmer >> Beans with JSF Words to the wise, csharpdotcom <email***@***.com> wrote:

*snip*

Why not use a PageBean for each page which then handles all the
validation and computation that you need?

- Me, lately.
 
csharpdotcom





PostPosted: 2007-11-12 6:43:00 Top

java-programmer >> Beans with JSF On 11 Nov, 21:22, CK <email***@***.com> wrote:
> Words to the wise, csharpdotcom <email***@***.com> wrote:
>
> *snip*
>
> Why not use a PageBean for each page which then handles all the
> validation and computation that you need?
>
> --
> Claus Dragon <email***@***.com>
> =(UDIC)=
> d++ e++ T--
> K1!2!3!456!7!S a27
> "Coffee is a mocker. So, I am going to mock."
>
> - Me, lately.

Many thanks, but what is a PageBean and how do I create one?

For a single bean I tried the following code in the JSF form
with the "Submit" and "Reset" buttons (put in below the normal
form mentioned previously):

<h:form id="submitform">
<!-- Get a test value held in "physics" using the DOM
and send it to a bean -->
<h:inputHidden name="physics" id="physics"
value=opt1form.physics.value />
<h:commandButton id="submit" value="Submit"
onclick="return readOpt1Form()" type="submit"/>
<h:commandButton id="reset" value="Reset"
onclick="resetForm()" type="reset"/>
</h:form>

In the faces-config.xml file I put in "physics" as the
managed bean, name and "com.csharp.Option1Bean" for the
managed bean class, and in the Option1Bean I created the
method "setPhysics" which is supposed to pick up the
contents of "physics" in the HTML code and write it to a
test file. The code for the bean is:

package com.csharp;

import java.io.*;

public class Option1Bean {
private String myphysics;

public void setPhysics(String physics) {
physics=myphysics;
try {
BufferedWriter fout = new BufferedWriter(new
FileWriter("testout.txt"));
fout.write("Physics = " + physics);
}
catch (IOException e) {}
}
}

which compiles OK. However, in trying to call up the bean
in Glassfish I get the HTTP 500 error message with the
exception happening in the line with the inputHidden code.
It states that a quote symbol was expected, whatever that
means. Obviously, something is wrong with the syntax in
trying to pass a reference from the DOM to a bean.

It would be nice to know what the correct syntax is for
this, although as I have a large (and variable) number of
items to send to the sever, it would obviously make more
sense to send everything on the page at once, then parse
it on the server.

Some suggestions would be most appreciated, particularly
as I'm working at a university in France and appear to be
the only person writing code for a webserver in the
department, so I'm effectively on my own.

Christopher Sharp