简体   繁体   English

形式:错误未在 Spring 中的 JSP 上显示错误

[英]form:errors not displaying errors on JSP in Spring

I want to create simple form which will display errors if input is not proper means if validation fails.我想创建一个简单的表单,如果输入不正确,如果验证失败,它将显示错误。 I am using spring 3.0 annotations.我正在使用 spring 3.0 注释。

I did following things我做了以下事情

1: Created JSP 2: Created Controller 3: Created DTO 4: Created org.springframework.validation.Validator (write an implementation the necessary methods) 1:创建 JSP 2:创建 Controller 3:创建 DTO 4:创建 org.springframework.validation.Validator(编写实现所需的方法)

int error = bindingResult.getErrorCount() returning the error count and even my page is not being submitted which is expected but my JSP is not showing error messages int error = bindingResult.getErrorCount() 返回错误计数,甚至我的页面没有被提交,这是预期的,但我的 JSP 没有显示错误消息

I have write on JSP.我写过 JSP。

Please guide me how to do this.请指导我如何做到这一点。

If i miss on something please let me know i will paste it.如果我错过了什么,请告诉我我会粘贴它。

Have a look at this answer for the structure of the controller.查看 controller 结构的这个答案 The important think is to have a paramter BindingResult and if this binding result contains an error you must return the same view (not redirect) again.重要的想法是有一个参数BindingResult ,如果这个绑定结果包含错误,您必须再次返回相同的视图(而不是重定向)。

In the jsp code you can use the spring errors tag.在 jsp 代码中,您可以使用 spring 错误标签。

@see Spring Reference chapter 16.2.4.14 The errors tag -- there is an example @see Spring 参考章节16.2.4.14 错误标签——有一个例子

I have solved this i have just mentioned dto object name in @ModelAttribute我已经解决了这个我刚刚在@ModelAttribute中提到了 dto object 名称

public void myMethod(@Valid @ModelAttribute**("myDto")** MyDTO myDTO,
        BindingResult bindingResult, ActionResponse response,
        SessionStatus sessionStatus) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM