首页  >  问答  >  正文

java - 按照Spring实战配置的SpringMVC控制器不能对浏览器的请求进行跳转?

在Eclipse中按照《Spring实战》第四版关于SpringMVC控制器的有关代码写了一个最简单的控制器,
但是在浏览器中却无法通过http://localhost:8080/homepage跳转到在控制器中指定的home.jsp文件,一直提示404错误。

奇怪的是使用mockMvc对跳转的返回值进行验证确实是正确的(这个验证方式也是书上教的),
想问下我的问题到底出在哪里?

工程目录结构如下:

SpittrWebAppInitializer.java代码如下:

package spittr.config;

import org.springframework.web.servlet.support.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.java如下:

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 viewRseolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }
    
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }

}

RootConfig.java如下:

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 {

}

控制器HomeController.java如下:

package spittr.web;

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

@Controller
@RequestMapping({"/","/homepage"})
public class HomeController {
    
    @RequestMapping(method=RequestMethod.GET)
    public String home(){
        return "home";
    }

}

实在找不到问题在哪里……

PHP中文网PHP中文网2741 天前361

全部回复(1)我来回复

  • 迷茫

    迷茫2017-04-18 10:33:08

    单词拼错了,无地自容……
    我的包名是spittr
    但是我的自动扫描写的是spitter,
    问题解决了

    回复
    0
  • 取消回复