简体   繁体   中英

Spring MVC validation doesn't work

I want to implement easy validation with Spring MVC.

I added to pom.xml :

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>

Here is my POJO :

import org.hibernate.validator.constraints.Range;

public class Goal {
    @Range(min = 1, max = 120)
    private int minutes;
    // getters + setters

and Controller implementation:

import javax.validation.Valid;

@Controller
@SessionAttributes("goal")
public class GoalController {

    @RequestMapping(value = "addGoal", method = RequestMethod.GET)
    public String addGoal(Model model) {
        Goal goal = new Goal();
        goal.setMinutes(10);
        model.addAttribute("goal", goal);
        return "addGoal";
    }

    @RequestMapping(value = "addGoal", method = RequestMethod.POST)
    public String updateGoal(@Valid @ModelAttribute(value = "goal") Goal goal, BindingResult result) {
        System.out.printf("Result has errors: %s%n", result.hasErrors());
        System.out.printf("Minutes updated: %d%n", goal.getMinutes());
        if (result.hasErrors()) {
            return "addGoal";
        }
        return "redirect:/addMinutes.html";
    }
}

addGoal.jsp page:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
    <title>Add Goal Here</title>
    <style>
        .error {
            color: #ff0000;
        }

        .errorblock {
            color: #000;
            background-color: #ffeeee;
            border: 3px solid #ff0000;
            padding: 8px;
            margin: 16px;
        }
    </style>
</head>

<body>
Language: <a href="?language=en">English</a> | <a href="?language=uk">Ukrainian</a>
<form:form commandName="goal">
    <form:errors path="*" cssClass="errorblock" element="div" />
    <table>
        <tr>
            <td><spring:message code="goal.minutes"/></td>
            <td><form:input path="minutes"/></td>
            <td><form:errors path="minutes" cssClass="error"/></td>
        </tr>
        <tr>
            <td colspan="3">
                <input type="submit" value="Enter Goal Minutes"/>
            </td>
        </tr>
    </table>
</form:form>
</body>
</html>

And here is page view:

在此处输入图片说明

And appropriate result:

在此处输入图片说明

snippet from console:

Result has errors: false
Minutes updated: -5

It should display error message for negative input and stay at the same page instead second screen shot.

I couldn't figure out causing of this trouble.

Any suggestion?

Solution was cleaned - webapps folder at Tomcat.

I deleted old webapps which were deployed. And now it works fine .

在此处输入图片说明

It has some strange impact on my current application.

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