将调度程序servlet映射附加到url [英] append dispatsher servlet mapping to url

查看:62
本文介绍了将调度程序servlet映射附加到url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Spring MVC应用程序,我的web.xml中有多个调度程序servlet,如下所示:

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>

一切正常,除了一件事 我在我的jsp文件中使用的网址是这样的:

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

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

不返回调度程序servlet的映射,我的意思是我需要一个像myapp/one/user这样的URL才能使用一个servlet中的控制器,但是我得到的URL是myapp/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

是否有可能将调度程序servlet映射附加到我的URL,而无需在jsp文件中对其进行硬编码?

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

推荐答案

您必须重新考虑您的工作.当您明确地将web.xml中的多个 DispatcherServlet映射到特定URL时,该映射是本地部分或URL的一部分. <c:url><spring:url>都可以添加servlet上下文部分,但是servlet前缀是Web应用程序的一部分,没有人会猜到它.毕竟,您应该有一个从servlet 1服务的页面到servlet 2服务的页面的链接,所有链接仍将在同一servlet上下文中.

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.

恕我直言,您应该了解为什么在一个Spring MVC应用程序中需要多个DispatcherServlet是很不常见的.您会发现许多教程和示例(只是google ...),而我所知道的全部都使用一个DispatcherServlet

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

如果您确实想这样做(但实际问题确实需要吗?),则可以使用拦截器,该拦截器将在所有请求中添加包含Servlet路径的属性.您在每个servlet应用程序上下文中声明一个拦截器bean,并将servlet路径设置为其.拦截器可能看起来像:

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;
    }
}

在您的JSP中,您使用:${servletPath}/user

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

这篇关于将调度程序servlet映射附加到url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆