【问题标题】:Annotation for required spring web mvc dropdown box所需spring web mvc下拉框的注释
【发布时间】:2013-09-17 08:41:09
【问题描述】:

如果用户没有选择 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>

【问题讨论】:

  • 为什么不能使用@NotNull 或@NotEmpty?...发布一些代码(jsp、实体、控制器)。
  • 我已经添加了jsp和实体
  • 取决于您的实现(查看休眠的)@NotNull 应该可以正常工作。但是,您应该正确地进行绑定,您正在绑定 Id 而您应该绑定到 Domain 对象。使用转换器转换为/从所需的对象。这样您就可以简单地将@NotNull 放在domainId 字段上。

标签: spring-mvc


【解决方案1】:

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

首先更改 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));
    }

}

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

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

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

链接

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

【讨论】:

  • 成功了,谢谢老板。我没有使用存储库。我不知道您如何在 DomainEditor 类的 @Controller 和 DomainRepository 字段中使用存储库。我们为什么要使用它?
  • @M. Denium- 在您的 setAsText() 方法中而不是 repository.findOne(Integer.valueOf(source)) 不应该是 repository.findOne(Integer.valueOf(text)) 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多