首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring验证-未显示错误

Spring验证-未显示错误
EN

Stack Overflow用户
提问于 2016-11-03 00:43:12
回答 1查看 887关注 0票数 0

我结合使用了注释验证和自定义验证器

对象:

代码语言:javascript
复制
@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;

CustomValidator:

代码语言:javascript
复制
@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.");
    }
}  

}

我在控制器中初始化自定义验证器,并为表单和保存方法设置映射。

控制器:

代码语言:javascript
复制
@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),以便显示错误。

表格:

代码语言:javascript
复制
<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没有正确地传递给视图。但是我找不到我的错误。

EN

回答 1

Stack Overflow用户

发布于 2016-11-03 20:55:18

在我添加errorclasses的if条件中有一个错误。我不得不将${#fields.hasErrors('url')} ?: 'has-error has-danger'更改为${#fields.hasErrors('*{name}')} ? 'has-error has-danger'

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40385272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档