Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
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>
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.