Skip to content

Integrating validation into the @MVC request lifecycle [SPR-5417] #10091

@spring-projects-issues

Description

@spring-projects-issues

Keith Donald opened SPR-5417 and commented

Currently, validation is not part of the Spring MVC @Controller binding lifecycle. This typically results in data binding being controlled by the framework, while validation logic is manually invoked by the user. One of the goals of Spring 3.0 is to build validation support into the @Controller lifecycle, which is consistent with other web frameworks like JSF and Stripes. To do this, there needs to be a way for the @Controller author to influence execution of the lifecycle; for example, to suppress model validation for some use cases, and to specify the view that should be re-rendered in the case of a binding or validation error.

Below is an example of a search controller that defines two handler methods: one to setup a search form for display, and another to process a search form submission. There is a 'required' valiation constraint on one of the form fields.
This example shows two implementation flavors of this @Controller: the first as it would need to be implemented today on Spring 2.5.6 [without any support for declarative validation], and the second how it could potentially be implemented in Spring 3.0 once validation is built back into the MVC lifecycle.

BEFORE DECLARATIVE VALIDATION

@Controller
public class AccountSearchController {

private AccountManager accountManager;

@Autowired
public AccountSearchController(AccountManager accountManager) {
    this.accountManager = accountManager;
}

@RequestMapping
public void newSearch(AccountSearchCriteria criteria) {
	
}

@RequestMapping
public String search(AccountSearchCriteria criteria, BindingResult result, Model model) {
    if (criteria.getSearchString().length() == 0) {
       result.rejectValue("searchString", "missingField");
    }
    if (result.hasErrors()) {
        return "accountsearch/newSearch";
    }
    List<Account> accounts = accountManager.findAccounts(criteria);
    model.addAttribute(accounts);
    return "accountsearch/search";
}

}

AFTER DECLARATIVE VALIDATION

@Controller
public class AccountSearchController {

private AccountManager accountManager;

@Autowired
public AccountSearchController(AccountManager accountManager) {
    this.accountManager = accountManager;
}

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.setRequiredFields(new String[] { "searchString" });
}

@RequestMapping
@SuppressValidation
public void newSearch(AccountSearchCriteria criteria) {		
}

@RequestMapping
@BindingErrorView("accountsearch/newSearch")
public List<Account> search(AccountSearchCriteria criteria) {
    return accountManager.findAccounts(criteria);
}

}


Affects: 2.5.6

Issue Links:

2 votes, 4 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions