【发布时间】:2016-09-17 16:54:53
【问题描述】:
我正在关注“Spring in action - Craig Walls”一书并遇到以下错误消息。提到了很多与web.xml 相关的问题。我使用的是 Java 配置,而不是 web.xml。
spitter.web 包中的控制器:
package spitter.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value="/", method = RequestMethod.GET)
public String home(){
return "home";
}
}
spittr.config 包中的 Dispatcher servlet 配置:
package spittr.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
@Override
protected Class<?>[] getRootConfigClasses() {
//return new Class<?>[] {RootConfig.Class};
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
//return new Class<?>[] {WebConfig.Class};
return null;
}
}
Rootconfig 在同一个包中:
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 {
}
WebConfig:
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 viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WebContent/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
configurer.enable();
}
}
我正在使用 Maven 来解决依赖关系,并在 Eclipse 中使用 Tomcat 9 来运行。
2016 年 9 月 17 日下午 4:46:48 org.apache.catalina.startup.Catalina 开始 信息:服务器在 5007 毫秒内启动 2016 年 9 月 17 日下午 4:46:49 org.springframework.web.servlet.PageNotFound noHandlerFound 警告: 未找到带有 URI [/SpringMVC/] 的 HTTP 请求的映射 DispatcherServlet 名称为“dispatcher”
我的视图home.jsp 在WebContent/WEB-INF/views/home.jsp 中。
【问题讨论】:
-
确保所有的spring依赖都添加到类路径...
-
取消注释
SpittrWebAppInitializer中的那些注释行并删除它们下面的行。 -
我也确信
resolver.setPrefix("/WebContent/WEB-INF/views/");以及您的视图home.jsp在webContent/WEB-INF/views/home.jsp中。 -
您尝试访问的网址是什么?
-
@Sekar, resolver.setPrefix("/WebContent/WEB-INF/views/");和webContent/WEB-INF/views/home.jsp,还是有一些区别的。 webContent 与 WebContent 不同。尝试有一个统一的名字。区分大小写
标签: java spring spring-mvc