Is this an 'ok' way to setup commonly used collections??
"Rick Reumann" <[email protected]> Mon, 12 May 2003 00:14:55 -0400
| Newsgroups | gmane.comp.java.mvc.devel |
|---|---|
| Organization | baseBeans Engineering |
| Message-ID | <[email protected]> |
In the whole BaseBeans/Ibatis structure, is this a decent way to set up
some List-backed Beans into application scope for the entire application
to use (for example you need a drop down select of US States). Here's what I
did...
I created a LabelValueBean that extends BaseBean
The only method that I provided an implementation for was for a
populateAll method which looks like this:
public void populateAll(String sqlCall) throws Exception {
_labelValueDAO.populateAll(sqlCall);
_rlist = new ResList();
_rlist.addAll(_dao.getResList());
setupList();
}
The class also has set and getters for the properties "label" and
"value" repsectively.
In order to make the bean reusable, you pass in the sql parameter that
Ibatis needs... for example 'USstatesSelectAll' or
'CompanyDepartmentsSelectAll'.
To populate the collections into application scope I created a
SetupServlet which I have load on container setup (could have also set
this up as a Struts plug-in).
The SetupServlet looks like this:
public class SetupServlet extends HttpServlet {
protected static Log log = LogFactory.getLog(SetupServlet.class);
private ServletContext servletContext = getServletContext();
private static LabelValueBean itGroups = null;
private static LabelValueBean projectPriorities = null;
private static LabelValueBean projectStatus = null;
static {
try {
log.debug("In static block of SetupServlet");
itGroups = new LabelValueBean();
itGroups.populateAll( "ItGroupSelectAll");
projectPriorities = new LabelValueBean();
projectPriorities.populateAll( "ProjectPrioritySelectAll");
projectStatus = new LabelValueBean();
projectStatus.populateAll( "ProjectStatuSelectAll");
}
catch( Exception e ) {
log.error("Problem trying to create LabelValueBeans: "+e );
System.out.println("Problem trying to create LabelValueBeans: "+e );
}
}
public void init() throws ServletException {
log.debug("In init of SetupServlet - should
only see this once per server start-up");
servletContext.setAttribute("itGroups", itGroups );
servletContext.setAttribute("projectPriorities", projectPriorities );
servletContext.setAttribute("projectStatus", projectStatus );
}
public void destroy() {
log.debug("destroy() of SetupServlet -
removes Collections from ServletContext");
servletContext.removeAttribute("ItGroupSelectAll");
servletContext.removeAttribute("projectPriorities");
servletContext.removeAttribute("projectStatus");
}
}
The above works fine and I'm happy with it, but wanted to check to see if
there is a better way or if there are problems with this approach?
Thanks again,
--
Rick