Rick Reumann wrote:
> Sloan Seaman wrote:
>> For my edit page, how do I populate a bean for the edit page to use
>> to populate the fields? From what I've been reading it seems like I
>> don't ahve to write a form class anymore because of the
>> DynaActionForm stuff, but I'm a bit confused.
> I like to first set up my page by having a mapping submit to a setUp
> Action. Then in this setUp action you could just do:
> DynaActionForm f = (DynaActionForm) form;
> f.set("firstName", "John Doe");
> then you would just forward to the jsp form.
I "solved" this problem quasi-generically (I depend on an ID field
in my beans, at least for now) by writing a CrudDispatchAction that
gets the bean class from a standardized name in the resource bundle
(i.e., the name attribute in the action configuration is the prefix
for the classname and I append a .classname to it to find the
appropriate class).
I've appended my CrudDispatchAction class below (it's very alpha and not
robust, it's a work in progress :) and I'd love to hear any comments on
it regarding style issues, robustification, struts paradigms, whether or
not this is even a good idea, you name it. I'm the only programmer here
so feedback is lacking :(
I left out the doCreate method since I haven't done the general purpose
one yet and I would expect some refactoring to happen (for instance the
Class instantiation stuff will go into a base class because I'm already
planning to subclass this for some other purposes).
Thanks,
Dave Newton
// Here it is
// [...]
public class CrudDispatchAction extends DispatchAction {
/**
* Logger instance
*/
private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());
/**
* List collection of items. Assumes that the class name of the
* database bean is in the resources file.
*/
public ActionForward doList(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ActionForward resultForward = mapping.findForward("errjsp");
log.info("enter");
DBRow rows = getRowObject(mapping, request);
try {
rows.select();
request.setAttribute("rows", rows.getRows());
resultForward = mapping.findForward("listjsp");
} catch (SolarException x) {
throw new ModuleException("global.exception.db");
}
return resultForward;
}
/**
* Delete an item based on ID
*/
public ActionForward doDelete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
int id = StringUtil.parseInt(request.getParameter("id"));
log.info("entering to delete id=" + id);
DBRow row = getRowObject(mapping, request);
row.deleteById(id);
return mapping.findForward("listact");
}
/**
* Edit an item based on ID
*/
public ActionForward doEdit(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
int id = StringUtil.parseInt(request.getParameter("id"));
log.info("entering to edit id=" + id);
// If GET then populate form and go to editing page
if (request.getMethod().equals("GET")) {
DBRow row = getRowObject(mapping, request);
if (row.selectById(id)) {
PropertyUtils.copyProperties(form, row);
return mapping.findForward(mapping.getInput());
} else {
return mapping.findForward("notfound");
}
}
// We've edited; validate
ActionErrors errs = form.validate(mapping, request);
if ((errs != null) && !errs.isEmpty()) {
saveErrors(request, errs);
return mapping.findForward(mapping.getInput());
}
// We've validated; update
ActionForward ret = mapping.findForward("listact"); // Assume success
try {
DBRow row = getRowObject(mapping, request);
PropertyUtils.copyProperties(row, form);
if (row.updateById(id) == 0) { // it didn't update
errs = new ActionErrors();
errs.add("systemerror", new ActionError("global.system.error")); //
TODO system error key constant
saveErrors(request, errs);
return mapping.findForward(mapping.getInput());
}
} catch (SolarException sae) {
throw new ModuleException("global.exception.db");
} catch (ModuleException me) {
throw me;
}
return mapping.findForward("success");
}
/**
* Create an item
*/
public ActionForward doCreate(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
log.info("enter");
return mapping.findForward("success");
}
/**
* Determine the appropriate bean classname for this CRUD.
* @param mapping ActionMapping to get form name from
* @param request Request to get resources from
* @return String Form name with appended .classname
*/
protected String findClassname(ActionMapping mapping, HttpServletRequest request) {
MessageResources msgs = getResources(request);
String key = mapping.getName() + ".classname";
return msgs.getMessage(key);
}
protected Class getRowClass(ActionMapping mapping, HttpServletRequest request)
throws ModuleException {
Class ret = null;
try {
ret = Class.forName(findClassname(mapping, request));
} catch (Exception x) {
throw new ModuleException("global.exception.classname");
}
return ret;
}
/**
* Instantiate the appropriate DBRow subclass for this particular
* CRUD invocation.
* @param mapping ActionMapping of action
* @param request Request of action
* @return DBRow Appropriate DBRow subclass
* @throws ModuleException on any Class issues
*/
protected DBRow getRowObject(ActionMapping mapping, HttpServletRequest request)
throws ModuleException {
DBRow ret = null;
try {
ret = (DBRow) getRowClass(mapping, request).newInstance();
} catch (Exception x) {
throw new ModuleException("global.exception.classname");
}
return ret;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]