BinderSupport.java 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2004-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
K
polish  
Keith Donald 已提交
16
package org.springframework.model.binder.support;
17 18

import org.springframework.context.MessageSource;
19 20
import org.springframework.model.binder.BindingResult;
import org.springframework.model.binder.MissingFieldException;
21 22 23
import org.springframework.util.Assert;

/**
K
Keith Donald 已提交
24
 * Binder implementation support class that defines common structural elements.
25 26
 * @author Keith Donald
 * @since 3.0
K
Keith Donald 已提交
27
 * @see #setRequiredFields(String[])
K
polish  
Keith Donald 已提交
28
 * @see #setMessageSource(MessageSource)
29
 * @see #createBindTemplate()
30
 */
K
Keith Donald 已提交
31
public abstract class BinderSupport {
32

33
	private BindTemplate bindTemplate;
34

K
polish  
Keith Donald 已提交
35
	private MessageSource messageSource;
36

K
Keith Donald 已提交
37
	public BinderSupport() {
38 39 40
		bindTemplate = createBindTemplate();
	}

K
polish  
Keith Donald 已提交
41 42 43 44 45 46
	/**
	 * Configure the fields for which values must be present in each bind attempt.
	 * @param fieldNames the required field names
	 * @see MissingFieldException
	 */
	public void setRequiredFields(String[] fieldNames) {
47
		bindTemplate.setRequiredFields(fieldNames);
K
polish  
Keith Donald 已提交
48
	}
49
	
50 51 52 53 54 55 56 57
	/**
	 * Configure the MessageSource that resolves localized {@link BindingResult} alert messages.
	 * @param messageSource the message source
	 */
	public void setMessageSource(MessageSource messageSource) {
		Assert.notNull(messageSource, "The MessageSource is required");
		this.messageSource = messageSource;
	}
K
polish  
Keith Donald 已提交
58

59
	// subclass hooks
K
polish  
Keith Donald 已提交
60
	
61
	/**
62 63
	 * Create the template defining the bulk-binding algorithm.
	 * Subclasses may override to customize the algorithm.
64
	 */
65 66
	protected BindTemplate createBindTemplate() {
		return new BindTemplate();
67 68
	}

K
polish  
Keith Donald 已提交
69
	/**
70
	 * The template defining the bulk-binding algorithm.
K
polish  
Keith Donald 已提交
71
	 */
72 73
	protected BindTemplate getBindTemplate() {
		return bindTemplate;
K
polish  
Keith Donald 已提交
74
	}
75
	
K
polish  
Keith Donald 已提交
76
	/**
77
	 * The configured MessageSource that resolves binding result alert messages.
K
polish  
Keith Donald 已提交
78
	 */
79 80
	protected MessageSource getMessageSource() {
		return messageSource;
81
	}
82
	
83
}