gpt4 book ai didi

java - HTTP 状态 404 -/EventTracker/问候语

转载 作者:行者123 更新时间:2023-11-30 06:12:44 28 4
gpt4 key购买 nike

我正在尝试访问 http://localhost:8080/EventTracker/greeting在我的机器上。但是,我收到 404 错误。我正在关注 PluralSight Introduction to Spring MVC4 教程,看起来我的代码与视频中的代码匹配。我正在使用 WebConfig 和 WebAppInitializer 这两个 java 文件来配置我的应用程序。我错过了什么吗?我想我已经逐行复制了,但仍然没有用。

HelloController.java

@Controller
public class HelloController {

@RequestMapping(value="/greeting")
public String sayHello(Model model) {
model.addAttribute("greeting", "Hello World");

return "hello.jsp";
}
}

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("DispatcherServlet", new DispatcherServlet(context) );
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.html");

}

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

WebConfig.java

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

}

你好.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>

已编辑 9/15 下午 4:25 PST使用 http://localhost:8080/EventTracker/greeting.html 时,我仍然得到同样的错误,错误是:

16:24:41.925 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'DispatcherServlet' processing GET request for [/EventTracker/greeting.html]
16:24:41.931 [http-nio-8080-exec-3] WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/EventTracker/greeting.html] in DispatcherServlet with name 'DispatcherServlet'
16:24:41.931 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request

最佳答案

因为这是我在 google 中找到的第一个问题,这里没有正确答案,所以这对我有帮助。

接下来你应该添加WebAppInitializer.java

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

所以你的文件应该是这样的:

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("DispatcherServlet", new DispatcherServlet(context) );
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.html");

}

private WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.pluralsight.WebConfig");
context.register(com.pluralsight.WebConfig.class);
return context;
}
}

关于java - HTTP 状态 404 -/EventTracker/问候语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32597051/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com