Monday, January 4, 2016

Spring MVC form handling annotation example

tomerValidator = customerValidator;
 }
 
 @RequestMapping(method = RequestMethod.POST)
 public String processSubmit(
  @ModelAttribute("customer") Customer customer,
  BindingResult result, SessionStatus status) {
  
  customerValidator.validate(customer, result);
  
  if (result.hasErrors()) {
   //if validator failed
   return "CustomerForm";
  } else {
   status.setComplete();
   //form success
   return "CustomerSuccess";
  }
 }
 
 @RequestMapping(method = RequestMethod.GET)
 public String initForm(ModelMap model){
  
  Customer cust = new Customer();
  //Make "Spring MVC" as default checked value
  cust.setFavFramework(new String []{"Spring MVC"});
  
  //Make "Make" as default radio button selected value
  cust.setSex("M");
  
  //make "Hibernate" as the default java skills selection
  cust.setJavaSkills("Hibernate");
  
  //initilize a hidden value
  cust.setSecretValue("I'm hidden value");
  
  //command object
  model.addAttribute("customer", cust);
  
  //return form view
  return "CustomerForm";
 }
 
 
 @ModelAttribute("webFrameworkList")
 public List<String> populateWebFrameworkList() {
  
  //Data referencing for web framework checkboxes
  List<String> webFrameworkList = new ArrayList<String>();
  webFrameworkList.add("Spring MVC");
  webFrameworkList.add("Struts 1");
  webFrameworkList.add("Struts 2");
  webFrameworkList.add("JSF");
  webFrameworkList.add("Apache Wicket");
  
  return webFrameworkList;
 }
 
 @InitBinder
 public void initBinder(WebDataBinder binder) {
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  
  binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
  
 }
 
 @ModelAttribute("numberList")
 public List<String> populateNumberList() {
  
  //Data referencing for number radiobuttons
  List<String> numberList = new ArrayList<String>();
  numberList.add("Number 1");
  numberList.add("Number 2");
  numberList.add("Number 3");
  numberList.add("Number 4");
  numberList.add("Number 5");
  
  return numberList;
 }
 
 @ModelAttribute("javaSkillsList")
 public Map<String,String> populateJavaSkillList() {
  
  //Data referencing for java skills list box
  Map<String,String> javaSkill = new LinkedHashMap<String,String>();
  javaSkill.put("Hibernate", "Hibernate");
  javaSkill.put("Spring", "Spring");
  javaSkill.put("Apache Wicket", "Apache Wicket");
  javaSkill.put("Struts", "Struts");
  
  return javaSkill;
 }

 @ModelAttribute("countryList")
 public Map<String,String> populateCountryList() {
  
  //Data referencing for java skills list box
  Map<String,String> country = new LinkedHashMap<String,String>();
  country.put("US", "United Stated");
  country.put("CHINA", "China");
  country.put("SG", "Singapore");
  country.put("MY", "Malaysia");
  
  return country;
 }
}
To make annotation work, you have to enable the component auto scanning feature in Spring.
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <context:component-scan base-package="com.mkyong.customer.controller" />
 
 <bean class="com.mkyong.customer.validator.CustomerValidator" />
 
  <!-- Register the Customer.properties -->
 <bean id="messageSource"
  class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basename" value="com/mkyong/customer/properties/Customer" />
 </bean>
  
 <bean id="viewResolver"
       class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
              <property name="prefix">
                 <value>/WEB-INF/pages/</value>
              </property>
              <property name="suffix">
                 <value>.jsp</value>
              </property>
        </bean>
</beans>

No comments:

Post a Comment