Announcement Announcement Module
Collapse
No announcement yet.
Default @RequestMapping ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Default @RequestMapping ?

    I have a number of older pages that I would like to handle with one controller that maps them to a path-based view name. As I have thousands of older pages, I'd like to pass any page not otherwise mapped through this controller.

    I've been trying to figure out how to do this using the @RequestMapping annotation, but it doesn't seem to get called. Is there a way to annotate a controlled to be called if no other controllers match?

    This is what I've been trying, but it doesn't seem to work....

    Code:
    @Controller
    public class OldPageController extends BaseController {
    
        @RequestMapping
        public String oldPageHandler( HttpServletRequest request ) {
            return calcOldViewName( request );
        }
    }

  • #2
    Is this answer correct?

    Sorry to answer my own question, but I think I've figured out what works. Can someone who knows these things better than I confirm whether this is correct? My remaining question is in bold below.

    To reiterate my situation, I'm porting an old (non-Spring, static) site to Spring. I've decided to start with the home page being dynamic, and then add dynamic modules as I go. That leaves the rest of the site (a few thousand pages.)

    What I decided to do was write unique controllers for the new sections of the site, and then have a 'fall-back' controller for older pages. Since the only 'new section' right now is the home page, the two controllers look like this:

    Code:
    @Controller
    public class HomePageController extends BaseController {
    
      @RequestMapping("/index.htm")
      public String homePageHandler( HttpServletRequest request ) {
        return calcDefaultViewName( request );
      }
    }
    
    @Controller
    public class OldPageController extends BaseController {
    
      @RequestMapping("/**/*.htm")
      public String oldPageHandler( HttpServletRequest request ) {
        return calcOldViewName( request );
      }
    }
    This works because all of the older pages have URLs that are in subdirectories of the root. I'm curious whether if I add a new controller, it will take precedence over OldPageController because it's more specific. For example, if I add the following controller, will it be called instead of OldPageController for the URI '/new/index.htm' ?

    Code:
    @Controller
    public class NewController extends BaseController {
    
      @RequestMapping("/new/*.htm")
      public String oldPageHandler( HttpServletRequest request ) {
        return calcViewName( request );
      }
    }
    In case you're curious, BaseController currently looks something like this. It's intended to contain the 'base' model elements for every page on the site:

    Code:
    @Controller
    public abstract class BaseController {
    
      /*
        INJECTED DEPENDENCIES
        */
    
      private SharedPropertyService   sharedPropertyService;
    
      /*
        HELPER METHODS
        */
    
      protected String calcDefaultViewName( HttpServletRequest request ) {
        String    viewName = request.getRequestURI();
    
        if (viewName.endsWith( sharedPropertyService.getUriPageExtension() )) {
          int   viewLen;
    
          viewLen = viewName.length() - sharedPropertyService.getUriPageExtension().length();
          viewName = viewName.substring( 0, viewLen );
        }
    
        return viewName;
      }
    
      protected String calcOldViewName( HttpServletRequest request ) {
        String    viewName = "/old" + request.getRequestURI();
    
        if (viewName.endsWith( sharedPropertyService.getUriPageExtension() )) {
          int   viewLen;
    
          viewLen = viewName.length() - sharedPropertyService.getUriPageExtension().length();
          viewName = viewName.substring( 0, viewLen );
        }
    
        return viewName;
      }
    
      /*
        INJECTED DEPENDENCY GETTERS AND SETTERS
          */
    
        @ModelAttribute
        public SharedPropertyService getSharedPropertyService() {
          return sharedPropertyService;
        }
    
        @Autowired
        public void setSharedPropertyService(SharedPropertyService sharedPropertyService) {
          this.sharedPropertyService = sharedPropertyService;
        }
    }

    Comment

    Working...
    X