简体   繁体   English

在表单上显示错误消息

[英]Display error message back on form

I want to validate the form input in jsp and display the error messages back to the form only. 我想在jsp中验证表单输入并仅将错误消息显示回表单。 I am able to redirect to the form in case of error but haven't found something useful to display the errors. 我可以在出错的情况下重定向到表单,但是没有找到显示错误的有用信息。

create.jsp create.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Create Order</title>
</head>
<body>
    <form method="POST" action="placeOrder.html">
        <p>Product Name : 
            <input type="text" name="pname" required/>
        </p>
        <p>Customer Name : 
            <input type="text" name="cname" required/>
        </p>
        <p>Amount : 
            <input type="text" name="amount" required/>
        </p>
        <p>Address : 
            <input type="text" name="address" required/>
        </p>
         <input type="submit" value="Submit"/>
    </form>
</body>
</html>

controller class method : 控制器类方法:

@RequestMapping(value = "placeOrder.html", method = RequestMethod.POST)
    public String orderPlaced(@ModelAttribute("orderweb") Orders o,
            BindingResult binders) {
        if (binders.hasErrors()) {
            System.out.println(binders.getFieldError());
            return "create";
        }
        this.orderservice.createOrder(o);
        return "orderDetails";
    }

@RequestMapping(value = "displayOrder.html", method = RequestMethod.POST)
    public String displayOrder(@RequestParam("id") int id, Model model) {
        model.addAttribute("orderweb", this.orderservice.getOrderbyId(id));
        return "orderDetails";
    }

I want to check whether amount field in orderPlaced method and id field in displayOrder method are integers or not and display the appropriate messages on repective forms. 我想检查orderOlader方法中的amount字段和displayOrder方法中的id字段是否为整数,并在各个表单上显示相应的消息。 For ex- amount should be integer or integer not in range etc. Thanks. 对于ex- amount应该是整数或整数不在范围等等。谢谢。

In your JSP add the below lines 在您的JSP中添加以下行

 <spring:hasBindErrors name="YourCommandObjectName">
    <font color="red">  
       <c:forEach items="${errors.allErrors}" var="error">  
        <spring:message code="${error.code}" text="${error.defaultMessage}"/>  
    </c:forEach>  
    </font>  
 </spring:hasBindErrors>

You can use validation-api and hibernate-validator and hangs required annotations to your class fields such as @Size, @NotNull and others. 您可以使用validation-api和hibernate-validator并将所需的注释挂起到您的类字段,例如@ Size,@ NotNull等。 In the controller simply add @Valid to your model attribute : 在控制器中,只需将@Valid添加到模型属性:

@RequestMapping(value = "placeOrder.html", method = RequestMethod.POST)
public String orderPlaced(@ModelAttribute("orderweb") 
                          @Valid
                          Orders o,
        BindingResult binders) {
    if (binders.hasErrors()) {
        System.out.println(binders.getFieldError());
        return "create";
    }
    this.orderservice.createOrder(o);
    return "orderDetails";
}

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

相关问题 当拒绝值时,form:errors不显示错误消息 - form:errors does not display error message when value is rejected 在自定义 spring 引导登录表单上显示错误登录消息 - Display error login message on custom spring boot login form 我希望我的程序在用户输入除整数以外的任何内容并将其循环回时显示错误消息 - I want my program to display an error message when the user inputs anything other than an integer and loop it back 如何显示错误消息,该错误消息在重定向回另一个servlet时仍然存在? - How can I display an error message, that stays when redirected back to a different servlet? Java-显示的错误消息 - Java - Error message to display 弹簧显示错误消息 - Spring display error message jsp中没有消息显示<form:errors> - No message display in jsp <form:errors> 在控制器之间传递表单反馈以重新显示表单错误消息时出现问题 - Problems passing form feedback between controllers to re-display a form error message Spring MVC - 是否存在显示表单验证错误消息的替代方法(不使用@ModelAtrribute和@Valid)? - Spring MVC -Is there alternative approach to display form validation error message (without using @ModelAtrribute along with @Valid)? 使用javascript在jsp中显示错误消息 - display error message in jsp with javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM