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

时间:2017-05-01 14:44:25

标签: java spring spring-mvc http-status-code-404

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

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

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

  

HTTP状态404 - 未找到

     

输入状态报告

     

描述源服务器未找到当前表示   对于目标资源或不愿意披露存在的资源。

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

  

16:15:02.072 [http-nio-8080-exec-4] DEBUG   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]   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 page - Working with XML configuration

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

1 个答案:

答案 0 :(得分:2)

@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”);

To This:

  

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

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

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

相关问题