我有spring boot 1.5应用程序,我使用javaConfig类MVCConfigAn.java定义了自定义webapplicationContext,使用ServletRegistrationBean从springbootapplication calss调用了该方法,控制器被正常调用,但是视图未显示,我得到404错误
这是代码:
package org.me
@SpringBootApplication
public class AppConfig extends SpringBootServletInitializer{
public static void main(String[] args ){
SpringApplication.run(AppConfig.class, args);
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
//viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public ServletRegistrationBean dispatcherRegistration() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(MVCconfigAn.class);
dispatcherServlet.setApplicationContext(ctx);
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet,"/admin/*");
registration.setLoadOnStartup(1);
//registration.addUrlMappings("/admin/*");
registration.setName("myDispatcher");
return registration;
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AppConfig.class);
}
/*
CommandLineRunner run(){
return null;
}*/
}
我的MVCconfigAn.java是空的,它在org1.me程序包中,带有@ComponentScan批注,用于扫描cotroller
package org1.me
@Controller
@RequestMapping(value="/",path="/")
public class ControllerAnnot {
@RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
model.addAttribute("greeting", "Hello World from Spring 4 MVC");
return "welcome";
}
@RequestMapping(value = "/helloagain", method = RequestMethod.GET)
public String sayHelloAgain(ModelMap model) {
model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC");
System.out.println("coucou");
return "you";
}
}
我的application.properties文件包含:
server.context-path=/myapp
server.port= 8081
我的午餐应用程序带有:java -jar myapplication.war
我愿意:http://localhost:8081/myapp/admin/helloagain 已达到对控制器的请求,显示了内部消息:system.out.println,但出现404错误
当我在tomcat中部署应用程序时,它工作正常,但没有/ myapp