In a servlet mapping in Spring MVC how do I map the root of a url pattern directory?

后端 未结 5 1491
故里飘歌
故里飘歌 2020-12-05 16:28

    testServlet
    /test/*

<         


        
相关标签:
5条回答
  • 2020-12-05 16:42

    In my case, every url was working except of the root "/" url.
    The problem was that i didn't deleted the index.htm file inside of my projects' webapp root folder.

    0 讨论(0)
  • 2020-12-05 16:46

    A way without touch the web.xml file is by set the map to the default welcome file path.

    @RequestMapping("/index.html")
    
    0 讨论(0)
  • 2020-12-05 16:50

    Yevgeniy is correct, but if your DispatcherServlet is taking over for the default servlet, you have to add this to your web.xml:

    <welcome-file-list>
        <welcome-file>/</welcome-file>
    </welcome-file-list>
    
    0 讨论(0)
  • 2020-12-05 16:50

    my setup usually looks like this:

    <servlet-mapping>
        <servlet-name>testServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    controller, where i assume you want to handle /test and /test/ equally:

    @Controller
    public class MyController {
    
        @RequestMapping("/test")
        public String test() {
            return "redirect:/welcome";
        }
    
        @RequestMapping("/test/")
        public String test() {
            return "redirect:/welcome";
        }
    
        @RequestMapping("/welcome")
        public void test(ModelMap model) {
            // do your stuff
        }
    }
    

    setup like this would cause DispatcherServlet to handle requests for *.css and *.js files, which is not desired in most cases. i think this is the problem Bhavik describes. For those resources you can you the ResourceController like this:

    <mvc:resources mapping="/css/**" location="/resources/css/" />
    <mvc:resources mapping="/js/**" location="/resources/js/" />
    

    files from /resources/css and /resources/js will be served without forcing you to write a extra controller.

    0 讨论(0)
  • 2020-12-05 17:04

    First of all, the difference between mapping dispatcher servlet to "/" and to "/*". There is a difference!

    When mapping to "/*", all URL requests (including something like this "/WEB-INF/jsp/.../index.jsp") are mapped to dispatcher servlet.

    Secondly, when using Spring + Tiles, and returning some JSP in your tiles definition, it is treated as an internal forward request, and handled by the same servlet as the original request. In my example, I invoke root URL "/", which is properly caught by home() method, and then forwarded to "index.jsp" by Tiles, which is again being handled by Dispatcher Servlet. Obviously, dispatcher servlet cannot handle "index.jsp", because there is no controller for it.

    Yeah, it is ugly, but looks like this is the way it works.

    So, the only solution I've found so far: to change "/*" back to "/" in web.xml. This way JSPs are rendered properly by Tomcat's jsp servlet, I guess, and not dispatcher servlet. Unfortunately, this fix will break the ROOT URL dispatching by Spring, so you need to leave the idea of using ROOT URL + Tiles for now.

    Please note that adding explicit servlet mapping ".jsp -> Tomcat jsp in web.xml doesn't help, when using "/*", and it sucks.

    Still the problem is not resolved.

    Also this is the problem in Spring MVC 3.0

    0 讨论(0)
提交回复
热议问题