繁体   English   中英

Spring-mvc Dispatcher Servlet映射给出了404错误

[英]Spring-mvc Dispatcher Servlet mapping giving 404 error

我跟随Pluralsight“Spring MVC 4简介”课程,我已经完成了之前的两门课程(Spring简介和Spring MVC简介)。

我没有使用任何XML配置,它纯粹基于Java / Annotation。 使用XML Equivalent,我可以访问“/greeting.html”页面,没有任何问题。 网站上的所有其他答案都涉及添加mvc:annotation-driven或不同的url-mapping,例如“/”或“* .do”,这无助于解决我的问题。

索引页面在服务器启动时显示(localhost:8080),但显示404 for localhost:8080 / greeting.html。

HTTP状态404 - 未找到

输入状态报告

描述源服务器未找到目标资源的当前表示,或者不愿意透露存在该目标资源。

控制台日志显示以下内容:

16:15:02.072 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - 名为'eventTrackerDispatcherServlet'的DispatcherServlet处理[/greeting.html]的GET请求

16:15:02.078 [http-nio-8080-exec-4] WARN org.springframework.web.servlet.PageNotFound - 在名为'eventTrackerDispatcherServlet'的DispatcherServlet中找不到带有URI [/greeting.html]的HTTP请求的映射

16:15:02.078 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - 已成功完成请求

请告知我可能遗漏的配置。

WebConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.pluralsight")
public class WebConfig {

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");

        return resolver;
    }
}

WebAppInitializer.java

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("eventTrackerDispatcherServlet", new DispatcherServlet(context));

        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("*.html");
    }

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("com.pluralsight.WebConfig");

        return context;
    }
}

HelloController.java

@Controller
public class HelloController {

    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    public String sayHello(Model model) {
        model.addAttribute("greeting", "Hello World :)");
        return "hello";
    }
}

为hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello JSP Page</title>
</head>
<body>
    <h1>${greeting}</h1>
    <br />
    <h4>This Thing On? :/</h4>
</body>
</html>

我重新观看了视频,试图找到我的错误,但还没有。 非常感谢。

编辑1 -使用基于XML的Servlet映射,我可以成功访问以下页面: http:// localhost:8080 / greeting.html我可以使用上面原始帖子中的HelloController代码访问此页面。 Greeting.html页面 - 使用XML配置

将我的应用程序从基于XML的配置转换为仅基于Java的配置后 ,我在访问该页面时开始接收404。

@hagrawal - 感谢您的评论,我设法让应用程序正常运行:

(1.)根据链接,您可以使用context.register(AppConfig.class)注册一个类; 或使用context.setConfigLocation(“com.example.app.config”);扫描完整包。 我看到你可以使用扫描包配置但指定一个类,所以我认为你应该使用context.setConfigLocation(“com.pluralsight”); 或context.register(“com.pluralsight.WebConfig.class”); 或context.register(“WebConfig.class”); - hagrawal

问题在于我的AnnotationConfigWebApplicationContext如何在我的WebAppInitializer类中注册我的“WebConfig”类。 我改变了:

context.setConfigLocation( “com.pluralsight.WebConfig”);

对此:

context.register(com.pluralsight.WebConfig.class);

现在应用程序找到了正确的映射来响应。 这意味着我的应用程序完全使用Java代码,并且不再配置Web.xml! :)

非常感谢帮助并向我提出建议!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM