Java Spring mvc Dispatcher Servlet映射出现404错误

Java Spring mvc Dispatcher Servlet映射出现404错误,java,spring,spring-mvc,http-status-code-404,Java,Spring,Spring Mvc,Http Status Code 404,我在学习Pluralsight“SpringMVC4简介”课程的同时,也完成了之前要求的两门课程(Spring简介和SpringMVC简介) 我没有使用任何XML配置,它完全基于Java/注释。使用XML等价物,我可以毫无问题地访问“/greeting.html”页面。该站点上的所有其他答案都涉及添加mvc:annotation-driven或其他url映射,如“/”或“*.do”,这并没有帮助解决我的问题 服务器启动时会显示索引页(localhost:8080),但对于localhost:80

我在学习Pluralsight“SpringMVC4简介”课程的同时,也完成了之前要求的两门课程(Spring简介和SpringMVC简介)

我没有使用任何XML配置,它完全基于Java/注释。使用XML等价物,我可以毫无问题地访问“/greeting.html”页面。该站点上的所有其他答案都涉及添加mvc:annotation-driven或其他url映射,如“/”或“*.do”,这并没有帮助解决我的问题

服务器启动时会显示索引页(localhost:8080),但对于localhost:8080/greeting.html会显示404

HTTP状态404–未找到

类型状态报告

说明源服务器未找到当前表示形式 对于目标资源或不愿意披露其存在

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

16:15:02.072[http-nio-8080-exec-4]调试 org.springframework.web.servlet.DispatcherServlet-DispatcherServlet 名为“eventTrackerDispatcherServlet”的正在处理的GET请求 [/greeting.html]

16:15:02.078[http-nio-8080-exec-4]警告 org.springframework.web.servlet.PageNotFound-未找到的映射 名为的DispatcherServlet中URI为[/greeting.html]的HTTP请求 “eventTrackerDispatcherServlet”

16:15:02.078[http-nio-8080-exec-4] 调试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>

你好JSP页面
${问候语}

这东西是开着的吗/
我重新观看了视频,试图找出我的错误,但还没有。 非常感谢

编辑1-使用基于XML的Servlet映射,我可以成功访问以下页面: 我可以使用HelloController代码访问此页面,如我的原始帖子中所述


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

@hagrawal-多亏了您的评论,我成功地使应用程序工作了:

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

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

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

为此:

register(com.pluralsight.WebConfig.class)

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


非常感谢你对我的帮助和建议

在WebContent或WEB-INF中,
greetins.html
直接放在哪里?您是否能够访问您的
index.jsp
,如果是,那么URL是什么?@hagrawal结构如下:/WEB-INF/jsp/hello.jsp和/WEB-INF/index.jsp。我可以通过[link](localhost:8080)和[link](localhost:8080/index.jsp)访问索引页,您的
greeting.html
在哪里,正如您在问题中提到的,您得到的是404。
@RequestMapping(value=“/greeting”,method=RequestMethod.GET)
尝试将
/greeting
替换为
/greeting.html
。@hagrawal-没有greeting.html页面。/greeting URL通过HelloController映射到hello.jsp文件。我在上面添加了类似的XML配置。