即使检查了其他答案并将代码与我开发的其他应用程序进行比较,我也无法正确加载index.jsp。我正在使用Spring MVC,使用注释。问题是我以前做过同样的事情并且有效,这次我的错误是什么?
MvcConfig:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "tool.controller" })
public class MvcConfig extends WebMvcConfigurerAdapter {
private static final String VIEW_CONTROLLER = "/index.htm";
private static final String VIEW_NAME = "/view/index.jsp";
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
registry.addViewController(VIEW_CONTROLLER).setViewName(VIEW_NAME);
}
@Override
public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
WebInitializer:
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(BasicConfig.class, MvcConfig.class);
servletContext.addListener(new ContextLoaderListener(context));
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
ServletRegistration.Dynamic dispatcher;
dispatcher = servletContext.addServlet("dispatcher",
new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
的IndexController:
@Controller
public class IndexController {
private static final String INDEX_VIEW = "/view/index.jsp";
@RequestMapping(value = { "/", "/index" })
public ModelAndView home() {
ModelAndView modelAndView = new ModelAndView(INDEX_VIEW);
return modelAndView;
}
}
文件夹结构:
来自Tomcat初始化的控制台消息可能相关:
任何帮助将不胜感激,谢谢!
编辑:build.gradle
apply plugin: 'base'
apply plugin:'war'
apply plugin: 'jetty'
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin:'idea'
ext.springVersion='4.1.4.RELEASE'
ext.hibernateVersion='4.3.8.Final'
ext.hibernateValidatorVersion='5.1.3.Final'
repositories {
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.10'
compile 'org.slf4j:slf4j-simple:1.7.10'
compile 'ch.qos.logback:logback-core:1.1.2'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'org.springframework.data:spring-data-jpa:1.3.0.RELEASE'
compile "org.springframework:spring-jdbc:$springVersion"
compile "org.springframework:spring-tx:$springVersion"
compile "org.springframework:spring-orm:$springVersion"
compile "org.springframework:spring-aop:$springVersion"
compile "org.springframework:spring-webmvc:$springVersion"
compile "org.springframework:spring-oxm:$springVersion"
compile "org.springframework.security:spring-security-core:4.1.3.RELEASE"
compile "org.hibernate:hibernate-entitymanager:$hibernateVersion"
compile "org.hibernate:hibernate-core:$hibernateVersion"
compile "org.hibernate:hibernate-validator:$hibernateValidatorVersion"
compile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final"
compile 'org.postgresql:postgresql:9.4-1201-jdbc41'
compile 'javax.servlet.jsp:javax.servlet.jsp-api:2.3.1'
compile 'javax.servlet:jstl:1.2'
testCompile "org.mockito:mockito-core:1.+"
testCompile 'junit:junit:4.11'
}
eclipse {
project.natures "org.springframework.ide.eclipse.core.springnature"
}
答案 0 :(得分:0)
还尝试根据spring的良好实践修改控制器类。
@Controller
public class IndexController {
//private static final String INDEX_VIEW = "/view/index.jsp";
@RequestMapping(value = { "/", "/index" })
public ModelAndView home() {
ModelAndView modelAndView = new ModelAndView("index");
return modelAndView;
}
}
答案 1 :(得分:0)
我显然发现了什么问题。首先,我正在研究Eclipse IDE。我找到的解决方案是将文件夹“webapp”添加到项目属性中的“Web部署程序集”中。执行此操作后,index.jsp正在正确加载,我不再收到404错误。