0

如果用户没有选择 spring web mvc 下拉框的任何值,如何显示验证错误消息。无法使用@Notnull 和@Notempty,因为我正在使用@manytoone 映射另一个bean 值。如何做到这一点?谢谢

@Entity()
@Table(name = "QuestionType")
public class QuestionType {

    @Id
    @GeneratedValue
    @Column(name = "id", nullable = false)
    private int id;

    @Column(name = "questionTypeName", length = 25, nullable = false)
    @Pattern(regexp="[a-z|A-Z|\\s]+$",message = "*invalid")
    @Size(max = 25, message = "*invalid")
    @NotEmpty(message = "*")
    private String questionTypeName;

    @ManyToOne
        //Here not able to use @Notnull & NotEmpty. I'm getting validator should not be used for primitive type
    @JoinColumn(name = "domainid", referencedColumnName = "id", insertable = true, updatable = true)
    private Domain domainId;
//getters and setters omited
}

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="form" uri="../../tlds/spring-form.tld"%>
<%@ taglib prefix="core" uri="../../tlds/c.tld"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Question Type Page</title>
<link href="../../css/default.css" rel="stylesheet" />
</head>
<body>
    <form:form commandName="questionType" action="QuestionTypePage.htm" method="post">
        <table style="width: 100%; height: 100%;">
            <tr height="20px">
                <td><span class="label_Heading">Question Type</span></td>
            </tr>
            <tr height="30px">
                <td><form:hidden path="id" />
                    <div align="center" style="width: 100%; border: solid 1px;"
                        class="div_Padding">
                        <span class="label_Normal">Question type</span> <span><form:input
                                cssClass="textbox_Normal" path="questionTypeName" maxlength="25"/> <form:errors
                                cssClass="errorMsg" path="questionTypeName" /> </span>
                    </div>
                    <div align="center" style="width: 100%; border: solid 1px;"
                        class="div_Padding">
                        <span class="label_Normal">Domain</span> 
                        <span>
                        <form:select path="domainId.id">
                        <form:option value="0">Select</form:option>
                        <core:forEach items="${domainList}" var="domain">

                        <form:option value="${domain.id}">${domain.domainName}</form:option>
                        </core:forEach>
                        </form:select><form:errors path="domainId.id" cssClass="errorMsg"/>
                        </span>
                    </div></td>
            </tr>
            <tr height="20px">
                <td class="line_Normal">
                    <table style="width: 100%;">
                        <tr>
                            <td><label class="statusMsg">Status :</label><label
                                class="statusMsg_Small">${statusMsg}</label></td>
                            <td align="right"><input type="submit" name="button"
                                value="${buttonValue}" class="button_Normal" /></td>
                        </tr>
                    </table>
                </td>
            </tr>


    </form:form>
</body>
</html>
4

1 回答 1

0

您的数据绑定有缺陷。您应该使用 domainId 字段本身(恕我直言,应该命名为 domain)。使用 aConverter与该对象进行转换。

首先更改 JSP 中的选择。

<form:select path="domainId" items="${domainList}" itemValue="id" itemLabel="domainName" />

那么你的实体应该在domainId字段上有@NotNull

@ManyToOne
@JoinColumn(name = "domainid", referencedColumnName = "id", insertable = true, updatable = true)
@NotNull
private Domain domainId;

最后,您需要PropertyEditor将传入的 id 转换为实际的 Domain 对象。

public class DomainEditor extends PropertyEditorSupport {

    private DomainRepository repository;

    public DomainEditor(DomainRepository repository) { this.repository=repository;}

    public String getAsText() {
        Domain value = (Domain) getValue();
        return value != null ? String.valueOf(value.getId()) : "";
    }

    public void setAsText(String text) {
        setValue(text == null ? null : repository.findOne(Integer.valueOf(text));
    }

}

您可以@Controller通过添加带@InitBinder注释的方法在带注释的类中注册它。

@InitBinder
public void initBinder(WebDataBinder dataBinder) {
    dataBinder.registerCustomEditor(Domain.class, new DomainEditor(this.repository);
}

最后要注意的是,确保您在类中正确实现了equalshashcode方法Domain

链接

  1. 数据绑定/类型转换(参考指南
于 2013-09-17T11:05:29.157 回答