KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > validation > BindingResultUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.validation;
18
19 import java.util.Map JavaDoc;
20
21 import org.springframework.util.Assert;
22
23 /**
24  * Convenience methods for looking up BindingResults in a model Map.
25  *
26  * @author Juergen Hoeller
27  * @since 2.0
28  * @see BindingResult#MODEL_KEY_PREFIX
29  */

30 public abstract class BindingResultUtils {
31
32     /**
33      * Find the BindingResult for the given name in the given model.
34      * @param model the model to search
35      * @param name the name of the target object to find a BindingResult for
36      * @return the BindingResult, or <code>null</code> if none found
37      * @throws IllegalStateException if the attribute found is not of type BindingResult
38      */

39     public static BindingResult getBindingResult(Map JavaDoc model, String JavaDoc name) {
40         Assert.notNull(model, "Model map must not be null");
41         Assert.notNull(name, "Name must not be null");
42         Object JavaDoc attr = model.get(BindingResult.MODEL_KEY_PREFIX + name);
43         if (attr != null && !(attr instanceof BindingResult)) {
44             throw new IllegalStateException JavaDoc("BindingResult attribute is not of type BindingResult: " + attr);
45         }
46         return (BindingResult) attr;
47     }
48
49     /**
50      * Find a required BindingResult for the given name in the given model.
51      * @param model the model to search
52      * @param name the name of the target object to find a BindingResult for
53      * @return the BindingResult (never <code>null</code>)
54      * @throws IllegalStateException if no BindingResult found
55      */

56     public static BindingResult getRequiredBindingResult(Map JavaDoc model, String JavaDoc name) {
57         BindingResult bindingResult = getBindingResult(model, name);
58         if (bindingResult == null) {
59             throw new IllegalStateException JavaDoc("No BindingResult attribute found for name '" + name +
60                     "'- have you exposed the correct model?");
61         }
62         return bindingResult;
63     }
64
65 }
66
Popular Tags