春は

2017-01-04 1 views
1

を働いていないコントローラと外部のjarファイルを追加し、私はここに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ファイルとしてJavaプロジェクトの上にエクスポートしてきました。 その後、新しい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

をヒットしようとすると、それはブラウザと私のEclipseのコンソールで

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

で見つかった404-ません示しています。

問題点は何ですか?何か不足していますか?通常、これは正しくコントローラを見つけていない春によって引き起こされる

編集

プロジェクト構造 enter image description here

+0

あなたはこの問題への解決策を見つけました。もしそうなら、答えを書いてください。私は同じ問題を抱えている。 – Darshana

答えて

1

。 DEBUGロギングを有効にすると、Springはアプリケーションの起動時に登録されているBeanを記録します。

コントローラーが正しくロードされている場合は、ルートパスが正しいことを確認してください(これはデフォルトでは戦争名です)。これは起動時に再度ログに表示する必要があります。

1

私はそれがあなたのケースに役立ちますが、私は@EnableWebMvcは、Javaの設定で使用されなければならないとして、スタンドアロンアプリケーションに問題があるだろうと思うかどうかわからないです - @Configurationで注釈が付けクラス、ない@Controller豆と。

xml設定を使用する場合は、<mvc:annotation-driven />を入力してください。詳細はhttps://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enableを参照してください。

また、例http://docs.spring.io/spring-flex/docs/1.0.x/reference/html/ch02s02.htmlのようにディスパッチャサーブレットのparamとして、アプリケーションのコンテキストを提供する必要があるでしょう:

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