spring web-mvc 4 java 配置不起作用 [英] spring web-mvc 4 java config doesn't work

查看:35
本文介绍了spring web-mvc 4 java 配置不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在没有 xml 配置的情况下运行基本的 spring-4 web-mvc 应用程序.我查看了 spring 文档和示例,但它对我不起作用.我的控制器:

I'm trying to run elementary spring-4 web-mvc application without xml configuration at all. I've looked spring documentation and examples, but it didn't work for me. My controller:

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!";
}
}

应用配置:

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

和 WebInitializer:

And 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 上运行我的应用程序时,我收到一个错误:

But when I run my application on tomcat I receive an error:

2015 年 2 月 14 日 11:35:29.825 警告 [http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound 未找到具有 URI [/home/] 的 HTTP 请求的映射DispatcherServlet 名为dispatcher"2015 年 2 月 14 日 11:35:32.766 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 部署 Web 应用程序目录/home/nikolay/apache-tomcat-8.0.9/webapps/manager2015 年 2 月 14 日 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 毫秒内完成2015 年 2 月 14 日 11:35:34.888 警告 [http-nio-8080-exec-3] org.springframework.web.servlet.PageNotFound.noHandlerFound 在 DispatcherServlet 中未找到具有 URI [/home/] 的 HTTP 请求的映射,名称为'调度员'

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'

推荐答案

我认为这是您注册配置类的方式不正确.

I think it's the way you register your config class that is not correct.

尝试使用 AnnotationConfigWebApplicationContext#register 代替.

AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(WebConfig.class);

这篇关于spring web-mvc 4 java 配置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆