2 qq302663258 qq302663258 于 2016.04.14 20:43 提问

spring mvc中javaConfig配置问题

spring mvc中关于继承AbstractAnnotationConfigDispatcherServletInitializer的疑问

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

@Override
protected Class<?>[] getRootConfigClasses() {
    // TODO Auto-generated method stub
    return new Class<?>[] {RootConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    // TODO Auto-generated method stub
    return new Class<?>[] {WebConfig.class};
}

@Override
protected String[] getServletMappings() {
    // TODO Auto-generated method stub
    return new String[]{"/"};
}

}

然后这个是WebConfig
@Configuration
@EnableWebMvc
@ComponentScan("spitter.web")
public class WebConfig extends WebMvcConfigurerAdapter{

@Bean
public ViewResolver viewResolver(){
    InternalResourceViewResolver resolver =
            new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    return resolver;
}

这Controller
@Controller

@RequestMapping("/homepage")
public class HomeController {

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

}

有一个home.jsp放在WEB-INF/views里。

这是一个jnuitTest
ublic class MyTest {

@Test
public void testHomePage() throws Exception{
    HomeController controller = new HomeController();
    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    mockMvc.perform(MockMvcRequestBuilders.get("/homepage"))
        .andExpect(MockMvcResultMatchers.view().name("home"));
}

运行的时候没有任何问题 Jnuit也没有问题  但是在Tomcat中运行的时候就404错误
HTTP Status 404 - /SpringinAction/homepage

type Status report

message /SpringinAction/homepage

description The requested resource is not available.

Apache Tomcat/8.0.33

1个回答

caozhy
caozhy   Ds   Rxr 2016.04.15 04:21
qq302663258
qq302663258 也许这个博客说的也是一种,但我在AbstractAnnotationConfigDispatcherServletInitializer这方面配置遇到了点迷惑,能不能就这个地方指点我一下!
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!