Greetings David.

I understood everything up to the point where I have to read in a properties 
file. If it is not to much of a problem., can you please elaborate from that 
point downwards.

I thought that once I have the criteria I would just build the SQL query. 
However, your approach appears to be more elegant and flexible (the ability 
to easily add more criterias when needed).

Thanks,

CJ

On Saturday 19 July 2003 04:15 pm, David Graham wrote:
> Name all of your search fields using this pattern:
> criteria(name).   For example,
> <input name="criteria(firstName)">
> <input name="criteria(email)">
>
> Now use a Map backed form attribute called criteria in your form bean with
> a method like this:
> public void setCriteria(Map criteria) {
>    this.criteria=criteria;
> }
>
> When the form is submitted you'll now have each criteria + its value from
> the form in the Map like [EMAIL PROTECTED]
>
> Now read in a properties file of criteria_name=SQL_chunk, for example:
> email=AND schema.person.emailAddress=''{0}''
>
> Then loop through your criteria keys and use MessageFormat to insert the
> value into the SQL chunk and append it to the total query string.  Notice
> that your SQL property names must be the same as the criteria names used
> on the form.  I used this technique recently (without Struts so I had to
> do the Map backed form business by hand); it's really not very much code
> and is easy to add criteria to.
>
> David
>
> --- Curtney Jacobs <[EMAIL PROTECTED]> wrote:
> > Hi everyone,
> >
> > I know this is of the topic but I am hoping that someone has came
> > accross this
> > and wouldn't mine shareing with me how they eloquently solve this
> > problem.
> >
> >
> > I have an ActionForm (and corresponding Action class) that takes input
> > from
> > the user and does a search depending on the inputed criteria. With three
> > or
> > four criteria it is not a problem to dynamically build the query,
> > however my
> > input form will be growing to about 10-15 criteria.
> >
> > Also, the search criteria may span serveral tables. If you have an
> > elegant
> > solution to this problem/task please share.
> >
> > Currently, I have the following. Not very elegant.
> >
> > StringBuffer queryStr = new StringBuffer();
> >
> > queryStr.append("select * from " + TableNames.STUDENT_TABLE + " where
> > 1=1");
> >
> >  /* append additional conditions to where clause depending on the values
> >
> > specified in studentVO */
> >
> > if (studentVO.getStudentID() > 0) {
> >         queryStr.append (" and student_id = '" +
> > studentVO.getStudentID() +
> > "'");
> >      }
> >      if (studentVO.getLastName() != null &&
> > (studentVO.getLastName().length())
> >
> > > 0 ) {
> >
> >         queryStr.append(" and lastname = '" + studentVO.getLastName() +
> > "'");
> >      }
> >      if (studentVO.getFirstName() != null &&
> > (studentVO.getFirstName().length() ) > 0) {
> >         queryStr.append(" and firstname = '" + studentVO.getFirstName()
> > +
> > "'");
> >      }
> >      queryStr.append(";");
> >
> >
> >
> > Thanks inadvance,
> >
> > Curtney Jacobs
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
>
> __________________________________
> Do you Yahoo!?
> SBC Yahoo! DSL - Now only $29.95 per month!
> http://sbc.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to