2017-03-26 18 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:

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

    } 

} 

Spring配置,

<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

相關問題