Spring MVC Tutorial

Spring MVC Annotation based Form Handling example

Annotation based Form Handling:

Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view.

Can be used to expose command objects to a web view, using specific attribute names, through annotating corresponding parameters of an @RequestMapping method.

Can be used only on annotation based handler mapping @RequestMapping.

Using ModelAttribute on Method level.

Any request to this controller can share this attributes.

@ModelAttribute
  public void addingCommonObjects(Model model)
  {
    model.addAttribute("msg", "Student Registration Details");
  }

Using ModelAttribute on Parameter level

Can be used only with this request

@RequestMapping("/Register.htm")
  public ModelAndView getFormData(@ModelAttribute("ob")StudentBean sb, BindingResult result)
  {
    // BindingResult is used for validation purpose
    // Print the entire data
    
    if (!result.hasErrors())
    {
      System.out.println("Reg No..................." + sb.getRegno());
      System.out.println("Name..................." + sb.getSname());
      System.out.println("Course..................." + sb.getCourse());
      System.out.println("City..................." + sb.getAddressBean().getCity());
      System.out.println("State..................." + sb.getAddressBean().getState());
      System.out.println("Pincode..................." + sb.getAddressBean().getPincode());
    }
    else
    {
      System.out.println("Error!!!!!!!!!!!");
    }
    ModelAndView model = new ModelAndView("page");
    model.addObject("sb", sb);  //both will work it will automatically injected to on object
    //model.addObject("ob", sb);
    return model;
  }

Complete Code

package com.candidjava.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.candidjava.springmvc.bean.StudentBean;
@Controller
public class StudentController
{
  // **********************************************************
  // ModelAttribute at method level : common for all requests
  @ModelAttribute
  public void addingCommonObjects(Model model)
  {
    model.addAttribute("msg", "Student Registration Details");
  }
  // ************************************************************************************************************
  // ModelAttribute at parameter level
  @RequestMapping("/Register.htm")
  public ModelAndView getFormData(@ModelAttribute("ob")StudentBean sb, BindingResult result)
  {
    // BindingResult is used for validation purpose
    // Print the entire data
    
    if (!result.hasErrors())
    {
      System.out.println("Reg No..................." + sb.getRegno());
      System.out.println("Name..................." + sb.getSname());
      System.out.println("Course..................." + sb.getCourse());
      System.out.println("City..................." + sb.getAddressBean().getCity());
      System.out.println("State..................." + sb.getAddressBean().getState());
      System.out.println("Pincode..................." + sb.getAddressBean().getPincode());
    }
    else
    {
      System.out.println("Error!!!!!!!!!!!");
    }
    ModelAndView model = new ModelAndView("page");
    model.addObject("sb", sb);  //both will work it will automatically injected to on object
    //model.addObject("ob", sb);
    return model;
  }
  
}

Screenshot

Download

Annotation form handling @ModelAttribute Example war

Annotation form handling @ModelAttribute Example zip