2017-01-04 33 views
1

我已經具有Java項目春天控制器,它處理URL春季增加外部罐子控制器不工作

這裏是控制器代碼

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 


@EnableWebMvc 
@Controller 
public class SmartContentValidator { 

@RequestMapping(value = "/validate") 
public String validate() { 
    System.out.println("Yo"); 

    return "YO"; 
} 
} 

然後我在上面的Java項目作爲jar文件導出。 然後創建一個新的Web項目並在構建路徑中添加上面的jar。下面是web.xml和調度-servlet.xml中

的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" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0"> 
<display-name>SmartContentValidatorTest</display-name> 

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

調度-servlet.xml中

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

<context:component-scan base-package="com" /> 
<context:annotation-config/> 

<bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/WEB-INF/views/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

現在當我嘗試打http://localhost:8080/project_name/validate

它顯示404而不是瀏覽器的發現和

WARNING: No mapping found for HTTP request with URI [/SmartContentValidatorTest/validate/] in DispatcherServlet with name 'dispatcher' 

在我的Eclipse控制檯。

問題是什麼?我錯過了什麼嗎?

編輯

項目結構 enter image description here

+0

你找到任何解決這個問題。如果是的話請寫一個答案。我有同樣的問題。 – Darshana

回答

1

通常這是由Spring引起了不正確的找到您的控制器。如果啓用DEBUG日誌記錄,Spring將記錄啓動應用程序時註冊的bean。

如果它正確地加載你的控制器,然後檢查你的根路徑是否正確(我認爲這是你的戰爭名稱默認情況下),它應該在啓動過程中再次顯示在日誌中。

1

我不確定它是否有助於您的情況,但我認爲在獨立應用程序中會出現問題,因爲@EnableWebMvc應與Java配置類一起使用,類註釋@Configuration,而不是@Controller bean。

當您使用xml配置時,您可以嘗試將<mvc:annotation-driven />,請參閱https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable以瞭解更多詳細信息。

您還需要提供您的應用程序上下文作爲調度的servlet PARAM像例如http://docs.spring.io/spring-flex/docs/1.0.x/reference/html/ch02s02.html在:

<servlet> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/config/web-application-config.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet>