In our example, the validation of our data is done using the validate()-method in the
FormBean. The method is called within the UserEditAction, as soon as the Save button
on our form has been clicked. The form can generate a corresponding visual note in
front of the field in which an error has occurred. To do so, the error message is
set in the ActionErrors-Collection, specifying the corresponding Property.
The following validation results in a message "Input required for Field: First Name"
and displays a warning signal for the corresponding field if no input has been made there.
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
public class UserEditForm extends UserDisplayForm {
/**
* @see org.apache.struts.action.ActionForm#validate()
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ("".equals(firstName) ) {
errors.add("firstName",
new ActionError(
"Input Required for Field: ",
"First Name"));
}
if ("".equals(lastName) ) {
errors.add("lastName",
new ActionError(
"Input Required for Field: ",
"Last Name"));
}
return errors;
}
}
The validation is triggered in the save-onClick() method. Any errors occuring thereby
are set in the FormActionContext. The result of this is that on returning to the input
page, the corresponding error messages are displayed.
If the validation was successful, the changes can be accepted in the database.
Errors can also occur during this process. Such errors are not displayed in the input
screen in our example, but in the screen from which we have come to the editing mode.
For this, the error message is also set in the context, and then, the corresponding action
is called. The CommonControls Trial Version contains the detailed source code for it.
In case of success, a corresponding message is passed to the user. For this, the text
is set using the method addGlobalMessage() into theFormActionContext. Then, the input screen is quit again.
import java.io.IOException;
import javax.servlet.ServletException;
import com.cc.framework.adapter.struts.ActionContext;
import com.cc.framework.adapter.struts.FWAction;
import com.cc.framework.adapter.struts.FormActionContext;
public class UserEditAction extends FWAction {
// other code see above ...
public void save_onClick(FormActionContext ctx) {
UserEditForm form = (UserEditForm) ctx.form();
// Validate the Formdata
ActionErrors errors = form.validate(ctx.mapping(), ctx.request());
ctx.addErrors(errors);
// If there are any Errors return and display a Message
if (ctx.hasErrors()) {
ctx.forwardToInput();
return;
}
try {
// In our Example we get the User-Object
// from the Session
User user =
(User) ctx.session().getAttribute("userobj");
populateBusinessObject(ctx, user);
user.update();
}
catch (Throwable t) {
ctx.addGlobalError("Error: ", t);
ctx.forwardByName(Forwards.BACK);
return;
}
// Generate a Success Message
ctx.addGlobalMessage(
"Data updated: ", form.getUserName());
ctx.forwardByName(Forwards.SUCCESS);
}
}
back |
continue
|