users
[Top] [All Lists]

RE: [cinjug-users] newbie trying to learn m-v-c

To: <users@xxxxxxxxxx>
Subject: RE: [cinjug-users] newbie trying to learn m-v-c
From: "James Carman" <james@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 9 Sep 2004 09:39:18 -0400
Delivered-to: mailing list users@cinjug.org
Importance: Normal
In-reply-to: <000601c49672$1206f320$d040058f@carmani600m>
Mailing-list: contact users-help@cinjug.org; run by ezmlm
Organization: Carman Consulting, Inc.
Note, this implementation relies upon the fact that you've used keys in your
hashtable that do NOT contain any weird characters, so they really look like
JavaBean property names.  The JSTL can treat any Map object as a JavaBean!
It just uses the property name as the key for a lookup in the Map.  

-----Original Message-----
From: James Carman [mailto:james@xxxxxxxxxxxxxxxxxxxx] 
Sent: Thursday, September 09, 2004 9:37 AM
To: users@xxxxxxxxxx
Subject: RE: [cinjug-users] newbie trying to learn m-v-c

You may want to change how you do error messages.  The JSTL has a set of
internationalization tags (fmt prefix) for internationalizing messages.
But, to get it working in your current code, you must expose the "errors"
property (you should be using HashMap rather than Hashtable) on your form,
using a getter.  Then...

<c:out value="${formToValidate.errors.firstName}" />

-----Original Message-----
From: sdgesa gaeharth [mailto:pollux1234567890@xxxxxxxxx] 
Sent: Thursday, September 09, 2004 9:26 AM
To: users@xxxxxxxxxx
Cc: james@xxxxxxxxxxxxxxxxxxxx
Subject: RE: [cinjug-users] newbie trying to learn m-v-c

I tried your tag and it works good!!
<c:out value="${formToValidate.firstName}" />

But how can I "c:out" a function. I get an error when
trying this:
<c:out
value="${formToValidate.getErrorMsg("firstName")}" />

error:
org.apache.jasper.JasperException:
/memberAddForm.jsp(8,53) equal symbol expected


thanks again

--- James Carman <james@xxxxxxxxxxxxxxxxxxxx> wrote:

> Well, if your taglibs are working properly (I assume
> they are, since your
> JSP page runs without any hiccups with the taglib
> declaration in it), then,
> to print out the firstName property of the
> formToValidate "bean", you would
> do...
> 
> <c:out value="${formToValidate.firstName}" />
> 
> Yes, they ACTUALLY named the tag "c:out"!  Must have
> been a C++ programmer
> at one time! :-)  In protest, I usually use a
> different "prefix" for the
> JSTL core library.  Anyway, there is a good book out
> called JSTL In Action
> by Shawn Bayern (Manning Publications), if you're
> looking for a reference.  
> 
> -----Original Message-----
> From: sdgesa gaeharth
> [mailto:pollux1234567890@xxxxxxxxx] 
> Sent: Wednesday, September 08, 2004 11:25 PM
> To: users@xxxxxxxxxx
> Subject: RE: [cinjug-users] newbie trying to learn
> m-v-c
> 
> J Cameran:
> 
> Exactly the answer I was hoping for!!  Can you show
> me
>  how to integrate jstl with my jsp page.  I have
> only
> scratched the surface with jstl documenation and I
> need an example to get started.
> 
> For everyone else...
> I would use a framework except I would rather learn
> from scratch. Later on, if I decide to use a
> framework
> I try it out.
> 
> thanks again.
> 
> 
> --- James Carman <james@xxxxxxxxxxxxxxxxxxxx> wrote:
> 
> > With JSTL (I assume you're using JSTL since you've
> > defined the taglib in
> > your JSP page), you don't need ANY of that
> scriptlet
> > code.  Why not use the
> > JSTL tags to print out your values?  You wouldn't
> > have to worry about
> > casting anything to String.  You wouldn't have to
> > import any classes.  JSTL
> > would take care of all of that for you.  
> >  
> > 
> > -----Original Message-----
> > From: sdgesa gaeharth
> > [mailto:pollux1234567890@xxxxxxxxx] 
> > Sent: Wednesday, September 08, 2004 3:40 PM
> > To: users@xxxxxxxxxx
> > Subject: RE: [cinjug-users] newbie trying to learn
> > m-v-c
> > 
> > Ah, I see. That works. However my original
> question
> > is
> > to pass an object back to the jsp page. The
> > HttpServlet looks like this:
> > 
> > **********BEGIN SERVLET********
> >             MemberValidateForm formToValidate = new
> > MemberValidateForm();
> >             allOk =
> >
>
formToValidate.validate(firstName,lastName,emailAddress,homePhone);
> >             if(allOk){
> >                     toPage = "/MemberAdd";
> >             }else{
> >             
> > req.setAttribute("formToValidate",formToValidate);
> >             }
> >             RequestDispatcher dispatcher;
> >             dispatcher =
> context.getRequestDispatcher(toPage);
> >             dispatcher.forward(req, res);
> > 
> > **********END SERVLET********
> > 
> > the jsp page looks like this:
> > 
> > **********BEGIN JSP********
> > <%@ taglib prefix="c"
> > uri="http://java.sun.com/jstl/core"%>
> > <%@ page import="java.util.*,
> > com.bdi.www.members.MemberValidateForm" %>
> > <%
> > MemberValidateForm doThis= (MemberValidateForm)
> > request.getAttribute("formToValidate");
> > out.println(doThis.getFirstName());
> > %>
> > **********END JSP********
> > 
> > 
> > MemberValidateForm looks like this:
> > 
> > 
> > **********BEGIN MemberValidateForm********
> > 
> > import java.util.*;
> > 
> > public class MemberValidateForm {
> > 
> >   private String firstName;
> >   private String lastName;
> >   private String emailAddress;
> >   private String homePhone;
> >   private Hashtable errors;
> > 
> >   public boolean validate(String firstName,String
> > lastName,String emailAddress,String homePhone) {
> >     boolean allOk=true;
> >     if (firstName.equals("")) {
> >       errors.put("firstName","Please enter your
> > first
> > name");
> >       firstName="";
> >       allOk=false;
> >     }
> >     if (lastName.equals("")) {
> >       errors.put("lastName","Please enter your
> last
> > name");
> >       lastName="";
> >       allOk=false;
> >     }
> >     if (emailAddress.equals("") ||
> > (emailAddress.indexOf('@') == -1)) {
> >       errors.put("emailAddress","Please enter a
> > valid
> > email address");
> >       emailAddress="";
> >       allOk=false;
> >     }
> >     if (homePhone.equals("")) {
> >       errors.put("homePhone","Please enter a valid
> > home phone number");
> >       homePhone="";
> >       allOk=false;
> >     }
> >     return allOk;
> >   }
> > 
> >   public String getErrorMsg(String s) {
> >     String errorMsg =(String)errors.get(s.trim());
> >     return (errorMsg == null) ? "":errorMsg;
> >   }
> > 
> >   public MemberValidateForm() {
> >     firstName="";
> >     lastName="";
> >     emailAddress="";
> >     homePhone="";
> >     errors = new Hashtable();
> >   }
> > 
> >   public String getFirstName() {
> >     return firstName;
> >   }
> > 
> >   public String getLastName() {
> >     return lastName;
> >   }
> > 
> >   public String getEmailAddress() {
> >     return emailAddress;
> >   }
> > 
> >   public String getHomePhone() {
> >     return homePhone;
> >   }
> > 
> >   public void setFirstName(String fname) {
> >     firstName =fname;
> >   }
> > 
> >   public void setLastName(String lname) {
> >     lastName =lname;
> >   }
> > 
> >   public void setEmailAddress(String eml) {
> >     emailAddress=eml;
> >   }
> > 
> >   public void setHomePhone(String hp) {
> >     homePhone=hp;
> >   }
> > 
> 
=== message truncated ===

--- James Carman <james@xxxxxxxxxxxxxxxxxxxx> wrote:

> Well, if your taglibs are working properly (I assume
> they are, since your
> JSP page runs without any hiccups with the taglib
> declaration in it), then,
> to print out the firstName property of the
> formToValidate "bean", you would
> do...
> 
> <c:out value="${formToValidate.firstName}" />
> 
> Yes, they ACTUALLY named the tag "c:out"!  Must have
> been a C++ programmer
> at one time! :-)  In protest, I usually use a
> different "prefix" for the
> JSTL core library.  Anyway, there is a good book out
> called JSTL In Action
> by Shawn Bayern (Manning Publications), if you're
> looking for a reference.  
> 
> -----Original Message-----
> From: sdgesa gaeharth
> [mailto:pollux1234567890@xxxxxxxxx] 
> Sent: Wednesday, September 08, 2004 11:25 PM
> To: users@xxxxxxxxxx
> Subject: RE: [cinjug-users] newbie trying to learn
> m-v-c
> 
> J Cameran:
> 
> Exactly the answer I was hoping for!!  Can you show
> me
>  how to integrate jstl with my jsp page.  I have
> only
> scratched the surface with jstl documenation and I
> need an example to get started.
> 
> For everyone else...
> I would use a framework except I would rather learn
> from scratch. Later on, if I decide to use a
> framework
> I try it out.
> 
> thanks again.
> 
> 
> --- James Carman <james@xxxxxxxxxxxxxxxxxxxx> wrote:
> 
> > With JSTL (I assume you're using JSTL since you've
> > defined the taglib in
> > your JSP page), you don't need ANY of that
> scriptlet
> > code.  Why not use the
> > JSTL tags to print out your values?  You wouldn't
> > have to worry about
> > casting anything to String.  You wouldn't have to
> > import any classes.  JSTL
> > would take care of all of that for you.  
> >  
> > 
> > -----Original Message-----
> > From: sdgesa gaeharth
> > [mailto:pollux1234567890@xxxxxxxxx] 
> > Sent: Wednesday, September 08, 2004 3:40 PM
> > To: users@xxxxxxxxxx
> > Subject: RE: [cinjug-users] newbie trying to learn
> > m-v-c
> > 
> > Ah, I see. That works. However my original
> question
> > is
> > to pass an object back to the jsp page. The
> > HttpServlet looks like this:
> > 
> > **********BEGIN SERVLET********
> >             MemberValidateForm formToValidate = new
> > MemberValidateForm();
> >             allOk =
> >
>
formToValidate.validate(firstName,lastName,emailAddress,homePhone);
> >             if(allOk){
> >                     toPage = "/MemberAdd";
> >             }else{
> >             
> > req.setAttribute("formToValidate",formToValidate);
> >             }
> >             RequestDispatcher dispatcher;
> >             dispatcher =
> context.getRequestDispatcher(toPage);
> >             dispatcher.forward(req, res);
> > 
> > **********END SERVLET********
> > 
> > the jsp page looks like this:
> > 
> > **********BEGIN JSP********
> > <%@ taglib prefix="c"
> > uri="http://java.sun.com/jstl/core"%>
> > <%@ page import="java.util.*,
> > com.bdi.www.members.MemberValidateForm" %>
> > <%
> > MemberValidateForm doThis= (MemberValidateForm)
> > request.getAttribute("formToValidate");
> > out.println(doThis.getFirstName());
> > %>
> > **********END JSP********
> > 
> > 
> > MemberValidateForm looks like this:
> > 
> > 
> > **********BEGIN MemberValidateForm********
> > 
> > import java.util.*;
> > 
> > public class MemberValidateForm {
> > 
> >   private String firstName;
> >   private String lastName;
> >   private String emailAddress;
> >   private String homePhone;
> >   private Hashtable errors;
> > 
> >   public boolean validate(String firstName,String
> > lastName,String emailAddress,String homePhone) {
> >     boolean allOk=true;
> >     if (firstName.equals("")) {
> >       errors.put("firstName","Please enter your
> > first
> > name");
> >       firstName="";
> >       allOk=false;
> >     }
> >     if (lastName.equals("")) {
> >       errors.put("lastName","Please enter your
> last
> > name");
> >       lastName="";
> >       allOk=false;
> >     }
> >     if (emailAddress.equals("") ||
> > (emailAddress.indexOf('@') == -1)) {
> >       errors.put("emailAddress","Please enter a
> > valid
> > email address");
> >       emailAddress="";
> >       allOk=false;
> >     }
> >     if (homePhone.equals("")) {
> >       errors.put("homePhone","Please enter a valid
> > home phone number");
> >       homePhone="";
> >       allOk=false;
> >     }
> >     return allOk;
> >   }
> > 
> >   public String getErrorMsg(String s) {
> >     String errorMsg =(String)errors.get(s.trim());
> >     return (errorMsg == null) ? "":errorMsg;
> >   }
> > 
> >   public MemberValidateForm() {
> >     firstName="";
> >     lastName="";
> >     emailAddress="";
> >     homePhone="";
> >     errors = new Hashtable();
> >   }
> > 
> >   public String getFirstName() {
> >     return firstName;
> >   }
> > 
> >   public String getLastName() {
> >     return lastName;
> >   }
> > 
> >   public String getEmailAddress() {
> >     return emailAddress;
> >   }
> > 
> >   public String getHomePhone() {
> >     return homePhone;
> >   }
> > 
> >   public void setFirstName(String fname) {
> >     firstName =fname;
> >   }
> > 
> >   public void setLastName(String lname) {
> >     lastName =lname;
> >   }
> > 
> >   public void setEmailAddress(String eml) {
> >     emailAddress=eml;
> >   }
> > 
> >   public void setHomePhone(String hp) {
> >     homePhone=hp;
> >   }
> > 
> 
=== message truncated ===



                
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 

---------
You may unsubscribe from this mailing list
by sending a blank email addressed to:
users-unsubscribe@xxxxxxxxxx

--
Find additional help by sending a blank email
addressed to:
users-help@xxxxxxxxxx



---------
You may unsubscribe from this mailing list
by sending a blank email addressed to:
users-unsubscribe@xxxxxxxxxx

--
Find additional help by sending a blank email
addressed to:
users-help@xxxxxxxxxx



<Prev in Thread] Current Thread [Next in Thread>