32. Spring MVC: @ModelAttribute Example

Spring MVC: @ModelAttribute Example
·         By using this we can inject Form Data to Bean properties
·         Let say form has username and password as properties we can inject to bean properties directly
·         We can use @ModelAttribute in two ways
1.     ModelAttribute at method level : common for all requests
    @ModelAttribute
    public void addingCommonObjects(Model model) {        
           model.addAttribute("msg""Student Regstration Detailss");
    }

2.    ModelAttribute at parameter level

    public ModelAndView getFormData(@ModelAttribute("ob"StudentBo boBindingResult result ){ 
           System.out.println("In Else..................."+bo.getRegno());
           ModelAndView model = new ModelAndView("result");      
           model.addObject("bo"bo);
           return model;
    }

·         We can get the data in result page by ${ob. fieldname}

Example
            Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       id="WebApp_ID" version="3.0">
       <display-name>AnnoEx</display-name>


       <welcome-file-list>
              <welcome-file>/WEB-INF/index.jsp</welcome-file>
       </welcome-file-list>
       <servlet>
              <servlet-name>spring</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       </servlet>

       <servlet-mapping>
              <servlet-name>spring</servlet-name>
              <url-pattern>/</url-pattern>
       </servlet-mapping>
</web-app>

spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

       <!-- 1.handlerMapping Repalcement -->
       <mvc:annotation-driven></mvc:annotation-driven>

       <!-- 2. Controolers Base Pkg -->
       <context:component-scan base-package="controller"></context:component-scan>


       <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/"></property>
              <property name="suffix" value=".jsp"></property>
       </bean>

</beans>
           
index.jsp
<h1>Student Registartion Form</h1>

<form action="register">

Reg. No       :      <input type="text" name="regno"><br/>
Name           :     <input type="text" name="sname"><br/>

Cources :     <br/>
                           <select name="cource" MULTIPLE SIZE=8>
                                  <option value="J2SE">J2SE</option>
                                  <option value="J2EE">J2EE</option>
                                  <option value="STRUTS">STRUTS</option>
                                  <option value="SPRING">SPRING</option>
                                  <option value="HIBERNATE">HIBERNATE</option>
                                  <option value="JSF">JSF</option>                             
                                  </select><br/>
Address :     <br/>
              CITY   :      <input type="text" name="addressBo.city"><br/>
              STATE  :      <input type="text" name="addressBo.state"><br/>
              PINCODE       :      <input type="text" name="addressBo.pincode"><br/>            

              <button type="submit">REGISTER</button><br/>
                          
</form>

     AddressBo.java
package bo;

public class AddressBo {
       private String city;
       private String state;
       private int pincode;
       public String getCity() {
              return city;
       }
       public void setCity(String city) {
              this.city = city;
       }
       public String getState() {
              return state;
       }
       public void setState(String state) {
              this.state = state;
       }
       public int getPincode() {
              return pincode;
       }
       public void setPincode(int pincode) {
              this.pincode = pincode;
       }

}

StudentBo.java
package bo;

import java.sql.Date;
import java.util.List;

public class StudentBo {
private int regno;
private String sname;
private List<String> cource;
private AddressBo addressBo;
public int getRegno() {
       return regno;
}
public void setRegno(int regno) {
       this.regno = regno;
}
public String getSname() {
       return sname;
}
public void setSname(String sname) {
       this.sname = sname;
}

public List<String> getCource() {
       return cource;
}
public void setCource(List<String> cource) {
       this.cource = cource;
}
public AddressBo getAddressBo() {
       return addressBo;
}
public void setAddressBo(AddressBo addressBo) {
       this.addressBo = addressBo;
}
}

StudentController.java
            package 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 bo.StudentBo;
@Controller
public class StudentController {
      
      
       @ModelAttribute
    public void addingCommonObjects(Model model) {           
              model.addAttribute("msg""Student Regstration Detailss");
       }
      
       @RequestMapping("/register")
       public ModelAndView getFormData(@ModelAttribute("ob"StudentBo boBindingResult result ){    
              System.out.println("In Else..................."+bo.getRegno());
              ModelAndView model = new ModelAndView("result");      
              model.addObject("bo"bo);
              return model;
       }
      
}

Result.jsp
<h1>${msg}   <br/></h1>
<h2>
${bo.regno}   <br/>
${bo.sname}   <br/>
${bo.cource}   <br/>
${bo.addressBo.city}   <br/>
${bo.addressBo.state}   <br/>
${bo.addressBo.pincode}   <br/>
</h2>

Input
Student Registartion Form
Reg. No : 
Name : 
Cources :

Address :
CITY : 
STATE : 
PINCODE : 


Output

Student Registration Details
13212
Satya kaveti
[J2SE, J2EE]
Vijayawada
ANDRA
520008


Post a Comment

Thank You

Previous Post Next Post