Jan Hornbøll Hansen wrote:
> I've been trying to learn JSF last couple of days. After reading a few
> chapters in J5EE tutorial I began studying guessNumber example. I may
> not understand some of the finer points of this code, but it seems like
> a horrible example to learn from. I don't think I have to explain how
> unfortunate it is if examples are obscure. Developers who are new to
> this technology need clear examples without any unnecessary stuff.
>
You're right. Clarity is important.
> What is this example supposed to illustrate?
> I would imagine its supposed to show how UserNumberBean and
> greetings.jsp and response.jsp work together.
Right.
> If that is the case is
> MessageFactory needed in this example at all?
> If MessageFactory is needed, why is it so complicated?
> Why are methods that are never called included?
>
I think this is a case of 'cut and paste' The MessageFactory is
used in this example to get access to the standard messages.
However, I think you're right, it could be simplified.
> UserNumberBean is equally verbose and obscure.
> Why minSet and maxSet attributes? Why not just set min and max to some
> initial value (like Integer.MIN_VALUE & Integer.MAX_VALUE)?
> What is the point of intValue method? Seems like parameter passed to
> this method will allways be instanceof Integer.
>
I can't comment on why. This code is pretty old and hasn't really been
touched
since the 1.0 days. We'll see what we can do about cleaning it up based
on your
feedback.
> As previously mentioned I may not fully understand code. I have none the
> less attached my version of UserNumberBean. I hope this illustrates that
> UserNumberBean can simplified considerably.
>
> Kind Regards
> Jan Hornbøll Hansen
>
> ------------------------------------------------------------------------
>
> package guessNumber;
>
> import javax.faces.application.FacesMessage;
> import javax.faces.component.UIComponent;
> import javax.faces.context.FacesContext;
> import javax.faces.validator.Validator;
> import javax.faces.validator.ValidatorException;
>
> import java.util.Random;
>
>
> public class UserNumberBean {
>
> Integer userNumber = null;
> Integer randomInt = null;
> String response = null;
>
> private int maximum = Integer.MAX_VALUE;
> private int minimum = Integer.MIN_VALUE;;
>
> protected String[] status = null;
>
> public UserNumberBean() {
> Random randomGR = new Random();
> randomInt = new Integer(randomGR.nextInt(10));
> }
>
> public void setUserNumber(Integer user_number) {
> userNumber = user_number;
> }
>
> public Integer getUserNumber() {
> return userNumber;
> }
>
> public String getResponse() {
> if (userNumber != null && userNumber.compareTo(randomInt) == 0) {
> return "Yay! You got it!";
> } else {
> return "Sorry, " + userNumber + " is incorrect.";
> }
> }
>
> public String[] getStatus() {
> return status;
> }
>
> public void setStatus(String[] newStatus) {
> status = newStatus;
> }
>
> public int getMaximum() {
> return (this.maximum);
> }
>
> public void setMaximum(int maximum) {
> this.maximum = maximum;
> }
>
> public int getMinimum() {
> return (this.minimum);
> }
>
> public void setMinimum(int minimum) {
> this.minimum = minimum;
> }
>
> public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
> if (context == null || component == null) {
> throw new NullPointerException();
> }
>
> if (value != null) {
> Integer i = (Integer)value;
> int converted = i.intValue();
> if (converted > maximum) {
> Object[] objects = {new Integer(minimum),new Integer(maximum)};
> FacesMessage msg = MessageFactory.getMessage(context,Validator.NOT_IN_RANGE_MESSAGE_ID,objects);
> throw new ValidatorException(msg);
> }
> if (converted < minimum ) {
> Object[] objects = {new Integer(minimum),new Integer(maximum)};
> FacesMessage msg = MessageFactory.getMessage(context,Validator.NOT_IN_RANGE_MESSAGE_ID,objects);
> throw new ValidatorException(msg);
> }
> }
> }
> }
>
>
> ------------------------------------------------------------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_javaserverfaces.dev.java.net
> For additional commands, e-mail: dev-help_at_javaserverfaces.dev.java.net
>