Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-MVC 3.1: How to map URLs with a trailing slash?

I'm converting a legacy servlet application over to Spring 3.1. In the process some URLs are now obsolete. We've had some problems with our network that will not be resolved anytime soon. My boss doesn't want to trust that their redirects will always be operational. So, she asked me to put my own redirects into the webapp.

All works great, except that if a URL has a trailing slash Spring 3.1 will not find the Controller class function that handles it.

http://blah.blah.blah/acme/makedonation gets found, mapped and handled

http://blah.blah.blah/acme/makedonation/ does not

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({"makeadonation","contact","complain"})
    public String home() {
        logger.debug("started...");
        return "redirect:logout";

    }// end home()  

}// end class LegacyServletController

I Googled around and found this Stack Overflow post that offers several suggestions, but I am new to Spring and do not understand enough of it to implement some of those suggestions. This one in particular sounds like it would be good fit with my needs:

spring 3.1 RequestMappingHandlerMapping allows you to set a "useTrailingSlashMatch" property. By default it is true. I think switching it to false would solve your issue,

Would anyone give me a basic example of how to that, quote me a URL that has such an example ( I had no luck with Google ) or point me to a better idea?

Thanks much in advance Steve

like image 814
Steve Avatar asked Jul 19 '12 12:07

Steve


1 Answers

you should configure your bean in context.xml,and set property. or you can refer to link or spring doc section 16.4

example configuration

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useTrailingSlashMatch" value="true">
    </property>
</bean>
like image 63
Jigar Parekh Avatar answered Nov 15 '22 04:11

Jigar Parekh