如何将servlet映射到子路由(如/ blog)?

时间:2018-08-17 05:58:10

标签: spring spring-mvc

我正在使用SpringMVC和tomcat,我想将所有以/blog开头的URL分配给我的servlet,这是我的代码。

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

    <display-name>Spring MVC XML Configuration Example</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:app-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>my-dispatcher-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:web-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>my-dispatcher-servlet</servlet-name>
        <url-pattern>/blog</url-pattern>
    </servlet-mapping>

</web-app>

web-config.xml

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

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

    <mvc:annotation-driven />
    <mvc:resources mapping="/blog/**" location="/WEB-INF/static/blog/" />

</beans>

app-config.xml

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

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

</beans>

它不起作用。尝试访问/blog/index.html时得到404。但是,如果我将servlet-mapping中的web.xml部分更改为以下部分,它会起作用。

<servlet-mapping>
    <servlet-name>my-dispatcher-servlet</servlet-name>
    <url-pattern>/</url-pattern> <!-- from /blog to / -->
</servlet-mapping>

但是我不想这样做,我只希望servlet处理/blog下的URL,我可以这样做吗?我是否应该更改web-config.xml中的某些内容才能使其正常工作?

1 个答案:

答案 0 :(得分:1)

正如Deinum M.在评论中提到的,我应该将servlet-mapping中的web.xml部分更改为以下内容:

<servlet-mapping>
    <servlet-name>my-dispatcher-servlet</servlet-name>
    <url-pattern>/blog/*</url-pattern> <!-- notice the asterisk -->
</servlet-mapping>

然后将mvc:resource中的web-config.xml部分更改为以下部分。

<mvc:resources mapping="/**" location="/WEB-INF/static/blog/" />

已完成,现在打开/blog/index.html,现在应该可以使用。