当我将@RequestMapping的值设置为" /&#34时,控制器设置被执行。

时间:2017-03-26 09:46:23

标签: java spring-mvc

我想将应用程序的映射根路径发送到特定的控制器。以下是我这样做的方式:

我的控制器:

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

当我进入localhost:8080时,控制器未按预期执行。 我尝试将@RequestMapping(value = "/", method = RequestMethod.GET)更改为@RequestMapping(value = "/abc", method = RequestMethod.GET),并输入localhost:8080 / abc,控制器按预期执行。

所以我认为问题在于@requestMapping值属性的语法。

所以,我的问题是如何使用requestMapping映射根路径?

我搜索了相对关键字但没有找到任何内容。

2 个答案:

答案 0 :(得分:0)

请将@RequestMapping(value =&#34; /&#34;)注释添加到控制器,然后尝试。

以下是我的工作示例,

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

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

答案 1 :(得分:0)

解决。请参阅Spring welcome-file-list correct mapping

的第一个答案

添加

int main()  
{  
    int m,n;  
    scanf("%d%d",&m,&n); 
    int a[m][n];  
    foo(a,m,n);  
    return 0;  
}  

到我的web.xml。

为我工作。