【问题标题】:Spring form validation - errors are not displayedSpring 表单验证 - 不显示错误
【发布时间】:2013-07-21 17:16:15
【问题描述】:

我有以下情况: 我尝试创建一个简单的表单来编辑有关电影的信息。因此我使用带有jsp的spring mvc。

对于验证,我使用 JSR 303 hibernate 实现(hibernate-validator 4.2.0)。

我的问题是,如果有验证错误(BindResults hasErrors 方法返回 true)我只能用

显示错误
<form:errors path="*" />

并且不是特定领域的。喜欢:

<form:errors path="title" />

我不知道为什么,但所有错误都显示在 path="*" 错误标签中,但没有显示在特定字段中。唯一显示在该字段右侧的错误是一些带来异常的错误,即

Failed to convert property value of type java.lang.String to required type int for property runtime; nested exception is java.lang.NumberFormatException: For input string: "")

我的 jsp 文件的重要部分如下所示:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<form:form commandName="movie">
    <fieldset>
        <form:errors path="*" cssClass="formErrorBlock"/>
        <table>
            <tr>
                <td><form:label path="title">Title:</form:label></td>
                <td>
                    <form:input path="title"/>
                    <form:errors path="title" cssClass="formFieldError"/>
                </td>
            </tr>
            <tr>
                <td><form:label path="runtime">Runtime:</form:label></td>
                <td>
                    <form:input path="runtime"/><br />
                    <form:errors path="runtime" cssClass="formFieldError" />
                </td>
            </tr>
            <tr>
                <td><form:label path="imdbId">IMDB-ID:</form:label></td>
                <td>
                    <form:input path="imdbId"/><br />
                    <form:errors path="imdbId" cssClass="formFieldError" />
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" />
                </td>
            </tr>
        </table>
    </fieldset>
</form:form>

我的控制器:

@Controller
public class MovieController {

@Autowired
private MovieService _movieService;

//some other methods...

@RequestMapping(value="/movie/{pMovieId}/edit", method = RequestMethod.GET)
public String showForm(ModelMap pModel, @PathVariable Integer pMovieId) {
    Movie movie = _movieService.getMovieById(pMovieId);

    pModel.addAttribute("movie", movie);

    return "edit/movie";
}


@RequestMapping(value="/movie/{pMovieId}/edit", method = RequestMethod.POST) 
public String editMovieSave(@Valid Movie pMovie, BindingResult pResult, @PathVariable Integer pMovieId, ModelMap pModel) {
    Movie movie = _movieService.getMovieById(pMovieId);

    if(pResult.hasErrors()) {
        return "edit/movie";
    }

    //save

    //redirect to moviepage
    return "redirect:/movie/" + pMovieId;
}

}

最后是我的电影课:

public class Movie implements Serializable{

private int _id;
@NotEmpty
@Size(min=3)
private String _title;
@NotEmpty
private String _filename;
@Pattern(regexp="tt+[0-9]{7}")
private String _imdbId;
@Min(value=1)
private int _runtime;

//getters and setters....

}

我知道这个问题以前被问过很多次,但没有一个答案对我有用。

感谢您的帮助

【问题讨论】:

  • 你可以试试&lt;form:errors path="_title" cssClass="formFieldError"/&gt;
  • 我会尝试将类属性名称中的 _ 删除为驼峰式,我知道有 getter 但也许有反射访问

标签: spring validation spring-mvc


【解决方案1】:

我通过重命名属性(删除下划线)解决了这个问题。不过就像“jpprade”说的(也许有反射访问)。

对于那些不能重命名类属性的人(在某些编码约定的情况下,......)可以注释getter方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 2012-03-29
    相关资源
    最近更新 更多