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

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

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

工程目录结构如下:
【Java】按照Spring实战配置的SpringMVC控制器不能对浏览器的请求进行跳转?

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

}

}

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

回答

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

以上是 【Java】按照Spring实战配置的SpringMVC控制器不能对浏览器的请求进行跳转? 的全部内容, 来源链接: utcz.com/a/89145.html

回到顶部