Hi all, I'm a struts newbie.
I'm tryingto implement a database backed app using postgresql.
I'm experiencing a problem over the process of editing/inserting records
with ActionForms.
Following Craig's struts-example and Ted's scaffolding app, to edit a
record, I have an Action to do the fetching from the db, store it in a
bean, copying the bean properties to the perform()'s ActionForm form and
forwarding to the form.jsp.
Everything seems to work fine, and before forwarding, form has all the
correct properties (checking with servlet.log()).
Once I forward to the jsp, the <html:...> tags display the data if and
only if I use a
name="administratorForm" in each tag (being administratorForm the name
of the form-bean of the ActionForm, and the attribute "name" of the
<action>).
This seems strange, and moreover, it breaks the validate()-ing process
handled by struts, as I get an exception from the jsp
(javax.servlet.ServletException: No bean found under attribute key
administratorForm) when submitting the Form with not valid values.
This is the perform() method:
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
HttpSession session = request.getSession();
String action = request.getParameter("action");
String key = request.getParameter("key");
Integer iKey = null;
if (key != null)
iKey = new Integer(key);
// Identify the relevant administrator
Bean bean = null;
if (action.equals("Create")) {
bean = new Bean();
} else {
try{
bean = new Bean();
Collection result = Access.select((Object) bean, iKey);
Iterator i = result.iterator();
if (i.hasNext())
bean = (Bean) i.next();
} catch (Exception exc) {
throw new ServletException("EditAction: unable to
retrieve administrator for key " +
key);
}
}
if (bean == null) {
if (servlet.getDebug() >= 1)
servlet.log(" No administrator for key " + key);
return (mapping.findForward("failure"));
}
bean.setAction(action);
// Populate the administrator form
if (form == null) {
if (servlet.getDebug() >= 1)
servlet.log(" Creating new AdministratorForm bean under
key "
+ mapping.getAttribute());
form = new Form();
if ("request".equals(mapping.getScope()))
request.setAttribute(mapping.getAttribute(), form);
else
session.setAttribute(mapping.getAttribute(), form);
}
Form subform = (Form) form;
if (servlet.getDebug() >= 1)
servlet.log(" Populating form from " + bean);
try {
PropertyUtils.copyProperties(subform, bean);
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t == null)
t = e;
servlet.log("AdministratorForm.populate", t);
throw new ServletException("AdministratorForm.populate", t);
} catch (Throwable t) {
servlet.log("AdministratorForm.populate", t);
throw new ServletException("AdministratorForm.populate", t);
}
if (servlet.getDebug() >= 1)
servlet.log("form.getId()="+((Form) form).getId());
// Forward control to the edit subscription page
if (servlet.getDebug() >= 1)
servlet.log(" Forwarding to 'success' page");
return (mapping.findForward("success"));
}
and this is the excerpt from the struts-config.xml:
<!-- [...] -->
<!-- THE ONE AND ONLY FORM FOR HANDLING ADMINISTRATORS -->
<form-beans>
<form-bean name="administratorForm"
type="com.engitel.jsg.admin.administrator.http.Form"/>
</form-beans>
<!-- [...] -->
<!-- ACTION TO SELECT SINGLE ADMIN TO BE UPDATED/DELETED -->
<action path="/admin/administrator/edit"
type="com.engitel.jsg.admin.administrator.EditAction"
name="administratorForm"
scope="request"
validate="false">
<forward name="success" path="/admin/administrator/form.jsp"/>
</action>
<!-- ACTION TO SAVE UPDATE/DELETE/INSERT ADMINS -->
<action path="/admin/administrator/save"
type="com.engitel.jsg.admin.ModelHelper"
name="administratorForm"
validate="true"
input="/admin/administrator/form.jsp"
parameter="com.engitel.jsg.admin.administrator.Save">
<forward name="success" path="/admin/administrator/view.jsp"/>
</action>
<!-- LIST OF ADMINISTRATORS -->
<action path="/admin/administrator/view"
type="com.engitel.jsg.admin.ModelHelper"
validate="false"
parameter="com.engitel.jsg.admin.administrator.View">
<forward name="success" path="/admin/administrator/view.jsp"/>
<forward name="edit" path="/admin/administrator/form.jsp"/>
<forward name="add" path="/admin/administrator/add"/>
</action>
<!-- [...] -->
Thanx for the help.
alberto.