【问题标题】:No mapping found for HTTP request with URI [/proj/] in DispatcherServlet with name 'dispatcher'在名称为“dispatcher”的 DispatcherServlet 中找不到具有 URI [/proj/] 的 HTTP 请求的映射
【发布时间】:2016-09-17 16:54:53
【问题描述】:

我正在关注“Spring in action - Craig Walls”一书并遇到以下错误消息。提到了很多与web.xml 相关的问题。我使用的是 Java 配置,而不是 web.xml

spitter.web 包中的控制器:

package spitter.web;

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

@Controller
public class HomeController {

    @RequestMapping(value="/", method = RequestMethod.GET)
    public String home(){
        return "home";
    }
}

spittr.config 包中的 Dispatcher servlet 配置:

package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        //return new Class<?>[] {RootConfig.Class};
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        //return new Class<?>[] {WebConfig.Class};
        return null;
    }
}

Rootconfig 在同一个包中:

package spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"spitter"}, excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
public class RootConfig {

}

WebConfig:

package spittr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("spitter.web")
public class WebConfig extends WebMvcConfigurerAdapter{
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WebContent/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
         configurer.enable();
    }
}

我正在使用 Maven 来解决依赖关系,并在 Eclipse 中使用 Tomcat 9 来运行。

2016 年 9 月 17 日下午 4:46:48 org.apache.catalina.startup.Catalina 开始 信息:服务器在 5007 毫秒内启动 2016 年 9 月 17 日下午 4:46:49 org.springframework.web.servlet.PageNotFound noHandlerFound 警告: 未找到带有 URI [/SpringMVC/] 的 HTTP 请求的映射 DispatcherServlet 名称为“dispatcher”

我的视图home.jspWebContent/WEB-INF/views/home.jsp 中。

【问题讨论】:

  • 确保所有的spring依赖都添加到类路径...
  • 取消注释SpittrWebAppInitializer 中的那些注释行并删除它们下面的行。
  • 我也确信resolver.setPrefix("/WebContent/WEB-INF/views/"); 以及您的视图home.jspwebContent/WEB-INF/views/home.jsp 中。
  • 您尝试访问的网址是什么?
  • @Sekar, resolver.setPrefix("/WebContent/WEB-INF/views/");和webContent/WEB-INF/views/home.jsp,还是有一些区别的。 webContent 与 WebContent 不同。尝试有一个统一的名字。区分大小写

标签: java spring spring-mvc


【解决方案1】:

感谢您的帮助。我进行了以下更改,现在可以使用了。

我将 web.config 移动到与构造函数相同的包中。然后创建 war 文件并部署在 Apache Tomcat 中。现在我可以访问该网站了。

【讨论】:

    猜你喜欢
    • 2015-08-04
    • 2014-06-20
    • 2011-03-30
    • 2016-04-18
    • 1970-01-01
    • 2016-12-06
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多