append dispatsher servlet mapping to url

2019-08-31 02:51发布

问题:

I am trying to create a Spring MVC app, I have multiple dispatcher servlets in my web.xml like this:

    <servlet>
    <servlet-name>one-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:appServlet/one-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>one-servlet</servlet-name>
    <url-pattern>/one/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>two-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:appServlet/two-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>webapp-servlet</servlet-name>
    <url-pattern>/two/*</url-pattern>
</servlet-mapping>

And everything works fine, except one thing urls that i use in my jsp files like this:

<c:url value="/user" />

doesnt return the mapping of dispatcher servlet, i mean that i need a url like this myapp/one/user to use a controller from one-servlet, but the url i get is myapp/user

Any possibility to append the dispatsher servlet mapping to my urls without hardcoding them in jsp files ?

回答1:

You must rethink what you are doing. As you explicitely map multiple DispatcherServlet in your web.xml to specific URLs, that mapping is part of the local part or the URL. Either <c:url> or <spring:url> are able to add the servlet context part, but the servlet prefix is part of the webapplication, and none will guess it. After all, you should have a link from a page served by servlet one to a page served by servlet two, all that would still be in same servlet context.

IMHO, you should wnder why you need multiple DispatcherServlet in one Spring MVC application, is is rather uncommon. You will find many tutorials and examples aroud (just google ...) and all that I know use one single DispatcherServlet

Edit:

If you really want to go that way (but does actual problem really needs that ?), you could use an interceptor that would put in all requests an attribute containing the path for the servlet. You declare one interceptor bean in each servlet application context and set the servlet path to it. The interceptor could look like :

public class PathInterceptor implements WebRequestInterceptor {
    private String servletPath = "";
    private String fullPath;

    @Override
    public void preHandle(WebRequest request) throws Exception {
    }

    @Override
    public void postHandle(WebRequest request, ModelMap model) throws Exception {
        if (fullPath == null) {
            fullPath = request.getContextPath() + servletPath;
        }
        model.addAttribute("servletPath", fullPath);
    }

    @Override
    public void afterCompletion(WebRequest request, Exception ex) throws Exception {
    }

    public void setServletPath(String servletPath) {
        if (servletPath.startsWith("/")) {
            this.servletPath = servletPath;
        }
        else {
            this.servletPath = "/" + servletPath;
        }
        fullPath = null;
    }
}

The in your JSP, you use : ${servletPath}/user