2017-09-08 4 views
0

ich folgende bin dieses Spring Docs und konfiguriert web.xml und applicatioinContext.xml Funktion Einzelstammkontext in Spring Web MVC verwenden heißt es keine Servlets WebApplicationContext ist; Alle Anfragen werden von Root WebApplicatioinContext bedient.HTTP-Anforderungen werden nicht an Steuerungen von Wurzel WebApplicationContext delegierte

Aber Problem ist HTTP-Anforderungen werden nicht an die Controller von Root WebApplicationContext delegiert.

web.xml:

<web-app> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/root-context.xml</param-value> 
    </context-param> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value></param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
</web-app> 

Wurzel context.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="PostController" class="org.my.controllers.PostController"> 
    </bean> 

</beans> 

Controller:

@Controller 
public class PostController { 
    @RequestMapping(value= "/controller", method = RequestMethod.GET) 
    @ResponseBody 
    public String foo() { 
     return "Response!"; 
    } 
} 

Also, wenn ich Hit URL http://localhost:8080/PracticeJavaWeb/controller in Browser ist es 404 zeigt

Server-Protokoll zu sagen:

Warnung: Keine Zuordnung für HTTP-Anforderung mit URI gefunden [/ PracticeJavaWeb/Controller] in DispatcherServlet mit Namen 'Dispatcher'

Spring Docs sagen:

Es ist auch möglich, nur einen Stammkontext für einzelne DispatcherServlet-Szenarien zu haben.

Antwort

2

Nur eine kleine Änderung an der Wurzel-context.xml Datei, fügen Sie diese:

<mvc:annotation-driven></mvc:annotation-driven> 

mvc:annotation-driven ermöglicht Kontextpfad Feder mvc Anmerkung zu erkennen, um die Anfrage zu versenden zu die Controller.

Auch hier ist ein vollständiges Beispiel einer Wurzel context.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:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
     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-4.3.xsd"> 


    <mvc:annotation-driven></mvc:annotation-driven> 

    <bean id="welcomeService" class="com.myorg.controller.MyService"></bean> 
    <bean id="welcomControler" class="com.myorg.controller.WelcomeController"></bean> 
    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/views/"></property> 
    <property name="suffix" value=".jsp"></property> 
    </bean> 

</beans> 
1

Sie benötigen <mvc:annotation-driven /> Tag in root-context.xml hinzuzufügen.

<mvc:annotation-driven /> Das Tag stellt sicher, dass Ihre Anfrage an die Controller gesendet wird.

Und vergessen Sie nicht mvc Namensraum als auch in Wurzel context.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
</beans> 
hinzufügen
Verwandte Themen