如何在一个Spring应用程序中的web.xml中注册多个servlet [英] How to register multiple servlets in web.xml in one Spring application
问题描述
我想在我的Spring web.xml中定义两个servlet - 一个用于应用程序html / jsp页面,另一个用于将由外部应用程序调用的Web服务。这是web.xml:
I want to define two servlets in my Spring web.xml - one for the application html/jsp pages, and one for a web service that will be called by an external application. Here is the web.xml:
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/user-service-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>user-webservice</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>user-webservice</servlet-name>
<url-pattern>/UserService/*</url-pattern>
</servlet-mapping>
如果我有myservlet单独使用文件中的DispatcherServlet,它可以正常工作。如果我的user-webservice带有context-param,那就是它的配置文件(user-service-servlet.xml),它可以正常工作。但是,如果我同时在文件中,则myservlet不起作用,因为myservlet-servlet.xml文件未自动加载。如果我删除了context-param,那么myservlet可以工作,但是user-webservice不能正常工作,因为它的配置文件(user-service-servlet.xml)没有加载。
If I have myservlet use the DispatcherServlet in the file by itself, it works fine. If I have the user-webservice with the context-param for it's config file (user-service-servlet.xml), it works fine. However, if I have both in the file, then the myservlet doesn't work as the myservlet-servlet.xml file isn't loaded automatically. If I remove the context-param, then the myservlet works, but the user-webservice doesn't work as it's configuration file (user-service-servlet.xml) isn't loaded.
如何定义两个servlet并加载它们的两个配置文件?
How can I have both servlets defined and both of their configuration files loaded?
推荐答案
如此主题邮件列表,您可以将整个批次加载到根上下文中,而不是让CXFServlet从 user-webservice-servlet.xml
加载自己的弹簧上下文。将现有的 user-webservice-servlet.xml
重命名为其他名称(例如 user-webservice-beans.xml
)然后将 contextConfigLocation
参数更改为:
As explained in this thread on the cxf-user mailing list, rather than having the CXFServlet load its own spring context from user-webservice-servlet.xml
, you can just load the whole lot into the root context. Rename your existing user-webservice-servlet.xml
to some other name (e.g. user-webservice-beans.xml
) then change your contextConfigLocation
parameter to something like:
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/user-webservice-beans.xml
</param-value>
</context-param>
<servlet>
<servlet-name>user-webservice</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>user-webservice</servlet-name>
<url-pattern>/UserService/*</url-pattern>
</servlet-mapping>
这篇关于如何在一个Spring应用程序中的web.xml中注册多个servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!