首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Hibernate 能够满足我们的验证需求(5)为视图添加验证更新

Hibernate 能够满足我们的验证需求(5)为视图添加验证更新

清单 7 节选自 Utilities 类,其中包含了标签文件使用的所有函数。在前文中我们曾经说过,最适合使用 Java 代码编写的代码都被放到了几个 TLD 可以映射的函数中,这样标签文件就可以使用它们了;这些函数都是在 Utilities 类中进行编码的。因此,我们需要三样东西:定义这些类的 TLD 文件、Utilities 中的函数,以及标签文件本身,后者要使用这些函数。(第四样应该是使用这个标签文件的 JSP 页面。)
在清单 7 中,给出了在 TLD 中引用的函数和另外一个表示给定属性是否是 Date 的方法。在这个类中要涉及到比较多的代码,但是本文限于篇幅,不会给出所有的代码;不过需要注意 findGetterMethod() 除了将表达式语言(Expression Language,EL)方法表示(customer.contact)转换成 Java 表示(customer.getContact())之外,还执行了基本的映像操作。
清单 7. Utilities 节选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static Boolean required(Object object, String propertyPath) {
    Method getMethod = findGetterMethod(object, propertyPath);
    if (getMethod == null) {
        return null;
    } else {
        return getMethod.isAnnotationPresent(NotNull.class);
    }
}

public static Boolean isDate(Object object, String propertyPath) {
    return java.util.Date.class.equals(getReturnType(object, propertyPath));
}

public static Class getReturnType(Object object, String propertyPath) {
    Method getMethod = findGetterMethod(object, propertyPath);
    if (getMethod == null) {
        return null;
    } else {
        return getMethod.getReturnType();
    }
}




此处可以清楚地看到在运行时使用 Validation annotations 是多么容易。可以简单地引用对象的 getter 方法,并检查这个方法是否有相关的给定的注释 。
清单 8 中给出的 JSP 例子进行了简化,这样就可以着重查看相关的部分了。此处,这里有一个表单,它有一个选择框和两个输入域。所有这些域都是通过在 form.tld 文件中声明的标签文件进行呈现的。标签文件被设计成使用智能缺省值,这样就可以根据需要允许简单编码的 JSP 可以有定义更多信息的选项。关键的属性是 propertyPath,它使用 EL 符号将这个域映射为模型层属性,就像是使用 Spring MVC 的 bind 标签一样。
清单 8. 一个包含表单的简单 JSP 页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ taglib tagdir="/WEB-INF/tags/form" prefix="form" %>

<form method="post" action="<c:url value="/signup/customer.edit"/>">

<form:select propertyPath="creditCard.type" collection="${creditCardTypeCollection}"
  required="true" labelKey="prompt.creditcard.type"/>

<form:text propertyPath="creditCard.number" labelKey="prompt.creditcard.number">
    <img src="<c:url value="/images/icons/help.png"/>" alt="Help"
      onclick="new Effect.SlideDown('creditCardHelp')"/>
</form:text>

<form:text propertyPath="creditCard.expirationDate"/>
</form>




text.tag 文件的完整源代码太大了,不好放在这儿,因此清单 9 给出了其中关键的部分:
清单 9. 标签文件 text.tag 节选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<%@ attribute name="propertyPath" required="true" %>

<%@ attribute name="size" required="false" type="java.lang.Integer" %>

<%@ attribute name="maxlength" required="false" type="java.lang.Integer" %>

<%@ attribute name="required" required="false" type="java.lang.Boolean" %>



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

<%@ taglib uri="formtags" prefix="form" %>



<c:set var="objectPath" value="${form:getObjectPath(propertyPath)}"/>



<spring:bind path="${objectPath}">

    <c:set var="object" value="${status.value}"/>

    <c:if test="${object == null}">

<%-- Bind ignores the command object prefix, so simple properties of the command object

return null above. --%>

        <c:set var="object" value="${commandObject}"/>

        <%-- We depend on the controller adding this to request. --%>

    </c:if>

</spring:bind>



<%-- If user did not specify whether this field is required,
query the object for this info. --%>

<c:if test="${required == null}">

    <c:set var="required" value="${form:required(object,propertyPath)}"/>

</c:if>



<c:choose>

    <c:when test="${required == null || required == false}">

        <c:set var="labelClass" value="optional"/>

    </c:when>

    <ctherwise>

        <c:set var="labelClass" value="required"/>

    </ctherwise>

</c:choose>



<c:if test="${maxlength == null}">

    <c:set var="maxlength" value="${form:maxLength(object,propertyPath)}"/>

</c:if>



<c:set var="isDate" value="${form:isDate(object,propertyPath)}"/>



<c:set var="cssClass" value="input_text"/>

<c:if test="${isDate}">

    <c:set var="cssClass" value="input_date"/>

</c:if>



<div class="field">

<spring:bind path="${propertyPath}">

<label for="${status.expression}" class="${labelClass}"><fmt:message

key="prompt.${propertyPath}"/></label>

<input type="text" name="${status.expression}" value="${status.value}"

id="${status.expression}"<c:if test="${size != null}"> size="${size}"</c:if>

<c:if test="${maxlength != null}"> maxlength="${maxlength}"</c:if>

class="${cssClass}"/>



<c:if test="${isDate}">

    <img id="${status.expression}_button"

    src="<c:url value="/images/icons/calendar.png"/>" alt="calendar"

    style="cursor: pointer;"/>

    <script type="text/javascript">

        Calendar.setup(

        {

            inputField : "${status.expression}", // ID of the input field

            ifFormat : "%m/%d/%Y", // the date format

            button : "${status.expression}_button" // ID of the button

        }

        );

    </script>

</c:if>



<span class="icons"><jsp:doBody/></span>



<c:if test="${status.errorMessage != null && status.errorMessage != ''}">

    <p class="fieldError"><img id="${status.expression}_error"

    src="<c:url value="/images/icons/error.png"/>"

    alt="error"/>${status.errorMessage}</p>

</c:if>



</spring:bind>

</div>

返回列表