Java 如何设置Spring中每个错误字段中使用的全局前缀和后缀?
在Struts中,您可以在资源包文件中确认全局前缀和后缀。比如:Java 如何设置Spring中每个错误字段中使用的全局前缀和后缀?,java,spring,forms,spring-mvc,Java,Spring,Forms,Spring Mvc,在Struts中,您可以在资源包文件中确认全局前缀和后缀。比如: errors.prefix=<div class="error"> errors.suffix=</div> errors.prefix= 错误。后缀= 那么将在每个之前添加,为什么?Spring在标记中呈现错误,您可以通过的cssClass属性指定该标记的css类。然后,您可以通过CSS对其进行任意样式设置 本身显然也是如此(我刚刚看了一下,自己也不需要修改): 将错误包装在中有一个解决方法,但您
errors.prefix=<div class="error">
errors.suffix=</div>
errors.prefix=
错误。后缀=
那么将在每个之前添加,为什么?Spring在
标记中呈现错误,您可以通过
的cssClass
属性指定该标记的css类。然后,您可以通过CSS对其进行任意样式设置
本身显然也是如此(我刚刚看了一下,自己也不需要修改):
将错误包装在
中有一个解决方法,但您必须实现自定义消息源
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>
现在您必须定义自定义消息源
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>
信息
错误
您好,您能解释一下您需要它做什么吗?请参阅我的编辑-您可以像在问题中指定的那样执行
div
。如果这还不够(如果您需要几个封闭的标记),我想您可以选择DOM操作或扩展Spring的ErrorsTag并编写您自己的版本。Spring的errors标记没有Struts那样的全局设置。@ChssPly76如果我可以设置全局设置,我会避免以每种形式设置它:errorsI understand,但“global”设置不存在。不过,再一次,CSS对我来说已经足够了。扩展org.springframework.web.servlet.tags.form.ErrorsTag来添加“全局”设置(如果您真的需要)是非常简单的。@Arthur,如果Spring没有内置它,ChssPly76就无法工作!