You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@beehive.apache.org by Christophe DENEUX <ch...@capgemini.com> on 2006/01/20 18:27:24 UTC

"Exception handling" of exception containing values

Hi friend,

I have 2 questions concerning the exception handling on:
    - catch annotations
    - validation annotation


I have the following source code:

@Jpf.Action(
        forwards = {@Jpf.Forward(name="success", path="/jsp/xxxxxxx.jsp")},
        catches={
            ...
            @Jpf.Catch(type=xxxxxxxxxxxxxxx.class, path="xxxxxxxxxx", 
messageKey="myMessageKey_1")
        },
        validatableProperties={
            @Jpf.ValidatableProperty(
                propertyName="xxx",
                validateRequired=
                    @Jpf.ValidateRequired(messageKey="myMessageKey_2"),
                validateCustomRules={
                    @Jpf.ValidateCustomRule(rule="xxxxxxxx", 
messageKey="myMessage_3")}
            )},
        validationErrorForward=@Jpf.Forward(name="error", 
path="/jsp/error.jsp")
    )
    protected Forward creer(InseeForm form) 
        throws xx, yy, zz {
    ...
    }

In case of the catch annotations, how can I use a messageKey as "error 
4: the value {0} of field 'A' is not valid if field 'B' is filled with 
'{1}'."

In case of custom validation, how can I use messageKey as "The field 
'xxxxxxxx' has an invalid value: '{0}'.".


Thanks
Christophe


This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient,  you are not authorized to read, print, retain, copy, disseminate,  distribute, or use this message or any part thereof. If you receive this  message in error, please notify the sender immediately and delete all  copies of this message.

Re: "Exception handling" of exception containing values

Posted by Carlin Rogers <ca...@gmail.com>.
Hello Christophe,

You may have already solved this issue by now. If not, here's some
information that might help.

For your custom validation question, your rule implementation can use a
messageKey and also include args to be formatted by creating a new
org.apache.struts.action.ActionMessage and adding it to the ActionMessages
parameter, before returning false from your rule (on failure). From your
example, you might make the message key something like...

   myMessageKey_3=The field '{0}' has an invalid value: '{1}'.

Then your implementation of your rule might be...

    public static boolean validateX( Object bean, ValidatorAction va,
                                       Field field, ActionMessages errors,
                                       HttpServletRequest request,
                                       ServletContext servletContext ) {
        String value = null;
        if (bean == null || bean instanceof String) {
            value = (String) bean;
        }
        else {
            value = ValidatorUtils.getValueAsString(bean, field.getProperty
());
        }

        if (!GenericValidator.isBlankOrNull(value)) {
            if (some condition to check) {
                String property = field.getProperty(); // the field name
                String msgKey =
                    field.getMsg(va.getName()) != null
                            ? field.getMsg(va.getName())
                            : va.getMsg();
                Object[] args = new Object[2];
                args[0] = property;
                args[1] = value;
                ActionMessage am = new ActionMessage(msgKey, args);
                errors.add(property, am);
                return false;
            }
        }
        return true;
    }

In the code above the variable property will have the desired name for the
form bean field that this rule was applied. You can then use this rule on
multiple fields and the name does not need to be hard coded into the
message.

The property/name is then used as the key to identify this error. In your
JSP, you would include the tag... <netui:error key="xxx"/>, where xxx is the
property name from your example above, that the validation rule was applied
to.


The one solution I thought of for the other issue, being able to use
multiple arguments for formatting the message key in your example, is very
similar. You want to add a ActionMessage to the set of errors.
Unfortunately, when I tried this, I found a bug in the NetUI code. I've just
filed a JIRA issue to track this. See
http://issues.apache.org/jira/browse/BEEHIVE-1059

In this case, what you want to do is actually implement an exception
handler, @Jpf.ExceptionHandler, in your page flow and use the inherited
method addActionError() to a property-related message that will be shown
with the Errors and Error tags in the same way that the custom validation
example above does.

Hope this helps.
Carlin

On 1/20/06, Christophe DENEUX <ch...@capgemini.com> wrote:
>
>
> Hi friend,
>
> I have 2 questions concerning the exception handling on:
>     - catch annotations
>     - validation annotation
>
>
> I have the following source code:
>
> @Jpf.Action(
>         forwards = {@Jpf.Forward(name="success",
> path="/jsp/xxxxxxx.jsp")},
>         catches={
>             ...
>             @Jpf.Catch(type=xxxxxxxxxxxxxxx.class, path="xxxxxxxxxx",
> messageKey="myMessageKey_1")
>         },
>         validatableProperties={
>             @Jpf.ValidatableProperty(
>                 propertyName="xxx",
>                 validateRequired=
>                     @Jpf.ValidateRequired(messageKey="myMessageKey_2"),
>                 validateCustomRules={
>                     @Jpf.ValidateCustomRule(rule="xxxxxxxx",
> messageKey="myMessage_3")}
>             )},
>         validationErrorForward=@Jpf.Forward(name="error",
> path="/jsp/error.jsp")
>     )
>     protected Forward creer(InseeForm form)
>         throws xx, yy, zz {
>     ...
>     }
>
> In case of the catch annotations, how can I use a messageKey as "error
> 4: the value {0} of field 'A' is not valid if field 'B' is filled with
> '{1}'."
>
> In case of custom validation, how can I use messageKey as "The field
> 'xxxxxxxx' has an invalid value: '{0}'.".
>
>
> Thanks
> Christophe
>
>
> This message contains information that may be privileged or confidential
> and is the property of the Capgemini Group. It is intended only for the
> person to whom it is addressed. If you are not the intended recipient,  you
> are not authorized to read, print, retain, copy, disseminate,  distribute,
> or use this message or any part thereof. If you receive this  message in
> error, please notify the sender immediately and delete all  copies of this
> message.
>
>