Announcement Announcement Module
Collapse
No announcement yet.
Is there a Regular Expression Handler Mapping? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is there a Regular Expression Handler Mapping?

    The subject sums it up pretty well I think. I'm looking for a type of regular expression handler mapping. I know SimpleUrlHandlerMapping makes use of the PathMatcher object which offers ant like path matching but it doesn't work for what I need.

    This might sound a bit strange at first but the location of my page can change from one invocation to another. Reason being that we embed our query string within the url folders to make the page look static. We have a request wrapper that parses them out and allows them to be considered as request parameter.

    So essentially, I need to be able to declare */*.html which would match
    /contextRoot/myPage.html OR
    /contextRoot/key1/value1/myPage.html


    Is there such a handler mapping class already or will I need to implement my own version? I'm guessing I'll also need to do the same to replace PropertiesMethodNameResolver.

    Thanks,

    Eric A. Gravel

  • #2
    Unless I'm mistaken, the example you're giving could be implemented by using **/*.html as a pattern.

    More advanced mapping can always be achieved by extend the HandlerMapping (just extend AbstractUrlHandlerMapping or SimpleUrlHandlerMapping). It should not be that difficult...

    You can always attach a new one to a JIRA issue by the way, if it's useful, we'll always have a look at it.

    regards,
    Alef

    Comment


    • #3
      I thought that'd be the pattern to use, **/*.html meaning any html file within any directory but when I step through the code it seems to compare part by part of an array from a string split on "/" so if my url path was /key1/value1/myPage.html then it compares ** against key1 and then *.html against value1 which fails.

      I thought I could do an injection of a PathMatcher but it's being used in a static fashion. It would of been nice to have ability to use IoC to define the PathMatcher.

      I thought about extending SimpleUrlHandlerMapping and overwritting the lookupHandler() method of the parent class AbstractUrlHandlerMapping but the fields are classified as private. So my only choice was to write my own version of these two class. I had to do the same for PropertiesMethodNameResolver.

      I then created a UrlPathMatcher interface that simply has the PathMatcher's match(String, String) method. I copied PathMatcher to my own package but renamed to AntExpUrlPathMatcher and then create a RegExpUrlPathMatcher. The latter uses Jakarta ORO's Perl5 matcher.


      Here's what part of my application context looks like now:

      Code:
      <bean id="homeUrlMapping" class="com.interval.travelclub.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="ensureMappingStartsWithSlash">
          <value>false</value>
        </property>
      
        <property name="urlPathMatcher">
          <bean class="com.interval.travelclub.web.servlet.handler.RegExpUrlPathMatcher"/>
        </property>
      
        <property name="mappings">
          <props>
            <prop key=".*\/.*\.home">homeController</prop>
          </props>
        </property>
      </bean>
      
      <bean id="homeController" class="com.interval.travelclub.web.controller.HomeController">
        <property name="methodNameResolver"><ref local="homeControllerMethodNameResolver"/></property>
      </bean>
      
      <bean id="homeControllerMethodNameResolver" class="com.interval.travelclub.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
        <property name="urlPathMatcher">
          <bean class="com.interval.travelclub.web.servlet.handler.RegExpUrlPathMatcher"/>
        </property>
      
        <property name="mappings">
          <props>
            <prop key=".*\/guest\..*">handleGuest</prop>
          </props>
        </property>
      </bean>
      The urlPathMatcher property allows me to inject a different matcher while the ensureMappingStartsWithSlash property is a boolean needed by AntExpUrlPathMatch so that SimpleUrlHandlerMapping's initApplicationContext() can ensure that all path pattern start with /. I didn't want to make that assumption in RegExpUrlPathMatcher.

      Cheers,

      Eric

      Comment

      Working...
      X