Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring@Controller、Validator和JSF<;h:消息/>;标签_Spring_Jsf_Spring Mvc - Fatal编程技术网

Spring@Controller、Validator和JSF<;h:消息/>;标签

Spring@Controller、Validator和JSF<;h:消息/>;标签,spring,jsf,spring-mvc,Spring,Jsf,Spring Mvc,我有一个springmvc@Controller(带有一个Spring验证器)和一个JSF页面 我想要的是使用JSF标记在JSF页面中显示验证错误 据我所知: MessageContext+MessageBuilder适用于webflows,但不适用于@Controller 我已经阅读了Spring文档,似乎(最后一段)唯一的方法就是使用SpringMVC标记 有没有一种方法可以在不使用Spring MVC标记的情况下显示my@Controller验证程序的错误?默认情况下没有 您可以做的是

我有一个springmvc
@Controller
(带有一个Spring
验证器)和一个JSF页面

我想要的是使用JSF
标记在JSF页面中显示验证错误

据我所知:

  • MessageContext
    +
    MessageBuilder
    适用于webflows,但不适用于
    @Controller
  • 我已经阅读了Spring文档,似乎(最后一段)唯一的方法就是使用SpringMVC标记

有没有一种方法可以在不使用Spring MVC标记的情况下显示my
@Controller
验证程序的
错误?

默认情况下没有

您可以做的是创建一个
HandlerInterceptor
,它实现
postHandle
方法(我假设您对所有事情都使用普通的Spring MVC)。在这个方法中,您可以查找类型为
BindingResult
的所有模型属性,从中检索所有错误并将它们传输到JSF

类似的东西应该可以工作(还没有测试过,取决于你使用的东西)


在与Spring和JSF消息集成后,我发现最简单的方法是:

  • 直接在应用程序控制器中添加faces消息(尽管@M.Deinum显示了一个体系结构解决方案)
  • 使用jsf验证器而不是spring验证器,仍然可以访问spring上下文

这很有效,我认为这是一个干净的整合。

谢谢你,迪纳姆先生。只有一个问题。此时,FacesContext为
null
。如我所见,在Spring JSF视图中实际创建FacesContext之前,先执行处理程序(
DispatcherServlet
)。我们即将找到解决方案……;)一枪,你是对的。但是您使用的是
JsfView
,这也意味着您使用的是springwebflow。不要使用拦截器,而是使用自定义的
ViewFactory
,它返回一个自定义的
JsfView
,将消息放入
FacesContext
。当前的
JsfView
没有这样做。
public class ErrorTransferingInterceptor extends HandlerInterceptorAdapter implements MessageSourceAware {

    private MessageSource messageSource;

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        FacesContext ctx = FacesContext.getCurrentInstance();
        for (Object o : modelAndView.getModel().values() ) {
            if (o instanceof Errors) {
                Errors result = (Errors) o;
                for (ObjectError err : result.getAllErrors() ) {
                    String msg = messageSource.getMessage(err, LocaleContextHolder.getLocale());
                    if (err instanceof FieldError) {
                        ctx.addMessage(((FieldError) err).getField(), new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null));
                    } else {
                        ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null));
                    }
                }
            }
        }
    }

    @Override
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource=messageSource;
    }
}