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

 mobiledu2502881303 发布于 2022-10-29 21:33

在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";
    }

}

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

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

    2022-10-31 17:46 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有