简体   繁体   中英

struts dynamic error message handling

I want to display a dynamic error message ,i am having the code as

ActionMessages errors = new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.plan.foundForUser"));
saveErrors(request, errors);
error.plan.foundForUser={1} Not Found

I want to replace 1 with a dynamic value,how to do so ?

In Struts 1.x, if you are trying to display an dynamic ActionMessage (type can be a String,Integer etc.) using Struts ActionMessages class please have a look at the below snippet:

ActionMessages msg = new ActionMessages();
msg.add(ACtionMessages.GLOBAL_MESSAGE, new ActionMessage(
                "msg.displaymsg", new Object[] {"Message to be displayed"}));

In Property file we must set the property value like this:

msg.displaymsg={0}

Above defines that argument of zero holds the first value of Object ActionMessage.

Above worked for me.

You need to pass the variable to ActionMEssage like this,

       errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                "error.plan.foundForUser", new Object[] {"username"}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM