【问题标题】:How to set up a global prefix and suffix used in each error field in Spring?Spring中如何设置每个错误字段使用的全局前缀和后缀?
【发布时间】:2009-09-22 19:39:45
【问题描述】:

在 Struts 中,您可以在资源包文件中配置全局前缀和后缀。比如:

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

所以

将被添加在每个 将被添加在每个之后。

那么如何使用 Spring 表单标签

问候,

【问题讨论】:

    标签: java spring forms spring-mvc


    【解决方案1】:

    你为什么要这样做? Spring 在&lt;span&gt; 标记内呈现错误,您通过&lt;spring:errors&gt;cssClass 属性指定其css 类。然后,您可以通过 CSS 随意设置样式。

    &lt;span&gt; 本身也是customizable,显然(我只是看过,不需要自己更改):

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

    会在&lt;div class="errorBox"&gt;&lt;/div&gt; 中包装错误

    【讨论】:

    • 你能解释一下你需要它吗?请参阅我的编辑-您可以像在问题中指定的那样做div。如果这还不够(如果您需要 几个 封闭标签),我想您的选择是 DOM 操作或扩展 Spring 的 ErrorsTag 并编写您自己的版本。
    • Spring 的错误标签没有类似 Struts 的全局设置。
    • @ChssPly76 如果我可以设置全局设置,我会避免在每个表单中设置它:错误
    • 我了解,但“全局”设置不存在。不过,&lt;span&gt; + CSS 再一次对我来说已经足够了。如果您确实需要它们,扩展 org.springframework.web.servlet.tags.form.ErrorsTag 以添加“全局”设置是相当简单的。
    • @Arthur,如果没有 Spring 内置,ChssPly76 将无法使其工作!
    【解决方案2】:

    有一种解决方法,但您必须实现自定义 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);
        }
    
    }
    

    现在你必须定义你的自定义消息源

    <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>
    

    问候,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-12
      • 2021-02-10
      • 2015-10-21
      • 2018-07-12
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      相关资源
      最近更新 更多