Loading
Spring MVC in Nutshell

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Spring MVC in Nutshell


  • Spring MVC in Nutshell
    Sample Img 18

Ans)

The quick article describes the different components which are part of Spring MVC
architecture with a live example.

The DispatcherServlet :
The DispatcherServlet is a central servlet that dispatches requests
to controllers, this plays the “Front Controller” role in Spring MVC. 

<web-app>

    <servlet>
        <servlet-name>example</servlet-name>
        <servlet-class>
  org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>example</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>

</web-app>

Command controllers :

There are several types of Command controllers exist in Spring MVC including
"AbstractCommandController","SimpleFormController","AbstractWizardFormController"
each of these Controller offers different type of implementation capabilties.

Among these most commonly used one is "SimpleFormController", following code
 illustrates how to use this Controller.

This Controller offering form submission. The Controller class lets you specify
 a command object, a viewname for the form, a viewname for page you want to show
 the user when form submission has succeeded, and more.

Most important methods you should override when you use this Controller class are
 i) formBackingObject(HttpServletRequest httpServletRequest) :
  A method which you can use to set the New Form to your View.
 
 ii) Map referenceData():
     A method which you can use to pass model data to your view in the form of a Map;
 
 iii)
   onSubmit(HttpServletRequest httpServletRequest, HttpServletResponse
     httpServletResponse, Object o, BindException e) 
 
commandClass :
"commandClass" could be used to set your Form as following .
  <property name="commandClass" value="com.salesorder.StartForm"/>
 
commandName :
"commandName" with which you can  

Please take a look at following Example :

public class StartController extends SimpleFormController {
 @Override
 protected ModelAndView onSubmit(HttpServletRequest httpServletRequest, HttpServletResponse
 httpServletResponse, Object o, BindException e) throws Exception {
    StartForm form = (StartForm) o;

    if (form.getCommand().equals(PortalFormCommand.CLAIMSTARTPA_COMMAND)) {
      return new ModelAndView(new RedirectView("newOrder/newOrder.htm"));
    }
}

  @Override
  protected Object formBackingObject(HttpServletRequest httpServletRequest) throws Exception {
    AddDocumentForm form = new AddDocumentForm();
    return form;
  }

  @Override
  protected Map referenceData(HttpServletRequest request, Object o, Errors errors) throws Exception {
    Map<String, Object> map = super.referenceData(request, o, errors);
    map.put("cs", claimPortalAPI.getClaimSummary(newOrderSession.getClaimID()));
    return map;
  }
}

View :
   <bean name="/start.htm" class="com.salesorder.StartController">
        <property name="sessionForm" value="true"/>
        <property name="commandName" value="startForm"/>
        <property name="commandClass" value="com.salesorder.StartForm"/>
        <property name="formView" value="salesorder/start"/>
        <property name="successView" value="start.htm"/>
    </bean>
 
Resolver :

Views in Spring are addressed by a view name and are resolved by a view resolver,
Spring Offers Several View Resolvers such as "AbstractCachingViewResolver",
"XmlViewResolver","ResourceBundleViewResolver","InternalResourceViewResolver",
"VelocityViewResolver".

Each of the above solves different technical solution and offers different
 functionality.

"InternalResourceViewResolver" is most commonly used one , could be used as follows.

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



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Spring MVC in Nutshell


Spring MVC in Nutshell

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free