Spring MVC不适用于Java配置(源服务器没有找到目标资源的当前表示。)

发布于 2025-01-19 13:13:59 字数 2447 浏览 3 评论 0原文

我尝试创建一个简单的 Spring MVC 并通过 eclipse ide 使用一个简单的控制器在 tomcat 中运行:

    @Controller
    public class HomeController {
    
        @RequestMapping("/home")
        public String helloWorld(Model model) {
            model.addAttribute("message", "home university!");
            return "home";
        }
    }

我的其他配置:

webinitializer:

public class WebAppInitializer implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(MvcConfig.class, ServiceConfig.class,  AspectConfig.class, DaoConfig.class, DataSourceConfig.class);
        context.setServletContext(servletContext);
     
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispather", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

MvcSpringJavaConfig:

public class MvcConfig {
   
    @Configuration
    @ComponentScan(basePackages = "img.imaginary.controller")
    @EnableWebMvc
    public class MvcConfiguration {

        @Bean
        public ViewResolver getViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            return resolver;
        }
    }
}

结构:

在此处输入图像描述

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                                http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
            id="WebApp_ID" version="3.1">
   
</web-app>

我的pom.xml: pom.xml

当 tomcat 启动并且我尝试打开该应用程序时在浏览器中,我得到 404 状态和描述:

源服务器尚未找到目标资源的当前表示或不想公开其存在。

我尝试更改控制器和 web.xml 以及其他配置,但我总是得到这个结果

我无法弄清楚我缺少什么才能让我的简单家庭控制器正确启动

I try to create a simple Spring MVC and run in tomcat via eclipse ide with one simple controller:

    @Controller
    public class HomeController {
    
        @RequestMapping("/home")
        public String helloWorld(Model model) {
            model.addAttribute("message", "home university!");
            return "home";
        }
    }

my other configs:

webinitializer:

public class WebAppInitializer implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(MvcConfig.class, ServiceConfig.class,  AspectConfig.class, DaoConfig.class, DataSourceConfig.class);
        context.setServletContext(servletContext);
     
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispather", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

MvcSpringJavaConfig:

public class MvcConfig {
   
    @Configuration
    @ComponentScan(basePackages = "img.imaginary.controller")
    @EnableWebMvc
    public class MvcConfiguration {

        @Bean
        public ViewResolver getViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            return resolver;
        }
    }
}

structure:

enter image description here

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                                http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
            id="WebApp_ID" version="3.1">
   
</web-app>

my pom.xml:
pom.xml

and when tomcat starts and I try to open the app in the browser, I get
a 404 status and a description:

The source server has not found the current representation of the target resource or does not want to disclose its existence.

I tried to change the controller and the web.xml and other configs, but I always get this result

I can't figure out what I'm missing in order for my simple home controller to start correctly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2025-01-26 13:13:59

您应该将 @Configuration 添加到 MvcConfig 类。还可以参考 这个

任何嵌套配置类都必须声明为静态。

MvcConfiguration 类必须是静态的。

you should add @Configuration to MvcConfig class. also by referring to this:

Any nested configuration classes must be declared as static.

MvcConfiguration class must be static.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文