Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 所需SpringWebMVC下拉框的注释_Spring Mvc - Fatal编程技术网

Spring mvc 所需SpringWebMVC下拉框的注释

Spring mvc 所需SpringWebMVC下拉框的注释,spring-mvc,Spring Mvc,如果用户未选择SpringWebMVC下拉框的任何值,如何显示验证错误消息。无法使用@Notnull和@Notempty,因为我正在使用@manytoone映射Anotor bean值。如何做到这一点?。谢谢 @Entity() @Table(name = "QuestionType") public class QuestionType { @Id @GeneratedValue @Column(name = "id", nullable = false) p

如果用户未选择SpringWebMVC下拉框的任何值,如何显示验证错误消息。无法使用@Notnull和@Notempty,因为我正在使用@manytoone映射Anotor 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>
@Entity()
@表(name=“QuestionType”)
公共类问题类型{
@身份证
@生成值
@列(name=“id”,nullable=false)
私有int-id;
@列(name=“questionTypeName”,长度=25,可空=false)
@模式(regexp=“[a-z | a-z | \\s]+$”,消息=“*无效”)
@大小(最大值=25,消息=“*无效”)
@NotEmpty(message=“*”)
私有字符串类型名称;
@许多酮
//此处无法使用@Notnull&NotEmpty。我发现验证器不应用于基本类型
@JoinColumn(name=“domainid”,referencedColumnName=“id”,insertable=true,updateable=true)
私有域域ID;
//省略了getter和setter
}
问题类型页面
问题类型
问题类型
领域
挑选
${domain.domainName}
状态:${statusMsg}

您的数据绑定有缺陷。您应该使用domainId字段本身(应该改为命名为domain的IMHO)。使用
转换器
与该对象进行转换

首先在JSP中更改select

<form:select path="domainId" items="${domainList}" itemValue="id" itemLabel="domainName" />
最后,您需要一个
propertyEdit
来将传入id转换为实际的域对象

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);
}
最后一点要注意的是,确保在
类中正确实现了
equals
hashcode
方法

链接

  • 数据绑定/类型转换()

  • 为什么@NotNull或@NotEmpty不可能做到这一点呢?。。。发布一些代码(jsp、实体、控制器)。我添加了jsp和实体,具体取决于您的实现(查看hibernate实现)@NotNull应该可以正常工作。但是,您应该正确地进行绑定,您是绑定Id,而您应该绑定到
    对象。使用转换器与所需对象进行转换。这样,您只需在
    domainId
    字段中添加一个@NotNull即可。谢谢,老板。我没用过存储库。我不知道您是如何在DomainEditor类的@Controller和DomainRepository字段中使用repository的。为什么我们要使用它?@M.Denium-在你的
    setAsText()
    方法中,而不是
    repository.findOne(Integer.valueOf(source))
    它不应该是
    repository.findOne(Integer.valueOf(text))
    @InitBinder
    public void initBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(Domain.class, new DomainEditor(this.repository);
    }