简体   繁体   中英

How to provide support for URLs with .jsp extension in spring

I have been told in this post that it is possible to have .jsp extension in URL structure using Spring. I have been trying to achieve it with the following code:

HelloController.java

@Controller
@RequestMapping("/welcome")
public class HelloController {
    @RequestMapping(value="/item.jsp", method = RequestMethod.GET)
    public String helloDotJSP(ModelMap model) {

        System.out.println("/item.jsp RequestMapping");
        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "item";
    }
}

And when I try to access the page localhost:8080/app/welcome/item.jsp I get the error that The requested resource is not available. But when I try to just modify the extension from /item.jsp to for example /item.other it starts working. How can I add support for .jsp extension?

Thank you for your help.

You are getting the "requested resource is not available" because I your servlet mapping in the web.xml is not mapping item.jsp to the Spring DispatcherServlet .

Regardless, if your requirement is just to support old URLs, there are more effective ways of doing it.

Most lo-fi is to create jsps that with the same name and structure as the old urls, and in each of these jsps add scriptlets like

<%
response.setStatus(301);
response.setHeader( "Location", "/new-url.htm" );
response.setHeader( "Connection", "close" );
%>

I don't usually recommend scriptlets but in cases like these, its the simplest solution.

You can also add 301 redirect rules if you have a fronting webserver like apache.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM