【问题标题】:Spring Webflow: Unable to display error messages with jsr 303 validationsSpring Webflow:无法使用 jsr 303 验证显示错误消息
【发布时间】:2017-10-17 01:05:45
【问题描述】:

我正在使用 spring mvc 4.3.8 和 spring webflow 2.4.5 以及 thymeleaf 3.x。验证失败后,我无法从 spring webflow 中显示的 jsr-303 注释中获取错误消息。在重新渲染视图本身时,不会显示错误消息。我还需要做什么?请帮忙。

<!-- WebFlow Configuration -->
    <bean id="viewFactoryCreator"
      class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
      <property name="viewResolvers" ref="viewResolver" />
    </bean>

    <webflow:flow-builder-services id="flowBuilderServices" 
        view-factory-creator="viewFactoryCreator" validator="validator"/>

    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

    <webflow:flow-registry id="flowRegistry"
      flow-builder-services="flowBuilderServices" base-path="/WEB-INF/spring/flows">   
      <webflow:flow-location id="add-locale" path="/locale-flow.xml" />
    </webflow:flow-registry>

    <!-- the flow executor drives the execution of the flow -->
    <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry"/>    

    <!-- Enables FlowHandler URL mapping. 
        This handler adapter is the bridge between DispatcherServlet and the flow executor -->
    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
      <property name="flowExecutor" ref="flowExecutor" />
    </bean>

    <!-- Maps request paths to flows in the flowRegistry. 
        Tells DispatcherServlet to send flow requests to the FlowHandlerAdapter -->
    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
      <property name="flowRegistry" ref="flowRegistry" />
      <property name="order" value="0" />
    </bean>

locale-flow.xml

<input name="id"/>

  <on-start>
    <evaluate expression="localeController.newLocaleForm(id)" result="flowScope.localeForm" />
  </on-start>

  <view-state id="localeForm" view="locale/locale-form-p1" model="flowScope.localeForm">
    <transition on="next" to="configureMessageBundle"/>
  </view-state>

  <view-state id="configureMessageBundle" view="locale/locale-form-p2" model="flowScope.localeForm" />
  <view-state id="returnToViewPage" view="externalRedirect:locale-page.html" />

支持表单 bean,LocaleForm.java

@NotNull(message = "Locale cannot be blank")
    private String code;

    @NotBlank(message = "Name cannot be blank")
    @Size(min = 3, max = 255, message = "Name must be between 3 and 255 characters")
    @Pattern(regexp = "^[\\w-_]+$", message = "Name can contain only alphabets, numbers, hypen and underscore")
    private String name;

表单视图页面,locale-form-p1.html

    <form class="form-horizontal" th:action="${flowExecutionUrl}" th:object="${localeForm}" method="post" enctype="multipart/form-data">
                <div class="form-group">
                  <label class="control-label col-xs-2">Locale</label>
                  <div class="col-xs-10">
                    <select class="selectpicker form-control" tabindex="0" th:field="*{code}">
                      <option value="en_US" th:each="locale : *{availableLocales}" 
                          th:value="${locale.key}" 
                          th:text="${locale.value}">English (US)</option>
                    </select>
                  </div>
                </div>
                <div class="form-group required">
                  <label class="control-label col-xs-2">
                    Name <a role="button" data-toggle="popover" data-trigger="hover" data-html="true" title="" data-content="Provide a unique name for the Locale." data-placement="top"><span class="fa fa-info-circle"></span></a>
                  </label>
                  <div class="col-xs-10" th:classappend="${#fields.hasErrors('name')}? has-error">
                    <input class="form-control" type="text" placeholder="Name" th:field="*{name}" >
                    <span class="help-block" th:unless="${#fields.hasErrors('name')}">Allowed characters are alphabets, numbers, hyphen and underscore.</span>
                    <span class="help-block" th:errors="*{name}"></span>
                  </div>
                </div> 
<div class="form-group">
              <div class="col-xs-2 col-xs-offset-2">
                <button class="btn btn-primary btn-sm btn-primary-spacing" type="submit" name="_eventId_next">Next</button>
                <button class="btn btn-default btn-sm" type="button" up-href="locale-page.html" up-target="#page-content">Cancel</button>
              </div>
            </div>
          </form>

【问题讨论】:

    标签: spring spring-mvc spring-webflow spring-webflow-2


    【解决方案1】:

    解决了。事实证明,Spring Web Flow 有一种不同的方式向用户提供反馈消息。 Spring Web Flow reference documentation 说:“Spring Web Flow 的 MessageContext 是一个用于在流执行过程中记录消息的 API”。

    <div class="form-group required">
        <label class="control-label col-xs-2">
           Name <a role="button" data-toggle="popover" data-trigger="hover" data-html="true" title="" data-content="Provide a unique name for the Locale." data-placement="top"><span class="fa fa-info-circle"></span></a>
        </label>
        <div class="col-xs-10" th:classappend="${#arrays.length(flowRequestContext.messageContext.getMessagesBySource('name'))>0}? has-error">
          <input class="form-control" type="text" placeholder="Name" th:field="*{name}" >
          <span class="help-block" th:if="${#arrays.isEmpty(flowRequestContext.messageContext.getMessagesBySource('name'))}">Allowed characters are alphabets, numbers, hyphen and underscore.</span>
          <p class="help-block" th:each="err : ${flowRequestContext.messageContext.getMessagesBySource('name')}" th:text="${err.text}">Input is invalid</p>
        </div>
      </div>
    

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 2014-07-11
      • 2012-07-27
      • 2018-08-15
      • 2014-09-28
      • 2011-08-09
      • 2013-11-23
      • 2013-12-27
      • 1970-01-01
      相关资源
      最近更新 更多