Parameter Binding Techniques in Spring MVC

Parameter Binding Process

Spring MVC handles client requests by binding key/value data to controller method parameters. Unlike Struts2 which uses member variables, Spring MVC utilizes method parameters for data reception. The framework employs converters to facilitate this binding process.

Default Supported Types

Spring MVC automatically binds these common types when encountered as method parameters:

  • HttpServletRequest
  • HttpServletResponse
  • HttpSession
  • Model/ModelMap

The Model interface and its implementation ModelMap serve to populate request scope with data, similar to ModelAndView functionality.

Simple Type Binding

For basic data types, parameter names must match request parameters exactly. Example controller method:

@GetMapping("/product/edit")
public String editProduct(Model model, Long productId) {
    Product product = productService.findById(productId);
    model.addAttribute("product", product);
    return "product/edit";
}

When parameter names need to differ from request parameters, use @RequestParam:

@GetMapping("/product/edit")
public String editProduct(@RequestParam("id") Long productId, Model model) {
    // Implementation
}

The required attribute in @RequestParam specifies whether the parameter is mandatory.

POJO Binding

For plain Java objects, input field names must match POJO property names:

@PostMapping("/product/update")
public String updateProduct(Product product) {
    productService.update(product);
    return "redirect:/product/list";
}

Custom type conversion requires implementing Converter interface. Example date converter:

public class StringToDateConverter implements Converter<String, Date> {
    private static final String DATE_FORMAT = "yyyy-MM-dd";

    @Override
    public Date convert(String source) {
        try {
            return new SimpleDateFormat(DATE_FORMAT).parse(source);
        } catch (ParseException e) {
            return null;
        }
    }
}

Register custom converters in Spring configuration.

Wrapped POJO Binding

For nested objects, use dot notation in parameter names:

public class ProductQuery {
    private ProductFilter filter;
    // getters/setters
}

// Controller method
@GetMapping("/products")
public String listProducts(ProductQuery query) {
    // Implementation
}

Form fields should use names like filter.name and filter.price.

Collection Binding

For array parameters:

@PostMapping("/products/delete")
public String deleteProducts(Long[] productIds) {
    productService.deleteBatch(productIds);
    return "redirect:/products";
}

List binding requires index notation in parameter names:

public class BatchUpdateRequest {
    private List<Product> products;
    // getters/setters
}

// Form fields use names like products[0].name, products[1].price

Map binding uses key references:

public class StudentForm {
    private Map<String, Student> students;
    // getters/setters
}

// Form fields use names like students['john'].age

Tags: Spring MVC java web development Parameter Binding Backend

Posted on Thu, 18 Jun 2026 16:55:25 +0000 by spasme