2017-03-26 2 views
0

アプリケーションのマップルートパスを特定のコントローラーにしたい。以下は、私がこれをしなかった方法です:@RequestMappingの値を "/"に設定するとコントローラーが実行されない

マイコントローラ:

@Controller 
public class Index { 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
     public String indexPage(ModelMap model, @CookieValue(value = "cityName", defaultValue = "beijing") String cityNameCookie, HttpServletRequest request) { 
     String cityName = CookieTools.setCityNameValue(request, cityNameCookie); 
     model.addAttribute("cityName", cityName); 
     System.out.println("index"); 
     return "index"; 
    } 
} 

私のweb.xml:私はlocalhostを入力すると

<web-app ...> 
    <display-name>Archetype Created Web Application</display-name> 

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


    <servlet-mapping> 
     <servlet-name>springmvc</servlet-name> 
     <url-pattern>/</url-pattern> 
     <url-pattern>*.do</url-pattern> 
    </servlet-mapping> 

</web-app> 

:8080を予想通り、コントローラは実行されません。 @RequestMapping(value = "/", method = RequestMethod.GET)@RequestMapping(value = "/abc", method = RequestMethod.GET)に変更し、localhost:8080/abcと入力すると、コントローラが期待どおりに実行されます。

私は問題が@requestMapping値属性の構文にあると思います。

私の質問はです。requestMappingを使用してルートパスをマップするにはどうすればよいですか?

私は相対キーワードを探知しましたが、何も見つかりませんでした。

+0

使用しているサーバーで問題なく動作しますか? tomcatの場合、webappsフォルダ内に配置されたプロジェクト名は何ですか? – developer

+0

'/'にマップされた別のコントローラがありますか? – utkusonmez

+0

@ javaguy私はtomcatとintellijのアイデアを使用します。私のプロジェクト名はjiaotongですが、Tomcatのルートコンテキストで展開しました。 – inter18099

答えて

0

コントローラに@RequestMapping(value = "/")アノテーションを追加してから、試してください。ここで

は解決

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

@Controller 
@RequestMapping("/") 
public class HelloController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String printWelcome(ModelMap model) { 

     model.addAttribute("message", "Spring MVC Hello World"); 
     return "hello"; 

    } 

} 

春の設定、

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

    <context:component-scan base-package="com.mkyong.common.controller" /> 

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

</beans> 

web.xmlの

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring Web MVC Application</display-name> 

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

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

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
    </context-param> 

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

</web-app> 
+0

私は試しましたが、同じ問題が発生しました。 – inter18099

0

、私のために例を働いています。私のweb.xmlに

<welcome-file-list> 
    <welcome-file></welcome-file> 
</welcome-file-list> 

を追加Spring welcome-file-list correct mapping

の最初の答えを参照してください。

私のために働いた。

関連する問題