Spring MVCのは、私のコントローラを検出していません

cristid9:

私は、次のファイルでのSpring MVCプロジェクトを持っています:

/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
      <display-name>Archetype Created Web Application</display-name>
       <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/appconfig-root.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>

そして、2つのコンフィギュレーションファイル:

/src/main/webapp/WEB-INF/appconfig-root.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="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.xsd">

    <import resource="appconfig-mvc.xml"/>

    <context:component-scan base-package="yc.servlets"/>

    <!--<context:property-placeholder location="classpath:application.properties"/>-->
</beans>

/src/main/webapp/WEB-INF/appconfig-mvc.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">

    <mvc:annotation-driven/>

    <mvc:resources mapping="/resources/**" location="/resources/"/>

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

        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

そして、私のコントローラを含むクラス:

package yc.servlets;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class TestPage {


    @RequestMapping(value = "/hello_there", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }
}

私は、これは働いて春のMVCを取得するのに十分だと思いますが、私が入力したときにlocalhost:8888、ブラウザに私が取得404エラー。

localhost:8888/hello_thereまた、提供します404エラーを。

Mykhailoモスクワ:

こんにちは、あなたのコントローラーで「こんにちは」戻ってきているし、それはそれは404 HTTPエラーがスローされます見つけるいけない場合、そのはHello.jspのを見つけるつもり

あなたのInternalViewResolverを構成しました。

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

        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

コントローラで「こんにちは」ので、あなたは文字列を返すされている場合、/WEB-INF/jsp/hello.jspで検索しようとしています。

@RequestMapping(value = "/hello_there", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }

これは、 『こんにちは』に接尾辞「.jspファイル」を追加しますと、それは404を投げます、それはそれを見つけることができなかったとして 『Hello.jspの』を見つけようとします

あなたは暗黙的に@ResponseBody注釈を追加し@RestControllerにご@Controllerから変更すると、それが応答で「こんにちは」を返すかどうかを確認することができます

また、これはあなたが変更することができます解決しない場合:

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

これと:

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

その今も働いて試してみるの次のステップの場合:

そのようなURLを試してみてください。

localhost:8080/yourApplicationName/hello_there

デフォルトconextパスあなたはTOMCAT_HOMEに行く場合は、ここでそのアプリ名/ webappsに/

あなたは、ルートフォルダにアプリケーションが存在しないことがわかります

それはローカルホストによって発見できなかった理由のthatsので:8080 / ...

次の2つの方法でこの問題を解決することができます。

1.Find your application deployed and copy to root folder in 
    TOMCAT_HOME/webapps/ROOT
 2.See the name of your application in  TOMCAT_HOME/webapps/ 
   and call url : localhost:8080/yourAppName/hello_world   

感謝

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=207937&siteId=1