简体   繁体   中英

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. I am able to redirect to the form in case of error but haven't found something useful to display the errors.

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. For ex- amount should be integer or integer not in range etc. Thanks.

In your JSP add the below lines

 <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. In the controller simply add @Valid to your model attribute :

@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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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