Spring MVC; avoiding file extension in url?

前端 未结 6 1610
臣服心动
臣服心动 2020-12-31 20:46

I just started with Spring Web MVC. I\'m trying to avoid file extenstions in the url. How can i do this? (I\'m using Spring 2.5.x)

Bean:

<         


        
相关标签:
6条回答
  • 2020-12-31 20:56

    In 3.0, / seems to work. That is...

    <url-pattern>/</url-pattern>
    
    0 讨论(0)
  • 2020-12-31 21:08

    Try first:

    <servlet-mapping>
            <servlet-name>spring-mvc</servlet-name>
            <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    

    If that doesn't work then problem is somewhere else. Is your Apache set up to forward those urls to Tomcat? Something like:

    JkMount /hello worker1
    
    0 讨论(0)
  • 2020-12-31 21:09

    Have you tried <url-pattern>/*</url-pattern> in the servlet mapping and <bean name="/hello" .../> ?

    0 讨论(0)
  • 2020-12-31 21:13
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    Then you need to register your urls to be handled by a particular controller. See the following

    http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html

    0 讨论(0)
  • 2020-12-31 21:14

    As far as I know you can't do this if you're using JSP's as your view for controllers.

    Because when you pass a model to a JSP, Spring MVC internally performs a 'forward' to the URL of the JSP. If you use <url-pattern>/*</url-pattern> then this forward will also be handled by your DispatcherServlet and not by your JSP view.

    What you can do is use <url-pattern>/something</url-pattern> and have your JSP's in a different directory

    0 讨论(0)
  • 2020-12-31 21:14

    In Spring 3.2 at least, the accepted answer above is very nearly but not quite what's needed. The web.xml bit below just worked for me, and I'm adding it to the thread here for reference of whoever googles this next...

    <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    0 讨论(0)
提交回复
热议问题