Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1359 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
7 changed files with 183 additions and 12 deletions
@ -0,0 +1,59 @@ |
|||||||
|
/* |
||||||
|
* 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. |
||||||
|
*/ |
||||||
|
package org.springframework.ui; |
||||||
|
|
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import org.springframework.ui.binding.BindingResults; |
||||||
|
import org.springframework.ui.binding.UserValues; |
||||||
|
import org.springframework.ui.binding.support.WebBinder; |
||||||
|
import org.springframework.ui.message.MessageContext; |
||||||
|
import org.springframework.ui.validation.Validator; |
||||||
|
|
||||||
|
public class WebBindAndValidateLifecycle { |
||||||
|
|
||||||
|
private WebBinder binder; |
||||||
|
|
||||||
|
@SuppressWarnings("unused") |
||||||
|
private MessageContext messages; |
||||||
|
|
||||||
|
private ValidationDecider validationDecider = ValidationDecider.ALWAYS_VALIDATE; |
||||||
|
|
||||||
|
private Validator validator; |
||||||
|
|
||||||
|
public WebBindAndValidateLifecycle(Object model, MessageContext messages) { |
||||||
|
this.binder = new WebBinder(model); |
||||||
|
} |
||||||
|
|
||||||
|
public void execute(Map<String, ? extends Object> userMap) { |
||||||
|
UserValues values = binder.createUserValues(userMap); |
||||||
|
BindingResults results = binder.bind(values); |
||||||
|
if (validationDecider.shouldValidateAfter(results)) { |
||||||
|
validator.validate(binder.getModel(), results.successes().properties()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public interface ValidationDecider { |
||||||
|
|
||||||
|
boolean shouldValidateAfter(BindingResults results); |
||||||
|
|
||||||
|
public static ValidationDecider ALWAYS_VALIDATE = new ValidationDecider() { |
||||||
|
public boolean shouldValidateAfter(BindingResults results) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,53 @@ |
|||||||
|
/* |
||||||
|
* 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. |
||||||
|
*/ |
||||||
|
package org.springframework.ui.binding; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* The results of a bind operation. |
||||||
|
* @author Keith Donald |
||||||
|
* @see Binder#bind(UserValues) |
||||||
|
*/ |
||||||
|
public interface BindingResults extends Iterable<BindingResult> { |
||||||
|
|
||||||
|
/** |
||||||
|
* The subset of BindingResults that were successful. |
||||||
|
*/ |
||||||
|
BindingResults successes(); |
||||||
|
|
||||||
|
/** |
||||||
|
* The subset of BindingResults that failed. |
||||||
|
*/ |
||||||
|
BindingResults failures(); |
||||||
|
|
||||||
|
/** |
||||||
|
* The number of results. |
||||||
|
*/ |
||||||
|
int size(); |
||||||
|
|
||||||
|
/** |
||||||
|
* The BindingResult at the specified index. |
||||||
|
* @throws IndexOutOfBoundsException if the index is out of bounds |
||||||
|
*/ |
||||||
|
BindingResult get(int index); |
||||||
|
|
||||||
|
/** |
||||||
|
* The list of properties for which binding was attempted. |
||||||
|
*/ |
||||||
|
List<String> properties(); |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue