基于注释的springapplication,没有web.xml无法正常工作

时间:2015-09-15 06:04:10

标签: spring spring-mvc spring-security spring-integration spring-annotations

我正在尝试在不使用web.xml的情况下构建一个spring项目,但是我经常遇到这个错误,我已经尝试了所有内容,但到目前为止还没有解决问题,

**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 个答案:

答案 0 :(得分:1)

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

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

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