当前位置: 首页 > 知识库问答 >
问题:

spring web-mvc 4 java配置不工作

穆正祥
2023-03-14

我正在尝试在没有xml配置的情况下运行基本的spring-4 web-mvc应用程序。我看过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!";
}
}
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警告[http-nio-8080-exec-1]org.springframework.web.servlet.pagenotfound.nohandler在名为“dispatcher”的DispatcherServlet中未找到URI为[/home/]的HTTP请求的映射14-feb-2015 11:35:32.766信息[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信息[localhost-startStop-1]org.apache.catalina.startup.hostconfig.部署目录部署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.nohandler在名为“dispatcher”的DispatcherServlet中找不到URI为[/home/]的HTTP请求的映射

共有1个答案

颜河
2023-03-14

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

请尝试改用AnnotationConfigWebApplicationContext#寄存器。

AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(WebConfig.class);
 类似资料:
  • 那是我学习Kafka的初期。我正在检查我本地机器中的每一个Kafka属性/概念。 所以我遇到了属性,下面是我的理解。如果我误解了什么,请纠正我。 将消息发送到主题后,必须将消息写入至少关注者数。 还包括引导。 如果可用活动代理的数量(间接地,在同步副本中)少于指定的,则生产者将引发发布消息失败的异常。 以下是我创建上述场景所遵循的步骤 在本地启动了3个代理,代理ID为0、1和2 创建了主题insy

  • 但就是不管用。我也试过在-d中使用这个选项,但这也不起作用。 谢谢你的帮助

  • 下面是我正在使用的配置。消息没有错误,从exchange到队列都可以正常工作,并且侦听器可以进行转换,这很好。对于错误消息,我希望发生的是,当我抛出AmqpRejectAndDontRequeueException时,“rabbitQueue”将消息转发到死信交换,并最终进入“rabbitErrorQueue”但死信交换或队列上没有任何活动。有人知道我做错了什么吗?

  • 我正在开发一个带有的Spring Boot[web]REST风格的应用程序(因为它需要部署到现有的Tomcat服务器)。它有一个,其方法在调用时需要写入Redis。我让Redis服务器在localhost上运行(默认端口,没有密码)。POM文件的相关部分具有所需的启动器依赖关系: 当我部署战争并到达终点时,我得到这样的回应: 我在Spring Boot应用程序类中添加了以下内容: 在执行一些测试R

  • 我正试图使用两个redis节点设置哨兵。请找到内联的conf文件。 端口16371 dir“C:\程序文件\redis\16371\” loglevel通知 日志文件“C:\Program Files\redis\logs\16371.log” 哨兵监视器示例127.0.0.1 6371 *致命配置文件错误*读取配置文件,在第5行 有人能帮我把这个修好吗。蒂亚:)

  • 我的log4j2中有此配置。xml文件: 我将此文件放在src/resources中,并遵循以下指南:guide,但当我运行项目时,出现以下错误:

  • 我正在做一个Spring Boot应用程序,并试图以编程方式配置kafka,但由于某些原因,我仍然在从应用程序获取属性。yaml而不是我通过编程设置的

  • 更新:清理并直接指出问题和解决方案。 问题: Docker-tomcat已正确安装并运行,但Manager应用程序中的403访问错误除外。我的docker tomcat似乎也找不到我的tomcat-users.xml配置。 解决方案 感谢Farhad和Sanket的回答。 [文件]: DockerFile Tomcat用户配置(conf/tomcat-users.xml) 应用程序上下文(weba