views:

279

answers:

2

Hi,

In Struts, you can confire a global prefix and suffix in a resource bundle file. Something like:

errors.prefix=<div class="error">
errors.suffix=</div>

So <div class="error"> will be added before each <html:errors and </div> will be added after each one.

So how can i get the same effect by using Spring form tags <form:errors ?

regards,

+2  A: 

Why would you? Spring renders errors inside a <span> tag whose css class you specify via cssClass attribute of <spring:errors>. You can then style it however you want via CSS.

<span> itself is customizable too, apparently (I've just looked, never needed to change it myself):

<spring:errors element="div" cssClass="errorBox" path="..."/>

would wrap errors in <div class="errorBox"></div>

ChssPly76
Can you explain what you need it for? See my edit - you can very much do the `div` like you've specified in your question. If that's not enough (if you need **several** enclosed tags) I suppose your options are DOM manipulation or extending Spring's ErrorsTag and writing your own version.
ChssPly76
There are no Struts-like global settings for Spring's errors tag.
ChssPly76
@ChssPly76 If i can set up a global settings, i avoid set up it in each form:errors
Arthur Ronald F D Garcia
I understand, but "global" settings do not exist. Once again, though, `<span>` + CSS has always been enough for me. It's rather trivial to extend `org.springframework.web.servlet.tags.form.ErrorsTag` to add "global" settings if you really need them.
ChssPly76
@Arthur, ChssPly76 can't make it work if Spring didn't build it in!
matt b
A: 

Hi,

There is a workaround, but you have to implement a custom MessageSource

public class CustomMessageSource extends ResourceBundleMessageSource {

    private String prefix;
    private String suffix;

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
        if(code.startsWith("typeMismatch"))
            return this.prefix + super.getMessage(resolvable, locale) + this.suffix;

        if(code.startsWith("errors"))
            return this.prefix + super.getMessage(resolvable, locale) + this.suffix;

        return super.getMessage(code, args, defaultMessage, locale);
    }

    public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
        if(code.startsWith("typeMismatch"))
            return this.prefix + super.getMessage(resolvable, locale) + this.suffix;

        if(code.startsWith("errors"))
            return this.prefix + super.getMessage(resolvable, locale) + this.suffix;

        return super.getMessage(code, args, locale);
    }

    public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
        String [] errors = resolvable.getCodes();
        for(String error: errors) {
            if(error.startsWith("typeMismatch"))
                return this.prefix + super.getMessage(resolvable, locale) + this.suffix;

            if(error.startsWith("errors"))
                return this.prefix + super.getMessage(resolvable, locale) + this.suffix;
        }

        return super.getMessage(resolvable, locale);
    }

}

Now you have to define your custom MessageSource

<bean id="messageSource" class="br.com.some.CustomMessageSource">
    <property name="prefix" value="<div class=\"error\">"/>
    <property name="suffix" value="</div>"/>
    <property name="basenames">
        <list>
             <value>messages</value>
             <value>errors</value>
        </list>
    </property name="basenames">
</bean>

regards,

Arthur Ronald F D Garcia