【问题标题】:SpringMVC validation with ajax使用 ajax 进行 SpringMVC 验证
【发布时间】:2014-03-14 03:31:02
【问题描述】:

在项目中,我使用 SpringMVC 和 Ajax。

因此,为了验证来自提交表单的输入,我不使用 SpringMVC 的错误标签来显示错误消息。相反,我将所有错误转换为一个对象并返回到 JSP 以使用 jQuery 显示它们。

据我所知,如果使用 SpringMVC 的错误标记,Spring 根据您在 bean 上设置的键从 messages.properties(e.g) 获取错误消息。

问题

我想从messages.properites(e.g) 中提取错误消息,而不使用它的错误标签。

目前,我在实现Validator 接口的项目的验证器中硬编码默认错误消息,但这不是我想要的。

我想根据我设置的错误从xxx.properties 中提取消息。

我能做什么?

【问题讨论】:

  • 你的问题太难理解了,需要提供代码示例,预期输入/输出等

标签: jquery ajax spring validation


【解决方案1】:

你需要 ResourceBundleMessageSource bean。

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

在你的控制器中

  @Autowired
  ResourceBundleMessageSource messageSource;


 @RequestMapping( "/your/url" )
 public @ResponseBody String doSomething( @RequestParam Long myVal ) {
    String errorMessage = messageSource.getMessage("error.code", Locale.getDefault());
    ...
    return ...;
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多