Saturday, March 24, 2012

Spingframework MVC set up servlet.xml

This is springframework MVC servlet.xml for WEB developers.

I hope it helps you, it is brief though.

It is tested in Spring 3.1 and 3.1.1 version.

context:component-scan : It will scan your java class package. which is coded in spring annotation based.

It has several ViewResolver as you see.

ContentNegotiatingViewResolver : It will show json result.if you want you can put xml,rss result view in ContentNegotiatingViewResolver.

InternalResourceViewResolver: It will show your jsp page.

SimpleUrlHandlerMapping : It will be handle if spring doesn't find any RequestMapping from Controller, the request will get to UrlFilenameViewController. it will resolve jsp file that is under /WEB-INF/jsp directory. so you don't have to make empty controller for each request mapping that doesn't have any logic.

I am sure it will trigger more curiosity if you are interested.

<context:component-scan base-package="yourpackage"/>

  <property name="order" value="1" />
  <property name="mediaTypes">
    <entry key="json" value="application/json" />

  <property name="defaultViews">
    <!-- JSON View -->
  <property name="ignoreAcceptHeader" value="true" />


 <!-- If no extension matched, use JSP view -->
 <bean id="viewResolver"
  <property name="order" value="2" />
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 <bean name="myController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
 <!-- If no view matched -->
 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="mappings">
  <property name="order" value="3" />    

 <!-- Configure the multipart resolver for file upload request -->
 <bean id="multipartResolver"
  class="" />

 <!-- for rest url pattern support -->
 <mvc:default-servlet-handler />

 <mvc:annotation-driven />

No comments:

Post a Comment