Spring Validation - ошибки не отображаются

Я использую комбинацию проверки аннотаций и пользовательского валидатора.

Объект:

@Entity
@Table(name = "monitoringsystems")
public class MonitoringSystem {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotNull(message = "Name must not be empty.")@Size(min=1, message="Name must not be empty.")
private String name;
@NotNull(message = "URL must not be empty.")@Size(min=1, message="Name must not be empty.")
private String url;
@NotNull(message = "Username must not be empty.")@Size(min=1, message="Name must not be empty.")
private String username;
@NotNull(message = "Password must not be empty.")@Size(min=1, message="Name must not be empty.")
private String password;
@NotNull(message = "Confirm Password must not be empty.")@Size(min=1, message="Name must not be empty.")
@Transient
private String passwordConfirm;

Пользовательский валидатор:

@Component
public class MonitoringSystemValidator implements Validator {

@Override
public boolean supports(Class<?> type) {
    return MonitoringSystem.class.isAssignableFrom(type);
}

@Override
public void validate(Object o, Errors errors) {
    MonitoringSystem monitoringSystem = (MonitoringSystem) o;
    if(!monitoringSystem.getPassword().equals(monitoringSystem.getPasswordConfirm())){
        errors.rejectValue("passwordConfirm", "Passwords are not equal.");
    }
}  

}

Я инициализирую пользовательский валидатор в своем контроллере и устанавливаю сопоставление для формы и метода сохранения.

Контроллер:

@Controller
public class MonitoringSystemController {

@Autowired
private MonitoringSystemValidator monitoringSystemValidator;

@InitBinder
public void dataBinding(WebDataBinder binder) {
    binder.addValidators(monitoringSystemValidator);
}

@RequestMapping("/monitoringsystem/new")
public String newMonitoringSystem(Model model, HttpServletRequest request) {
    MonitoringSystem monitoringSystem = new MonitoringSystem();
    model.addAttribute("monitoringSystem", monitoringSystem);
    request.getSession().setAttribute("anonymization", monitoringSystem.getAnonymization());
    request.getSession().setAttribute("hosts", monitoringSystem.getHosts());

    return "monitoringsystem/form";
}

@RequestMapping(value = "/monitoringsystem/save", method = RequestMethod.POST)
public String save(@Valid MonitoringSystem monitoringSystem, BindingResult result, HttpServletRequest request, Model model) {

    if(result.hasErrors()){
        model.addAttribute("monitoringSystem", monitoringSystem);
        request.getSession().setAttribute("anonymization", request.getSession().getAttribute("anonymization"));
        request.getSession().setAttribute("hosts", request.getSession().getAttribute("hosts"));        
        return "monitoringsystem/form";
    }

//more code

На первом этапе я хочу изменить только CSS своих полей (я использую bootstrap), чтобы отображать ошибки.

Форма:

<form class="form-horizontal" th:modelAttribute="monitoringSystem" th:object="${monitoringSystem}" th:action="@{/monitoringsystem/save}" method="post">
            <input type="hidden" th:field="*{id}"/>
            <fieldset>
                <legend>New Monitoring-System</legend>
                <div class="form-group" th:classappend="${#fields.hasErrors('name')} ?: 'has-error has-danger'">
                    <label class="col-md-4 control-label" for="textinput">Systemname</label>  
                    <div class="col-md-5">
                        <input th:field="*{name}" class="form-control input-md" type="text" /> 
                    </div>
                </div>
                <div class="form-group" th:classappend="${#fields.hasErrors('url')} ?: 'has-error has-danger'">
                    <label class="col-md-4 control-label" for="textinput">URL</label>  
                    <div class="col-md-5">
                        <input th:field="*{url}" class="form-control input-md" type="text" /> 
                    </div>
                </div>
                <div class="form-group" th:classappend="${#fields.hasErrors('username')} ?: 'has-error has-danger'">
                    <label class="col-md-4 control-label" for="textinput">Username</label>  
                    <div class="col-md-5">
                        <input th:field="*{username}" class="form-control input-md" type="text" />
                    </div>
                </div>
                <div class="form-group" th:classappend="${#fields.hasErrors('password')} ?: 'has-error has-danger'">
                    <label class="col-md-4 control-label" for="textinput">Password</label>  
                    <div class="col-md-5">
                        <input th:field="*{password}" class="form-control input-md" type="password" />
                    </div>
                </div>
                <div class="form-group" th:classappend="${#fields.hasErrors('passwordConfirm')} ?: 'has-error has-danger'">
                    <label class="col-md-4 control-label" for="textinput">Confirm Password</label>  
                    <div class="col-md-5">
                        <input th:field="*{passwordConfirm}" class="form-control input-md" type="password" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-4 control-label" for="singlebutton"></label>
                    <div class="col-md-4">
                        <a th:href="@{/monitoringsystem}" class="btn btn-default btn-small">Cancel</a> <button id="singlebutton" name="singlebutton" class="btn btn-primary btn-small">Submit</button>
                    </div>
                </div>
            </fieldset>
        </form>

Моя проверка работает правильно. Форма сохраняется только в том случае, если мои поля не равны нулю, размер больше 1 и пароль совпадает. Если нет, мой контроллер перенаправляет меня на форму.

Проблема в том, что мой css не меняется. Таким образом, должна быть проблема с моим кодом представления, или errorBinding неправильно передается представлению. Но я не могу найти свою ошибку.


person Daniel    schedule 02.11.2016    source источник


Ответы (1)


В моем условии if была ошибка, которая добавляет классы ошибок. Мне пришлось изменить ${#fields.hasErrors('url')} ?: 'has-error has-danger' на ${#fields.hasErrors('*{name}')} ? 'has-error has-danger'

person Daniel    schedule 03.11.2016