所需spring web mvc下拉框的注释

时间:2013-09-17 08:41:09

标签: spring-mvc

如果用户未选择spring web mvc下拉框的任何值,如何显示验证错误消息。无法使用@Notnull和@Notempty,因为我正在使用@manytoone映射anothor 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>

1 个答案:

答案 0 :(得分:0)

您的数据绑定存在缺陷。您应该使用domainId字段本身(应该命名为域的恕我直言)。使用Converter转换为/从该对象转换。

首先更改JSP中的select。

<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);
}

为确保您在equals课程中正确实施hashcodeDomain方法,最后需要注意这一点!

链接

  1. 数据绑定/类型转换(reference guide