Spring Mvc 4 - <form:errors>不工作

时间:2015-10-09 14:31:50

标签: java spring-mvc

我是Spring MVC的新手

我正在使用我的应用程序&amp;它没有与字段一起显示错误。

请查看以下代码

Employee.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>    

<!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>Zaptech</title>
</head>
<body>
<form action="save">
    <table>
        <tr>
            <td>Name</td>
            <td><input type="text" name="name" id="name"></td>
            <td><form:errors path="name"></form:errors></td>            
        </tr>

        <tr>
            <td>Blood Group</td>
            <td><input type="text" name="bloodGroup" id="bloodGroup"></td>
            <td><form:errors path="bloodGroup"></form:errors></td>
        </tr>

        <tr>
            <td><input type="submit" value="Save"></td>
        </tr>
    </table>
</form>
</body>
</html>

EmployeeController.java

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jagir.hrm.Validator.EmployeeValidator;
import com.jagir.hrm.model.Employee;
import com.jagir.hrm.service.EmployeeService;

@Controller
public class EmployeeController
{
    @Autowired
    EmployeeService empService;

    @Autowired
    EmployeeValidator empValidator;

    @RequestMapping(value="/")
    public String homePage()
    {
        System.out.println("Inside intel");
        return "employee";
    }

    @RequestMapping(value="employee")
    public String showEmployee()
    {
        return "employee";
    }

    @RequestMapping(value="save")
    public String saveEmployee(@ModelAttribute("command") @Validated Employee e , BindingResult result) throws IOException
    {
        System.out.println("Inside employee :: " + e.getName());
        //throw new IOException("Excption");
        System.out.println(result);
        empValidator.validate(e, result);
        System.out.println(result);
        System.out.println(result.hasErrors());
        if(result.hasErrors())
        {
            System.out.println("Error has occured");    

        }
        else
        {
            System.out.println("No Errors continue to save records");
            empService.saveEmployee(e);
        }

        return "employee";
    }
}

EmployeeValidator.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.jagir.hrm.model.Employee;
import com.jagir.hrm.service.EmployeeService;

@Component
public class EmployeeValidator implements Validator
{
    @Autowired
    EmployeeService employeeService;

    public boolean supports(Class<?> clazz)
    {
        return Employee.class.equals(clazz);
    }

    public void validate(Object target, Errors errors)
    {
        System.out.println("validation of objects");
        Employee employee = (Employee) target;
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "employee.name.notempty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "bloodGroup", "employee.bloodGroup.size");
        if(employee.getBloodGroup().length()>=2)
        {
            System.out.println("Inside blood groups");
            errors.rejectValue("bloodGroup", "employee.bloodGroup.size");
        }
    }   
}

如果我输入正确的值,那么它正常工作并转到数据库,但是, 当我在血型组中输入5个字符时,它会抛出错误,我可以在控制台中看到但在我的网页中我无法看到错误以及血型字段。

1 个答案:

答案 0 :(得分:0)

Employee.jsp使用纯HTML form元素+ Spring form:errors标记。

form:errors标记应该在form:form标记(或spring:bind标记内)中使用。(

尝试使用form:form标记并查看其工作原理。我还建议使用form:input代替纯HTML input

<form:form modelAttribute="command" action="save"
    ...
    <form:input path="name"/>
    <form:errors path="name"/>
    ...
</form>