Spring Web Flow 2.4和JSR 303.无法显示错误消息

时间:2016-09-19 16:20:08

标签: spring jsp bean-validation spring-webflow spring-webflow-2

如果验证失败,我的collectCustomerInfo.jsp中没有任何错误,浏览器会显示HTTP状态500。

为什么<form:errors path="streetName" cssClass="text-danger"/>中没有显示错误?

消息:

ERROR [http-nio-8080-exec-19] org.hibernate.internal.SessionImpl$5.mapManagedFlushFailure HHH000346: Error during managed flush [Validation failed for classes [com.rog.webshop.model.user.User] during update time for groups [javax.validation.groups.Default, ]List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='{Size.User.streetName.validation}', propertyPath=streetName, rootBeanClass=class com.rog.webshop.model.user.User, messageTemplate='{Size.User.streetName.validation}'}]]

collectCustomerInfo.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;" charset="utf-8"/>

    <link rel="stylesheet"
          href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
    <title>Customer</title>
</head>
<body>
<section>
    <div class="jumbotron">
        <div class="container">
            <h1>Customer</h1>
            <p>Customer details</p>
        </div>
    </div>
</section>
<section class="container">
    <form:form modelAttribute="order.user" class="form-horizontal">
        <fieldset>
            <legend>Customer Details</legend>
            <form:errors path="*" cssClass="alert alert-danger" element="div"/>


            <div class="form-group">
                <label class="control-label col-lg-2" for="firstName">First Name</label>
                <div class="col-lg-10">
                    <form:input id="firstName" path="firstName" type="text" class="form:input-large"/>
                    <form:errors path="firstName" cssClass="text-danger"/>
                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="lastName">Last Name</label>
                <div class="col-lg-10">
                    <form:input id="lastName" path="lastName" type="text" class="form:input-large"/>
                    <form:errors path="lastName" cssClass="text-danger"/>

                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="email">E-mail</label>
                <div class="col-lg-10">
                    <form:input id="email" path="email" type="text" class="form:input-large"/>
                    <form:errors path="email" cssClass="text-danger"/>

                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="phoneNumber">Phone Number</label>
                <div class="col-lg-10">
                    <form:input id="phoneNumber" path="phoneNumber" type="text" class="form:input-large"/>
                    <form:errors path="phoneNumber" cssClass="text-danger"/>

                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="doorNo">Door No</label>
                <div class="col-lg-10">
                    <form:input id="doorNo" path="doorNo" type="text"
                                class="form:input-large"/>
                    <form:errors path="doorNo" cssClass="text-danger"/>

                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="streetName">Street Name</label>
                <div class="col-lg-10">
                    <form:input id="streetName" path="streetName" type="text"
                                class="form:input-large"/>
                    <form:errors path="streetName" cssClass="text-danger"/>

                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="areaName">Area Name</label>
                <div class="col-lg-10">
                    <form:input id="areaName" path="areaName" type="text"
                                class="form:input-large"/>
                    <form:errors path="areaName" cssClass="text-danger"/>

                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="state">State</label>
                <div class="col-lg-10">
                    <form:input id="state" path="stateName" type="text"
                                class="form:input-large"/>
                    <form:errors path="stateName" cssClass="text-danger"/>

                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="country">country</label>
                <div class="col-lg-10">
                    <form:input id="country" path="country" type="text"
                                class="form:input-large"/>
                    <form:errors path="country" cssClass="text-danger"/>

                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="zipCode">Zip Code</label>
                <div class="col-lg-10">
                    <form:input id="zipCode" path="zipCode" type="text"
                                class="form:input-large"/>
                    <form:errors path="zipCode" cssClass="text-danger"/>

                </div>
            </div>

            <input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>

            <div class="form-group">
                <div class="col-lg-offset-2 col-lg-10">
                    <input type="submit" id="btnAdd" class="btn btn-primary"
                           value="Add" name="_eventId_customerInfoCollected"/>
                    <button id="btnCancel" class="btn btn-default" name="_eventId_cancel">Cancel</button>
                </div>
            </div>

        </fieldset>
    </form:form>
</section>
</body>
</html>

用户是Order的子级,并且具有@Valid注释

@Valid
@ManyToOne(cascade = CascadeType.MERGE)
private User user;

用户类

@Entity
@Table(name = "APP_USER")
public class User implements Serializable
{

    private static final long serialVersionUID = 4256810895227136750L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotEmpty
    @UserSsoId(groups = ValidateOnCreationOnly.class)
    @Size(min = 3, max = 15, message = "{Size.User.ssoId.validation}")
    @Column(name = "SSO_ID", unique = true, nullable = false)
    private String ssoId;
    @NotEmpty
    @Email
    @Column(name = "EMAIL", nullable = false)
    private String email;

//    @NotEmpty
//    @Size(min = 6, max = 15, message = "{Size.User.password.validation}")
    @Column(name = "PASSWORD", nullable = false)
    private String password;
    @Column(name = "PHONE_NUMBER", nullable = false, length = 15)
    private String phoneNumber;
    @NotEmpty
    @Size(min = 3, max = 15, message = "{Size.User.firstName.validation}")
    @Column(name = "FIRST_NAME", nullable = false)
    private String firstName;
    @NotEmpty
    @Size(min = 3, max = 15, message = "{Size.User.lastName.validation}")
    @Column(name = "LAST_NAME", nullable = false)
    private String lastName;


    @NotEmpty
    @Column(name = "STATE", nullable = false)
    private String state = State.ACTIVE.getState();

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "APP_USER_USER_PROFILE",
            joinColumns = {@JoinColumn(name = "USER_ID")},
            inverseJoinColumns = {@JoinColumn(name = "USER_PROFILE_ID")})
    private Set<UserProfile> userProfiles = new HashSet<UserProfile>();


    @Size(min = 3, max = 50, message = "{Size.User.streetName.validation}")
    @Column(name = "STREET_NAME", length = 50)
    private String streetName;
    @Min(value = 1, message = "{Min.User.doorNo.validation}")
    @Column(name = "DOOR_NO")
    private String doorNo;
    @Size(min = 3, max = 20, message = "{Size.User.areaName.validation}")
    @Column(name = "AREA_NAME", length = 20)
    private String areaName;
    @Size(min = 3, max = 50, message = "{Size.User.state.validation}")
    @Column(name = "STATE_NAME", length = 50)
    private String stateName;
    @Size(min = 3, max = 25, message = "{Size.User.country.validation}")
    @Column(name = "COUNTRY", length = 25)
    private String country;
    @Size(min = 2, max = 10, message = "{Size.User.zipCode.validation}")
    @Column(name = "ZIP_CODE", length = 10)
    private String zipCode;

    getters setters over here

xml配置的一部分:

        <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationMessageSource" ref="messageSource"/>
    </bean>


    <!--WEBFLOW CONFIG-->
    <webflow-config:flow-executor id="flowExecutor"
    flow-registry="flowRegistry"/>
    <webflow-config:flow-builder-services id="flowBuilderServices" validator="validator" />
    <webflow-config:flow-registry id="flowRegistry"
    base-path="WEB-INF/flows">
    <webflow-config:flow-location path="/checkout/checkout-flow.xml"
    id="checkout"/>
    </webflow-config:flow-registry>

我的一部分流程:

    <view-state id="collectCustomerInfo" model="order">
    <binder>
    <binding property="user.firstName" required="true"  />
    <binding property="user.lastName" required="true"  />
    <binding property="user.email" required="true"  />
    <binding property="user.phoneNumber" required="true"  />
    <binding property="user.doorNo" required="true"  />
    <binding property="user.streetName" required="true"  />
    <binding property="user.areaName" required="true"  />
    <binding property="user.stateName" required="true"  />
    <binding property="user.country" required="true"  />
    <binding property="user.zipCode" required="true"  />
    </binder>
    <transition on="customerInfoCollected" to="updateCustomerAddress"/>
</view-state>

0 个答案:

没有答案