代码之家  ›  专栏  ›  技术社区  ›  thickGlass

没有web的基于注释的spring应用程序。xml不工作

  •  1
  • thickGlass  · 技术社区  · 9 年前

    我正在尝试在不使用web的情况下创建一个spring项目。xml,但我经常遇到这个错误,我已经尝试了所有方法,但到目前为止,没有任何解决问题的方法,

    **Sep 15, 2015 11:36:50 AM org.springframework.web.servlet.PageNotFound noHandlerFound
    WARNING: No mapping found for HTTP request with URI [/TestApp/] in DispatcherServlet with name 'dispatcher'**
    

    这是我的配置:-

    package com.springWeb.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.WebApplicationInitializer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import     org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcher    ServletInitializer;
    
    public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
    
        return new Class[] { AppConfig.class };
    }
    
    @Override
    protected Class<?>[] getServletConfigClasses() {
    
        return null;
    }
    
    @Override
    protected String[] getServletMappings() {
        System.out.println("\n\n\n\n\n\n now deploying");
        return new String[]{ "/" };
    }
    
    }
    

    我的AppConfig类

    package com.springWeb.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;
    
    @EnableWebMvc
    @Configuration
    @ComponentScan(basePackages = "com.springweb.controller.*")
    @Import({ SpringSecurityConfig.class })
    public class AppConfig {
    
    @Bean
    public InternalResourceViewResolver viewResolver() {
        System.out.println("\n\n\nello hello hello");
        InternalResourceViewResolver viewResolver
                = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
    
    }
    

    还有我的控制器

    package com.springWeb.controller;
    
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class TestController {
    @RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
    public String index() {
        System.out.println("test est test ets tstsd");
        return "index2";
    }
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   CollinD    9 年前

    我怀疑是你的 @ComponentScan 指令。尝试将其更改为

    @ComponentScan({"com.springWeb.*"})
    

    看起来你可能有一个 com.springweb 全部小写。