Skip to content

Spring MVC- Classic

May 7, 2010

When you refresh the web page in the form success view, the form you just submitted
will be resubmitted again. This problem is known as duplicate form submission. To avoid
this problem, you can apply the post/redirect/get design pattern, which recommends redirecting to another URL after a form submission is handled successfully, instead of returning an
HTML page directly.

1. web.xml – Dispatcher servlet, contextConfigLocation for additional configuration files. Listener class for this ContextLoaderListener.
2. Jars: std,jstl spring-mvc.jar

3. Implement a controller interface and return new ModelAndView (‘viewName’,’modelID’,data);

4. Default usage of BeanNameUrlMappingHandler – bean name = url pattern mapped.

———-
Controller
1Implement Controller interface – handlerRequest method.
2AbstractController – handleRequestInternal.
3SimpleFormController :
4ParameterizableViewController : Controller that has view property in it that can be used to display simply configured page.
5AbstractWizardFormController – for doing wizard like page input submissions
6MultiActionController :configure method mappings with a MethodNameResolver object
[return type of a handler method can be ModelAndView (a model, and a view name or a
view object), Map (a model only), String (a view name only), or void (the method itself handles
the HTTP response directly).]

Default: maps URLs to handler methods by the method names i.e /mem/add.htm -> add()
Use the class InternalPathMethodNameResolver to add prefix/suffix to method names and the class PropertiesMethodNameResolver to explicitly map method names to url’s.

7. Document downloader: Create views for that , return model from controller and the custom view name. Class extending either AbstractExcelView (buildExcelView) – apache poi, jexcel , or AbstractPdfView (iText)(buidPdfView).

Controller->service ->dao.
Controller -> constructor, initBinder (set cmd class) , formBackingObject (single command object referenced), referenceData (lots of data),

To register custom editor :ServletRequestDataBinder binder.registerCustomEditor(MyName.class, new MyNameEditor( myService));

Mapping :
1. Can use ControllerClassNameHandlerMapping – that maps pages with controller names.
2. Custom mapping definitions:SimpleUrlHandlerMapping, property, props, prop

Multiple mappings when defined: Specify the mapping priority. You can set the order properties of the handler mapping beans for this purpose. The lower order value represents the higher priority

Filters:
HandlerInterceptor interface, which contains three callback methods for you to implement: preHandle(), postHandle(), and afterCompletion() [after page rendered]. Register your interceptor in the configuration file with a definition of the Mapper.

Locale resolver:
1. accept-language header of an HTTP request.
2. SessionLocaleResolver. It resolves locales by inspecting a predefined attribute in a user’s session – if not found http header.
3. CookieLocaleResolver: checks cookie first.

LocaleChangeInterceptor : For changing locale based on parameter.
MessageSource – only one bean named messageSource associated with DispatcherServlet. (Mandatory to have this name only!),. Resolve this tag using spring tag library : http://www.springframework.org/tags.
ResourceBundleMessageSource class.

View Handlers
1. InternalResourceViewResolver prefix/suffix added , viewClass JstlView.
For redirection using the above do -> redirect:welcome.htm/ Keep this with highest priority if using
multiple.
2. XmlViewResolver – default looks for /WEB-INF/views.xml (can override with location property)

<bean id="welcome" class="org.springframework.web.servlet.view.JstlView">
             <property name="url" value="/WEB-INF/jsp/welcome.jsp" />
</bean>

3. ResourceBundleViewResolver , property basename , views (Searches for views.properties). Useful for redirects :
welcome.(class)=org.springframework.web.servlet.view.JstlView
welcome.url=/WEB-INF/jsp/welcome.jsp

welcomeRedirect.(class)=org.springframework.web.servlet.view.RedirectView
welcomeRedirect.url=welcome.htm

Exception Handling
Excp to impl the HandlerExceptionResolver interface. Then mapped to view id used view handlers.

<bean class="org.springframework.web.servlet.handler.å
SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
                  <prop key="java.lang.Exception">errorViewPageId</prop>
</props>
</property>
</bean>

We can return ModelMap directly also , but then it implicitly relies on RequestToViewNameTranslator and uses incoming url as outgoing view name. ModelMap has chaining involved and also

Validation
done using Validator interface. class.isAssignableFrom, ValidationUtils,
Different methods for field error, object bound error.configure an instance of it, either as an
inner bean or via a bean reference, in the validator property

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: