Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC from 2.5 to 3.0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC from 2.5 to 3.0

    Hi there!

    We are migrating our web application from Spring 2.5 to Spring 3.0.5. Looks like all the Controller classes (the classes in org.springframework.web.servlet.mvc package: BaseCommandController, AbstractCommandController, SimpleFormController, etc) have been deprecated. We used those classes heavily in our application. These classes have been replaced by @Controller annotation instead.

    I have a few questions regarding Spring 3.0.x Controller configuration...

    1. We used the following XML to create a Controller bean in Spring 2.5. If <context:component-scan> is used instead to create the Controller beans, then how do I wire-in the dependencies? If possible I would like to avoid Auto-Wiring.

    Code:
    <bean id="photosetViewController" class="com.xyz.web.PhotosetViewController"
    	p:photosetManager-ref="photosetManager"
    	p:photoManager-ref="photoManager" />
    2. We have created 3 Interceptors. We use SimpleUrlHandlerMapping to map these Interceptors to different request URLs and also to map URLs to Controllers. How do we achieve the same in Spring 3.0.x?
    Code:
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    	<property name="alwaysUseFullPath" value="true" />      
           <property name="interceptors">
               <list>
                   <ref bean="httpRedirectInterceptor"/>
                   <ref bean="loginInterceptor"/>
               </list>
           </property>      
            <property name="mappings">
                <value>             
                    /abc.html=abcLookupController
                    /photoupload.html=photoUploadController
              </value>
            </property>
    </bean>
    
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    	<property name="alwaysUseFullPath" value="true" />      
           <property name="interceptors">
               <list>
                   <ref bean="httpRedirectInterceptor"/>
                   <ref bean="loginInterceptor"/>
                   <ref bean="userMenuInterceptor" />
               </list>
           </property>      
            <property name="mappings">
                <value>
                    /index.html=homepageController
                    /success.html=simpleController
                </value>
            </property>
    </bean>
    3. In case of SimpleFormControllers we used different methods like initBinder, referenceData, formBackingObject. We also used command objects and validation classes. How do we achieve the same in Spring 3.0.x?
    Code:
    <bean id="photosetAddController" class="com.xyz.web.PhotosetAddController"
    	p:photosetManager-ref="photosetManager"
    	p:photosetTypeManager-ref="photosetTypeManager"
    	p:stateManager-ref="stateManager" >
    	<property name="validator">
    	<bean class="com.xyz.form.PhotosetAddValidator" />
    	</property>
    	<property name="formView" value="photosetadd" />
    	<property name="successView" value="redirect:/photoset" /> 
    </bean>
    Any pointers are greatly appreciated.

    Thank you!
    Joe

  • #2
    You don't HAVE to change..

    1) Component-scan is autowiring...
    2) The same but if you use annotation you need to set it on the DefaultAnnotationHandlerMapping
    3) Check the reference guide..

    Comment


    • #3
      To follow up to Martin's answers:

      1) It may not be obvious, but even if though you're annotating your controller with @Controller, you don't HAVE to use component-scanning. You can still define your bean in XML much as you did before.

      2) Take a look at the mvc:interceptors namespace tag as well. This allows you to specify interceptors WITHOUT modifying the handler mappings, reducing the need for handler mappings that only differ in their interceptors.

      For example, you might specify your interceptors something like this:
      Code:
      <mvc:interceptors>
          <ref bean="httpRedirectInterceptor"/>
          <ref bean="loginInterceptor"/>
          <mvc:interceptor>
              <mapping path="/index.html"/>
              <mapping path="/success.html"/>
              <ref bean="userMenuInterceptor" />       
          </mvc:interceptor>
      </mvc:interceptors>
      (My syntax may be slightly off, as I don't have the ref guide handy, but you get the idea)

      Hope this helps
      - Don

      Comment


      • #4
        Thanks for the answers, Marten and Don! I will try both the interceptor options that you've suggested and post later how they worked for me.

        Thanks again!
        Joe

        Comment


        • #5
          I am using mvc:interceptors to facilitate user authorization. I realize Spring Security will likely be a nice approach to replace a lot of what I am doing by hand, with simple and terse configuration. But the issue at hand might be at a lower level, which is what I am looking to get around.

          Essentially, the URIs have a specific prefix that allows an interceptor to preHandle the request destined for a controller annotated with class-level RequestMapping. That URI is further parsed to determine if the user is authorized to perform the operation represented by the request, because that operation is ultimately mapped to a controller method also annotated with a RequestMapping.

          It is working rather well, but fails in a misleading sort of way when the URL itself is considered invalid after the interceptor is done with its job and successfully transfers flow of control to the destined controller. Is there any way the interceptor can determine if the URI maps to an actual RequestMapping in the controller from within the overridden preHandle method?

          Thanks!

          Ashwin

          Comment

          Working...
          X