2011-02-08 3 views
2

J'ai un DispatcherServlet pour mes pages et un ServletContainer pour mon service web Jersey. Comme c'est le cas maintenant, aucune page n'est rendue (404), mais le service fonctionne correctement. Comment puis-je les exécuter conjointement?Problème Spring MVC multiple servlets (Jersey, Dispatcher)

J'ai vu ce fil How to register multiple servlets in web.xml in one Spring application qui semble traiter un problème similaire, même si cela concernait CXF donc je ne sais pas à quel point il est pertinent.

J'ai également essayé d'avoir un fichier Jersey-servlet.xml séparé, mais cela a empêché les pages et le service de fonctionner. Je ne sais pas vraiment où aller à partir de ce point, toute aide très appréciée.

web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 
    <display-name>HSRMVC</display-name> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

    <servlet> 
     <servlet-name>HSR</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>HSR</servlet-name> 
     <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 

    <servlet> 
    <servlet-name>Jersey REST Service</servlet-name> 
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>se.hsr.services</param-name> 
     <param-value>HSRMVC</param-value> 
    </init-param> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>Jersey REST Service</servlet-name> 
    <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

HSR-sevlet.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 


    <mvc:annotation-driven /> 
    <context:component-scan 
     base-package="se.hsr.web"/> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 

MISE À JOUR web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 
    <display-name>HSRMVC</display-name> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

    <servlet> 
     <servlet-name>HSR</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>HSR</servlet-name> 
     <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
    /resources/applicationContext.xml 
    /WEB-INF/service-beans.xml 
    </param-value> 
    </context-param> 

    <servlet> 
     <servlet-name>service-beans</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>se.hsr.services</param-name> 
      <param-value>HSRMVC</param-value> 
     </init-param> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>service-beans</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

</web-app> 
+0

"pas de pages sont rendu (404), mais le service fonctionne très bien" ... qu'est-ce que cela signifie? – skaffman

+0

Cela signifie que lorsque je pointe vers localhost/login.hmtl, j'obtiens un 404 -error. Quand j'écris localhost/service/emil, j'obtiens une page affichant mon nom. –

Répondre

0

J'ai résolu le problème.

Voici comment mon nouveau web.xml ressemble à:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 

    <display-name>HSRMVC</display-name> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath*:applicationContext.xml 
     /WEB-INF/HSR-service-beans.xml 
    </param-value> 
    </context-param> 

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

    <servlet> 
     <servlet-name>HSR</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>HSR</servlet-name> 
     <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 

    <servlet> 
     <servlet-name>HSR-service</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>se.hsr.services</param-name> 
      <param-value>HSRMVC</param-value> 
     </init-param> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>HSR-service</servlet-name> 
     <url-pattern>/service/*</url-pattern> 
    </servlet-mapping> 
</web-app> 
+0

Comment ' classpath *: applicationContext.xml' fonctionne? Quels sont les noms des fichiers de configuration de contexte pour vos servlets? '' fournit l'emplacement pour le contexte parent, qui est un pour toutes les servlets - ou ne l'est pas? –

+0

Salut Mikhail. Désolé, mais comme c'était il y a cinq ans, je n'ai pratiquement aucun souvenir de cela. J'espère que quelqu'un d'autre est capable de répondre à cela! –

3

The question you linked est pertinent à votre scénario.

Vous pouvez également essayer d'utiliser <mvc-default-servlet-handler />. Cependant, cela ne vise pas exactement ce but. (c'est destiné aux ressources statiques).

Vérifiez également this et this.

+0

Merci pour la réponse. J'ai lu les ressources auxquelles vous étiez lié mais je ne comprends toujours pas comment le faire fonctionner. J'ai essayé d'éditer mon web.xml (voir originalpost), mais le problème persiste identiquement à avant. –

Questions connexes