找不到另一个MVC映射

时间:2012-12-26 08:56:32

标签: java spring model-view-controller servlets mapping

之前我已经设置了一些弹簧MVC配置,但这次似乎我错过了一些东西。配置如下:

web.xml:

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

<listener>
    <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

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

dispatcher-servlet.xml:

<context:annotation-config />

<context:component-scan base-package="mypackage.controller" />

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/gupld">uploadController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

ManController.java:

package mypackage.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MainController {

    @RequestMapping("/test")
    public ModelAndView welcome() {
        ModelAndView mav = new ModelAndView("_index");
        return mav;
    }
}

当我致电http://127.0.0.1:8080/mycontext/test/时,我得到:

http-8080-1 INFO  2012-12-26 09:27:24,799 SimpleUrlHandlerMapping - Mapped URL path [/test] onto handler 'mainController'
http-8080-1 WARN  2012-12-26 09:27:24,887 PageNotFound - No mapping found for HTTP request with URI [/mycontext/test/] in DispatcherServlet with name 'dispatcher'

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您正在使用url-pattern作为/test/*来加载dispatcher servlet。

但是您发送的/mycontext/test/未映射到提供的网址。

尝试使用它,

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

或使用此

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