Spring-MVC: Redirects Not Properly Changing URLS When The Original Ends In A Slash

redirectspring-mvc

I'm using Spring 3.1 to renovate a very old Servlet based site. Some URLs have been made obsolete. My boss doesn't trust the dependability of the network to maintain redirects, so she asked me to put my own redirects away from obsolete URLS into the webapp.

I made a Controller called LegacyServletController to handle the obsolete URLs. It works great unless someone types a trailing slash onto the URL. The Controller method still picks it up, but it doesn't redirect to the new URL. It just adds the new URL to the URL already in the location bar.

For example, this is an obsolete URL:

http://blah.blah.blah/acme/moreinfo/

I would like it to redirect to

http://blah.blah.blah/acme/home

However, when the obsolete URL has a trailing slash as above, this is what the redirect produces:

http://blah.blah.blah/acme/moreinfo/home

I'm guessing I need another URL Handler in my *-servlet.xml, but I am still new to Spring and not sure how to set things up so my controller function handles obsolete URLs with and without trailing slashes properly.

Here is the controller class I am using to handle my legacy URLs

import org.springframework.stereotype.Controller;
import org.springframework.validation.*;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;


import org.apache.log4j.Logger;

@Controller
public class LegacyServletController {

    private static final Logger logger = Logger.getLogger(LegacyServletController.class);

    // Redirect these legacy screns "home", the login screen via the logout process
    @RequestMapping({"moreinfo","other_dead_screen"})
    public String home() {
        logger.debug("started...");
        return "redirect:home";

    }// end home()  

}// end class LegacyServletController

Here is my acme-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="com.acme.controller" />

  <mvc:resources mapping = "/**" location = "/,file:/apps1/bea/user_projects/domains/acme/common/,file:/c:/ftp/acme/"/>
  <mvc:annotation-driven/>

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name = "prefix" value = "/jsp/"/>
    <property name = "suffix" value = ".jsp"/>
  </bean>

  <bean name="af" class="com.acme.controller.security.CustomAuthenticationFilter"/>

</beans>

Best Answer

The return statement should be: return "redirect:/home". The controllers should return an absolute path for the view resolver.

The answers in this question are also helpful: redirect in Spring MVC

Related Topic