有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

SpringWebMVC4Java配置不工作

我正在尝试运行基本的spring-4WebMVC应用程序,完全不需要xml配置。我查看了spring文档和示例,但对我来说不起作用。 我的控制器:

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 {
}

网络配置:

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();
    }
}

和网络初始值设定项:

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 WARNING [http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/home/] in DispatcherServlet with name 'dispatcher' 14-Feb-2015 11:35:32.766 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /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 Deployment of web application directory /home/nikolay/apache-tomcat-8.0.9/webapps/manager has finished in 136 ms 14-Feb-2015 11:35:34.888 WARNING [http-nio-8080-exec-3] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/home/] in DispatcherServlet with name 'dispatcher'


共 (2) 个答案

  1. # 1 楼答案

    我认为注册配置类的方式不正确

    试着用AnnotationConfigWebApplicationContext#register代替

    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(WebConfig.class);
    
  2. # 2 楼答案

    也许你需要改变

    dispatcher.addMapping("/*");
    

    dispatcher.addMapping("/");