我试图在没有xml配置的情况下运行基本的spring-4 web-mvc应用程序。我看过弹簧文档和示例,但它对我没用。 我的控制器:
package com.nikolay.exam.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
@ResponseBody
public String home() {
return "Hello world!";
}
}
的AppConfig:
package com.nikolay.exam.config;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
}
WebConfig:
package com.nikolay.exam.config;
import com.nikolay.exam.controller.HomeController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public HomeController homeController() {
return new HomeController();
}
}
WebInitializer:
package com.nikolay.exam.config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.setConfigLocation("com.nikolay.exam.config");
servletContext.addListener(new ContextLoaderListener(root));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(root));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
}
}
但是当我在tomcat上运行我的应用程序时,我收到一个错误:
14-Feb-2015 11:35:29.825警告[http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound找不到带有URI [/ home /]的HTTP请求的映射DispatcherServlet,名称为' dispatcher' 14-Feb-2015 11:35:32.766 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory部署Web应用程序目录/home/nikolay/apache-tomcat-8.0.9/webapps/manager 14-Feb-2015 11:35:32.904 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory部署Web应用程序目录/home/nikolay/apache-tomcat-8.0.9/webapps/manager已完成136毫秒 14-Feb-2015 11:35:34.888警告[http-nio-8080-exec-3] org.springframework.web.servlet.PageNotFound.noHandlerFound在DispatcherServlet中找不到带有URI [/ home /]的HTTP请求的映射名称'调度'
答案 0 :(得分:1)
我认为这是注册配置类的方式不正确。
请尝试使用AnnotationConfigWebApplicationContext#register。
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(WebConfig.class);
答案 1 :(得分:-1)
可能你需要改变
dispatcher.addMapping("/*");
到
dispatcher.addMapping("/");