【问题标题】:Spring MVC : Dispatcher mapping to the wrong viewSpring MVC:调度程序映射到错误的视图
【发布时间】:2012-08-04 12:42:17
【问题描述】:

问题是:我的观点是回头客。根据我的viewresolver应该映射到WEB-INF/pages/customer.html。相反,它正在通过调度程序 servlet 并且无法找到客户 html。它给出的错误是:“警告:在 DispatcherServlet 中找不到带有 URI [/SpringMVC/WEB-INF/pages/customer.html] 的 HTTP 请求的映射,名称为 'mvc-dispatcher'”

这是我的控制器

@Controller
public class CustomerController implements BeanFactoryAware {

    private Customers customers;


    /*public String getCustomer(@RequestParam String name) {

        //ApplicationContext context = new FileSystemXmlApplicationContext("/WEB-INF/springapp-servlet.xml");
        //Customers customers = get
        System.out.println("In Controller");
        return "customer";
    }*/

    @RequestMapping(value="/form")  
    public String getCustomer(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {


            System.out.println("In Customer Controller");
            return "customer";
    }

    @Override
    public void setBeanFactory(BeanFactory context) throws BeansException {
        // TODO Auto-generated method stub
        customers = (Customers)context.getBean("customers");

        //System.out.println(customers);

    }

}

这是我的 web.xml

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,/WEB-INF/beans.xml</param-value>
</context-param>

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

这是我的 dispatcher.xml


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

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    尝试更改您的 servlet 映射:

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

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

    【讨论】:

    • 我也尝试将 /* 更改为 / 但仍然是同样的问题。我有奇怪的行为。每当我将所有 html 重命名为 jsps 时,它都可以正常工作,但是将扩展名更改为 html 会给我带来问题。改成jsp的时候,我只是在internalviewresolver中把suffix属性改成jsp。
    【解决方案2】:

    您的 servlet 将拦截对匹配 '/*' 的 url 模式的所有调用,但它无法在任何 spring 控制器中找到指定的映射。

    我曾经遇到过类似的麻烦,我是如何克服这个问题的,方法是遵循您所有春季电话的特定模式。例如,

    <url-pattern>*.htm</url-pattern>
    

    希望对你有所帮助。

    【讨论】:

      【解决方案3】:

      这不起作用,因为最终调用将转到 RequestDispatcher,如下所示:

      RequestDispatcher dispatcher = httpRequest.getRequestDispatcher("/WEB-INF/pages/test.html");
      dispatcher.forward(httpRequest, httpRequest);
      

      此时容器将期望 DispatcherServlet 再次处理请求(因为 /* 路径)。如果是jsp页面,容器有*.jsp的映射,知道怎么处理。

      解决方法是将资源放置在相对于 Web 资源的某个位置(如果是 maven 结构,则在 src/main/webapp/resources/ 下,为此内容配置处理程序:

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

      现在你可以从你的控制器返回:

      return "forward:/resources/mypage.html";
      

      另外,我看到您正在查找“客户”bean,您不需要这样做,而是希望 Spring 将其注入:

      @Controller
      public class CustomerController{
      
          @Autowired private Customers customers;
      

      【讨论】:

        猜你喜欢
        • 2016-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多